summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/tc_defined_name.rb
blob: 51f5a4841a802701ab4831e0935f740deaf2e660 (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
require 'tc_helper'

class TestDefinedNames < Test::Unit::TestCase
  def setup 
    @dn = Axlsx::DefinedName.new('Sheet1!A1:A1')
  end

  def test_initialize
    assert_equal('Sheet1!A1:A1', @dn.formula)
  end
  
  def test_string_attributes
    Axlsx::DefinedName::STRING_ATTRIBUTES.each do |attr|
      assert_raise(ArgumentError, 'only strings allowed in string attributes') { @dn.send("#{attr}=", 1) }
      assert_nothing_raised { @dn.send("#{attr}=", '_xlnm.Sheet_Title') }
    end
  end

  def test_boolean_attributes
   Axlsx::DefinedName::BOOLEAN_ATTRIBUTES.each do |attr|
      assert_raise(ArgumentError, 'only booleanish allowed in string attributes') { @dn.send("#{attr}=", 'foo') }
      assert_nothing_raised { @dn.send("#{attr}=", 1) }
    end

  end

  def test_local_sheet_id
    assert_raise(ArgumentError, 'local_sheet_id must be an unsigned int') { @dn.local_sheet_id = -1 }
    assert_nothing_raised { @dn.local_sheet_id = 1 }
  end

  def test_to_xml_string
    assert_raise(ArgumentError, 'name is required for serialization') { @dn.to_xml_string }
    @dn.name = '_xlnm.Print_Titles'
    @dn.hidden = true
    doc = Nokogiri::XML(@dn.to_xml_string)
    assert(doc.xpath("//definedName[@name='_xlnm.Print_Titles']"))
    assert(doc.xpath("//definedName[@hidden='true']"))
    assert_equal('Sheet1!A1:A1', doc.xpath('//definedName').text)
  end
end