summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_table.rb
blob: fd6c1923536226996385374a8550b2f620be594a (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
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'tc_helper.rb'

class TestTable < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    @ws = p.workbook.add_worksheet
    40.times do
      @ws << ["aa","aa","aa","aa","aa","aa"]
    end
  end

  def test_initialization
    assert(@ws.workbook.tables.empty?)
    assert(@ws.tables.empty?)

  end

  def test_table_style_info
    table = @ws.add_table('A1:D5', :name => 'foo', :style_info => { :show_row_stripes => true, :name => "TableStyleMedium25" })
    assert_equal('TableStyleMedium25', table.table_style_info.name)
    assert_equal(true, table.table_style_info.show_row_stripes)
  end

  def test_add_table
    name = "test"
    table = @ws.add_table("A1:D5", :name => name)
    assert(table.is_a?(Axlsx::Table), "must create a table")
    assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection")
    assert_equal(@ws.tables.last, table, "must be added to worksheet table collection")
    assert_equal(table.name, name, "options for name are applied")
  end

  def test_pn
    @ws.add_table("A1:D5")
    assert_equal(@ws.tables.first.pn, "tables/table1.xml")
  end

  def test_rId
    table = @ws.add_table("A1:D5")
    assert_equal @ws.relationships.for(table).Id, table.rId
  end

  def test_index
    @ws.add_table("A1:D5")
    assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first))
  end

  def test_relationships
    assert(@ws.relationships.empty?)
    @ws.add_table("A1:D5")
    assert_equal(@ws.relationships.size, 1, "adding a table adds a relationship")
    @ws.add_table("F1:J5")
    assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship")
  end

  def test_to_xml_string
    table = @ws.add_table("A1:D5")
    schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
    doc = Nokogiri::XML(table.to_xml_string)
    errors = []
    schema.validate(doc).each do |error|
      errors.push error
      puts error.message
    end
    assert(errors.empty?, "error free validation")
  end

  def test_to_xml_string_for_special_characters
    cell = @ws.rows.first.cells.first
    cell.value = "&><'\""

    table = @ws.add_table("A1:D5")
    doc = Nokogiri::XML(table.to_xml_string)
    errors = doc.errors
    assert(errors.empty?, "invalid xml: #{errors.map(&:to_s).join(', ')}")
  end
end