summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_col.rb
blob: c20d32be9449141585c1015e4bba889dd3900375 (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
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

require 'tc_helper'

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_nil(@col.bestFit)
    assert_raise(NoMethodError, 'bestFit is read only') { @col.bestFit = 'bob' }
    @col.width = 1.999

    assert(@col.bestFit, 'bestFit should be true when width has been set')
  end

  def test_collapsed
    assert_nil(@col.collapsed)
    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_nil(@col.customWidth)
    @col.width = 3
    assert_raise(NoMethodError, 'customWidth is read only') { @col.customWidth = 3 }
    assert(@col.customWidth, 'customWidth is true when width is set')
  end

  def test_widthUnderLimit
    @col.width = 3

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

  def test_widthOverLimit
    @col.width = 31_337

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

  def test_widthSetToNil
    @col.width = nil

    assert_nil(@col.width, 'width is set to unspecified value')
  end

  def test_hidden
    assert_nil(@col.hidden)
    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_nil(@col.outlineLevel)
    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_nil(@col.phonetic)
    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_nil(@col.style)
    @col.style = 1

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