summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/tc_shared_strings_table.rb
blob: 4aa308f1586eb6b80b09a6e642c172f5d82d194a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

require 'tc_helper'

class TestSharedStringsTable < Test::Unit::TestCase
  def setup
    @p = Axlsx::Package.new :use_shared_strings => true

    ws = @p.workbook.add_worksheet
    ws.add_row ['a', 1, 'b']
    ws.add_row ['b', 1, 'c']
    ws.add_row ['c', 1, 'd']
    ws.rows.last.add_cell('b', :type => :text)
  end

  def test_workbook_has_shared_strings
    assert(@p.workbook.shared_strings.is_a?(Axlsx::SharedStringsTable), "shared string table was not created")
  end

  def test_count
    sst = @p.workbook.shared_strings

    assert_equal(7, sst.count)
  end

  def test_unique_count
    sst = @p.workbook.shared_strings

    assert_equal(4, sst.unique_count)
  end

  def test_uses_workbook_xml_space
    assert_equal(@p.workbook.xml_space, @p.workbook.shared_strings.xml_space)
    @p.workbook.xml_space = :default

    assert_equal(:default, @p.workbook.shared_strings.xml_space)
  end

  def test_valid_document
    schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
    doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string)
    errors = []
    schema.validate(doc).each do |error|
      puts error.message
      errors << error
    end

    assert_equal(0, errors.size, "sharedStirngs.xml Invalid" + errors.map(&:message).to_s)
  end

  def test_remove_control_characters_in_xml_serialization
    nasties = "hello\x10\x00\x1C\x1Eworld"
    @p.workbook.worksheets[0].add_row [nasties]

    # test that the nasty string was added to the shared strings
    assert @p.workbook.shared_strings.unique_cells.key?(nasties)

    # test that none of the control characters are in the XML output for shared strings
    assert_no_match(/#{Axlsx::CONTROL_CHARS}/o, @p.workbook.shared_strings.to_xml_string)

    # assert that the shared string was normalized to remove the control characters
    assert_not_nil @p.workbook.shared_strings.to_xml_string.index("helloworld")
  end
end