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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# frozen_string_literal: true
require 'tc_helper'
class TestAxlsx < Test::Unit::TestCase
# rubocop:disable Layout/HashAlignment
def setup_wide
@wide_test_points = {
"A3" => 0,
"Z3" => 25,
"B3" => 1,
"AA3" => (1 * 26) + 0,
"AAA3" => (1 * (26**2)) + (1 * 26) + 0,
"AAZ3" => (1 * (26**2)) + (1 * 26) + 25,
"ABA3" => (1 * (26**2)) + (2 * 26) + 0,
"BZU3" => (2 * (26**2)) + (26 * 26) + 20
}
end
# rubocop:enable Layout/HashAlignment
def test_cell_range_empty_if_no_cell
assert_equal("", Axlsx.cell_range([]))
end
def test_do_not_trust_input_by_default
refute Axlsx.trust_input
end
def test_trust_input_can_be_set_to_true
# Class variables like this are not reset between test runs, so we have
# to save and restore the original value manually.
old = Axlsx.trust_input
Axlsx.trust_input = true
assert Axlsx.trust_input
Axlsx.trust_input = old
end
def test_cell_range_relative
p = Axlsx::Package.new
ws = p.workbook.add_worksheet
row = ws.add_row
c1 = row.add_cell
c2 = row.add_cell
assert_equal("A1:B1", Axlsx.cell_range([c2, c1], false))
end
def test_cell_range_absolute
p = Axlsx::Package.new
ws = p.workbook.add_worksheet name: "Sheet <'>\" 1"
row = ws.add_row
c1 = row.add_cell
c2 = row.add_cell
assert_equal("'Sheet <''>" 1'!$A$1:$B$1", Axlsx.cell_range([c2, c1], true))
end
def test_cell_range_row
p = Axlsx::Package.new
ws = p.workbook.add_worksheet
row = ws.add_row
row.add_cell
row.add_cell
row.add_cell
assert_equal("A1:C1", Axlsx.cell_range(row, false))
end
def test_name_to_indices
setup_wide
@wide_test_points.each do |key, value|
assert_equal(Axlsx.name_to_indices(key), [value, 2])
end
end
def test_col_ref
setup_wide
@wide_test_points.each do |key, value|
assert_equal(Axlsx.col_ref(value), key.gsub(/\d+/, ''))
end
end
def test_row_ref
assert_equal('1', Axlsx.row_ref(0))
assert_equal('100', Axlsx.row_ref(99))
end
def test_cell_r
assert_equal('A1', Axlsx.cell_r(0, 0))
assert_equal('Z26', Axlsx.cell_r(25, 25))
end
def test_range_to_a
assert_equal([['A1', 'B1', 'C1']], Axlsx.range_to_a('A1:C1'))
assert_equal([['A1', 'B1', 'C1'], ['A2', 'B2', 'C2']], Axlsx.range_to_a('A1:C2'))
assert_equal([['Z5', 'AA5', 'AB5'], ['Z6', 'AA6', 'AB6']], Axlsx.range_to_a('Z5:AB6'))
end
def test_sanitize_frozen_control_strippped
needs_sanitize = "legit\x08" # Backspace control char
assert_equal('legit', Axlsx.sanitize(needs_sanitize), 'should strip control chars')
end
def test_sanitize_unfrozen_control_strippped
needs_sanitize = +"legit\x08" # Backspace control char
sanitized_str = Axlsx.sanitize(needs_sanitize)
assert_equal('legit', sanitized_str, 'should strip control chars')
assert_equal(sanitized_str.object_id, sanitized_str.object_id, 'should preserve object')
end
def test_sanitize_unfrozen_no_sanitize
legit_str = +'legit'
sanitized_str = Axlsx.sanitize(legit_str)
assert_equal(sanitized_str, legit_str, 'should preserve value')
assert_equal(sanitized_str.object_id, legit_str.object_id, 'should preserve object')
end
class InstanceValuesSubject
def initialize(args = {})
args.each do |key, v|
instance_variable_set("@#{key}".to_sym, v)
end
end
end
def test_instance_values_for
empty = InstanceValuesSubject.new
assert_empty(Axlsx.instance_values_for(empty), 'should generate with no ivars')
single = InstanceValuesSubject.new(a: 2)
assert_equal({ "a" => 2 }, Axlsx.instance_values_for(single), 'should generate for a single ivar')
double = InstanceValuesSubject.new(a: 2, b: "c")
assert_equal({ "a" => 2, "b" => "c" }, Axlsx.instance_values_for(double), 'should generate for multiple ivars')
inner_obj = Object.new
complex = InstanceValuesSubject.new(obj: inner_obj)
assert_equal({ "obj" => inner_obj }, Axlsx.instance_values_for(complex), 'should pass value of ivar directly')
nil_subject = InstanceValuesSubject.new(nil_obj: nil)
assert_equal({ "nil_obj" => nil }, Axlsx.instance_values_for(nil_subject), 'should return nil ivars')
end
def test_hash_deep_merge
h1 = { foo: { bar: true } }
h2 = { foo: { baz: true } }
assert_equal({ foo: { baz: true } }, h1.merge(h2))
assert_equal({ foo: { bar: true, baz: true } }, Axlsx.hash_deep_merge(h1, h2))
end
def test_escape_formulas
Axlsx.instance_variable_set(:@escape_formulas, nil)
refute Axlsx.escape_formulas
Axlsx.escape_formulas = true
assert Axlsx.escape_formulas
Axlsx.escape_formulas = false
refute Axlsx.escape_formulas
ensure
Axlsx.instance_variable_set(:@escape_formulas, nil)
end
end
|