summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_col.rb
blob: 2f31d7a23ab4f787f2e7404feb76e1d043c72b9b (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'tc_helper.rb'

class TestCol < Test::Unit::TestCase
  def setup
    @col = Axlsx::Col.new 1, 1
  end

  def test_initialize
    options = { :width => 12, :collapsed => true, :hidden => true, :outline_level => 1, :phonetic => true, :style => 1}

    col = Axlsx::Col.new 0, 0, options
    options.each{ |key, value| assert_equal(col.send(key.to_sym), value) }
  end

  def test_min_max_required
    assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new }
    assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new nil, nil }
    assert_nothing_raised { Axlsx::Col.new 1, 1 }
  end

  def test_bestFit
    assert_equal(@col.bestFit, nil)
    assert_raise(NoMethodError, 'bestFit is read only') { @col.bestFit = 'bob' }
    @col.width = 1.999
    assert_equal(@col.bestFit, true, 'bestFit should be true when width has been set')
  end

  def test_collapsed
    assert_equal(@col.collapsed, nil)
    assert_raise(ArgumentError, 'collapsed must be boolean(ish)') { @col.collapsed = 'bob' }
    assert_nothing_raised('collapsed must be boolean(ish)') { @col.collapsed = true }
  end

  def test_customWidth
    assert_equal(@col.customWidth, nil)
    @col.width = 3
    assert_raise(NoMethodError, 'customWidth is read only') { @col.customWidth = 3 }
    assert_equal(@col.customWidth, true, 'customWidth is true when width is set')
  end

  def test_widthUnderLimit
    @col.width = 3
    assert_equal(@col.width, 3, 'width is set to exact value')
  end

  def test_widthOverLimit
    @col.width = 31337
    assert_equal(@col.width, 255, 'width is set to maximum allowed value')
  end

  def test_widthSetToNil
    @col.width = nil
    assert_equal(@col.width, nil, 'width is set to unspecified value')
  end

  def test_hidden
    assert_equal(@col.hidden, nil)
    assert_raise(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = 'bob' }
    assert_nothing_raised(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = true }
  end

  def test_outlineLevel
    assert_equal(@col.outlineLevel, nil)
    assert_raise(ArgumentError, 'outline level cannot be negative') { @col.outlineLevel = -1 }
    assert_raise(ArgumentError, 'outline level cannot be greater than 7') { @col.outlineLevel = 8 }
    assert_nothing_raised('can set outlineLevel') { @col.outlineLevel = 1 }
  end

  def test_phonetic
    assert_equal(@col.phonetic, nil)
    assert_raise(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = 'bob' }
    assert_nothing_raised(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = true }
  end

  def test_to_xml_string
    @col.width = 100
    doc = Nokogiri::XML(@col.to_xml_string)
    assert_equal(1, doc.xpath("//col [@bestFit='#{@col.best_fit ? 1 : 0}']").size)
    assert_equal(1, doc.xpath("//col [@max=#{@col.max}]").size)
    assert_equal(1, doc.xpath("//col [@min=#{@col.min}]").size)
    assert_equal(1, doc.xpath("//col [@width=#{@col.width}]").size)
    assert_equal(1, doc.xpath("//col [@customWidth='#{@col.custom_width ? 1 : 0}']").size)
  end

  def test_style
    assert_equal(@col.style, nil)
    @col.style = 1
    assert_equal(@col.style, 1)
    #TODO check that the style specified is actually in the styles xfs collection
  end
end