summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_sheet_protection.rb
blob: ebfff4b617c9b6a0b6b6b260b93d49f03de930b6 (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
# frozen_string_literal: true

require 'tc_helper'

# <xsd:complexType name="CT_SheetProtection">
# <xsd:attribute name="sheet" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="objects" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="scenarios" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="formatCells" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="formatColumns" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="formatRows" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="insertColumns" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="insertRows" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="insertHyperlinks" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="deleteColumns" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="deleteRows" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="selectLockedCells" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="sort" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="autoFilter" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="pivotTables" type="xsd:boolean" use="optional" default="true"/>
# <xsd:attribute name="selectUnlockedCells" type="xsd:boolean" use="optional" default=0/>
# <xsd:attribute name="password" type="xsd:string" use="optional" default="nil"/>
# </xsd:complexType>

class TestSheetProtection < Test::Unit::TestCase
  def setup
    # inverse defaults
    @boolean_options = { sheet: false, objects: true, scenarios: true, format_cells: false,
                         format_columns: false, format_rows: false, insert_columns: false, insert_rows: false,
                         insert_hyperlinks: false, delete_columns: false, delete_rows: false, select_locked_cells: true,
                         sort: false, auto_filter: false, pivot_tables: false, select_unlocked_cells: true }

    @string_options = { password: nil }

    @options = @boolean_options.merge(@string_options)

    @sp = Axlsx::SheetProtection.new(@options)
  end

  def test_initialize
    sp = Axlsx::SheetProtection.new
    @boolean_options.each do |key, value|
      assert_equal(!value, sp.send(key.to_sym), "initialized default #{key} should be #{!value}")
      assert_equal(value, @sp.send(key.to_sym), "initialized options #{key} should be #{value}")
    end
  end

  def test_boolean_attribute_validation
    @boolean_options.each do |key, value|
      assert_raise(ArgumentError, "#{key} must be boolean") { @sp.send("#{key}=".to_sym, 'A') }
      assert_nothing_raised { @sp.send("#{key}=".to_sym, value) }
    end
  end

  def test_to_xml_string
    @sp.password = 'fish' # -> CA3F
    doc = Nokogiri::XML(@sp.to_xml_string)

    @options.each do |key, value|
      assert(doc.xpath("//sheetProtection[@#{key.to_s.gsub(/_(.)/) { $1.upcase }}='#{value}']"))
    end
  end
end