workbook 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +21 -0
- data/.gitignore +4 -1
- data/.ruby-version +1 -1
- data/.travis.yml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +2 -2
- data/README.md +9 -7
- data/Rakefile +6 -6
- data/json_test.json +1 -0
- data/lib/workbook/book.rb +73 -62
- data/lib/workbook/cell.rb +58 -13
- data/lib/workbook/column.rb +31 -28
- data/lib/workbook/format.rb +23 -24
- data/lib/workbook/generatetypes.rb +4 -4
- data/lib/workbook/modules/cache.rb +6 -7
- data/lib/workbook/modules/cell.rb +77 -100
- data/lib/workbook/modules/diff_sort.rb +92 -83
- data/lib/workbook/modules/raw_objects_storage.rb +6 -8
- data/lib/workbook/modules/type_parser.rb +30 -22
- data/lib/workbook/nil_value.rb +4 -9
- data/lib/workbook/readers/csv_reader.rb +7 -10
- data/lib/workbook/readers/ods_reader.rb +51 -50
- data/lib/workbook/readers/txt_reader.rb +6 -8
- data/lib/workbook/readers/xls_reader.rb +21 -33
- data/lib/workbook/readers/xls_shared.rb +106 -117
- data/lib/workbook/readers/xlsx_reader.rb +45 -46
- data/lib/workbook/row.rb +99 -84
- data/lib/workbook/sheet.rb +47 -38
- data/lib/workbook/table.rb +96 -72
- data/lib/workbook/template.rb +12 -15
- data/lib/workbook/types/false.rb +0 -1
- data/lib/workbook/types/nil.rb +0 -1
- data/lib/workbook/types/nil_class.rb +1 -1
- data/lib/workbook/types/numeric.rb +1 -1
- data/lib/workbook/types/string.rb +1 -1
- data/lib/workbook/types/time.rb +1 -1
- data/lib/workbook/types/true.rb +0 -1
- data/lib/workbook/types/true_class.rb +1 -1
- data/lib/workbook/version.rb +2 -3
- data/lib/workbook/writers/csv_table_writer.rb +10 -13
- data/lib/workbook/writers/html_writer.rb +34 -38
- data/lib/workbook/writers/json_table_writer.rb +8 -11
- data/lib/workbook/writers/xls_writer.rb +30 -36
- data/lib/workbook/writers/xlsx_writer.rb +45 -29
- data/lib/workbook.rb +16 -15
- data/test/artifacts/currency_test.ods +0 -0
- data/test/helper.rb +6 -5
- data/test/test_book.rb +41 -38
- data/test/test_column.rb +26 -24
- data/test/test_format.rb +51 -55
- data/test/test_functional.rb +7 -8
- data/test/test_modules_cache.rb +18 -17
- data/test/test_modules_cell.rb +55 -46
- data/test/test_modules_table_diff_sort.rb +55 -64
- data/test/test_modules_type_parser.rb +61 -31
- data/test/test_readers_csv_reader.rb +48 -42
- data/test/test_readers_ods_reader.rb +36 -31
- data/test/test_readers_txt_reader.rb +21 -23
- data/test/test_readers_xls_reader.rb +20 -23
- data/test/test_readers_xls_shared.rb +2 -3
- data/test/test_readers_xlsx_reader.rb +44 -37
- data/test/test_row.rb +105 -109
- data/test/test_sheet.rb +35 -41
- data/test/test_table.rb +82 -60
- data/test/test_template.rb +16 -15
- data/test/test_types_date.rb +4 -6
- data/test/test_writers_csv_writer.rb +24 -0
- data/test/test_writers_html_writer.rb +42 -41
- data/test/test_writers_json_writer.rb +16 -9
- data/test/test_writers_xls_writer.rb +50 -35
- data/test/test_writers_xlsx_writer.rb +62 -34
- data/workbook.gemspec +25 -27
- metadata +96 -42
data/test/test_table.rb
CHANGED
@@ -1,39 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
5
4
|
|
6
|
-
class TestTable< Minitest::Test
|
5
|
+
class TestTable < Minitest::Test
|
7
6
|
def test_initialize
|
8
7
|
t = Workbook::Table.new
|
9
|
-
assert_equal(
|
8
|
+
assert_equal([], t.rows)
|
10
9
|
c = Workbook::Cell.new("celllll")
|
11
10
|
t = Workbook::Table.new [[c]]
|
12
11
|
|
13
|
-
assert_equal([[c]],t)
|
12
|
+
assert_equal([[c]], t.rows.map(&:cells))
|
14
13
|
end
|
14
|
+
|
15
15
|
def test_header
|
16
16
|
t = Workbook::Table.new
|
17
17
|
assert_nil(t.header)
|
18
18
|
t = Workbook::Table.new [[1]]
|
19
|
-
assert_equal(t.header,[1])
|
20
|
-
assert_equal(t.header.class,Workbook::Row)
|
19
|
+
assert_equal(t.header, [1])
|
20
|
+
assert_equal(t.header.class, Workbook::Row)
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_new_row
|
24
24
|
t = Workbook::Table.new
|
25
25
|
assert_equal(t.count, 0)
|
26
26
|
|
27
|
-
r = t.new_row [1,2,3,4]
|
28
|
-
assert_equal(r, [1,2,3,4])
|
27
|
+
r = t.new_row [1, 2, 3, 4]
|
28
|
+
assert_equal(r, [1, 2, 3, 4])
|
29
29
|
assert_equal(r.class, Workbook::Row)
|
30
|
-
assert_equal(1,t.count)
|
30
|
+
assert_equal(1, t.count)
|
31
31
|
|
32
32
|
r = t.new_row
|
33
33
|
assert_equal(r.empty?, true)
|
34
34
|
assert_equal(r.class, Workbook::Row)
|
35
35
|
|
36
36
|
assert_equal(2, t.count)
|
37
|
+
|
38
|
+
assert_equal(r, t.rows.last)
|
37
39
|
assert_equal(r, t.last)
|
38
40
|
|
39
41
|
r << 2
|
@@ -43,19 +45,19 @@ class TestTable< Minitest::Test
|
|
43
45
|
|
44
46
|
def test_append_row
|
45
47
|
t = Workbook::Table.new
|
46
|
-
row = t.new_row(["a","b"])
|
48
|
+
row = t.new_row(["a", "b"])
|
47
49
|
assert_equal(row, t.header)
|
48
|
-
row = Workbook::Row.new([1,2])
|
50
|
+
row = Workbook::Row.new([1, 2])
|
49
51
|
assert_nil(row.table)
|
50
52
|
t.push(row)
|
51
53
|
assert_equal(t, row.table)
|
52
|
-
row = Workbook::Row.new([3,4])
|
54
|
+
row = Workbook::Row.new([3, 4])
|
53
55
|
assert_nil(row.table)
|
54
56
|
t << row
|
55
57
|
assert_equal(t, row.table)
|
56
58
|
t = Workbook::Table.new
|
57
|
-
t << [1,2,3,4]
|
58
|
-
assert_equal(Workbook::Row,t.first.class)
|
59
|
+
t << [1, 2, 3, 4]
|
60
|
+
assert_equal(Workbook::Row, t.first.class)
|
59
61
|
end
|
60
62
|
|
61
63
|
def test_sheet
|
@@ -72,56 +74,65 @@ class TestTable< Minitest::Test
|
|
72
74
|
end
|
73
75
|
|
74
76
|
def test_delete_all
|
75
|
-
w = Workbook::Book.new [["a","b"],[1,2],[3,4]]
|
77
|
+
w = Workbook::Book.new [["a", "b"], [1, 2], [3, 4]]
|
76
78
|
t = w.sheet.table
|
77
79
|
t.delete_all
|
78
|
-
assert_equal(Workbook::Table,t.class)
|
79
|
-
assert_equal(0,t.count)
|
80
|
+
assert_equal(Workbook::Table, t.class)
|
81
|
+
assert_equal(0, t.count)
|
80
82
|
end
|
81
83
|
|
82
84
|
def test_clone
|
83
|
-
w = Workbook::Book.new [["a","b"],[1,2],[3,4]]
|
85
|
+
w = Workbook::Book.new [["a", "b"], [1, 2], [3, 4]]
|
84
86
|
t = w.sheet.table
|
85
|
-
assert_equal(3,t[2][:a])
|
87
|
+
assert_equal(3, t[2][:a])
|
86
88
|
t2 = t.clone
|
87
89
|
t2[2][:a] = 5
|
88
|
-
assert_equal(3,t[2][:a])
|
89
|
-
assert_equal(5,t2[2][:a])
|
90
|
+
assert_equal(3, t[2][:a])
|
91
|
+
assert_equal(5, t2[2][:a])
|
90
92
|
end
|
91
93
|
|
92
94
|
def test_clone_custom_header
|
93
|
-
w = Workbook::Book.new [[nil, nil],["a","b"],[1,2],[3,4]]
|
95
|
+
w = Workbook::Book.new [[nil, nil], ["a", "b"], [1, 2], [3, 4]]
|
94
96
|
t = w.sheet.table
|
95
|
-
t.header=t[1]
|
96
|
-
assert_equal(3,t[3][:a])
|
97
|
+
t.header = t[1]
|
98
|
+
assert_equal(3, t[3][:a])
|
97
99
|
t2 = t.clone
|
100
|
+
assert_equal(1, t2.header_row_index)
|
101
|
+
|
98
102
|
t2[3][:a] = 5
|
99
|
-
assert_equal(3,t[3][:a])
|
100
|
-
assert_equal(5,t2[3][:a])
|
103
|
+
assert_equal(3, t[3][:a])
|
104
|
+
assert_equal(5, t2[3][:a])
|
101
105
|
end
|
102
106
|
|
103
107
|
def test_spreadsheet_style_cell_addressing
|
104
|
-
w = Workbook::Book.new [[nil, nil],["a","b"],[1,2],[3,4]]
|
108
|
+
w = Workbook::Book.new [[nil, nil], ["a", "b"], [1, 2], [3, 4]]
|
105
109
|
t = w.sheet.table
|
106
110
|
assert_nil(t["A1"].value)
|
107
111
|
assert_nil(t["B1"].value)
|
108
|
-
assert_equal("a",t["A2"].value)
|
109
|
-
assert_equal("b",t["B2"].value)
|
110
|
-
assert_equal(1,t["A3"].value)
|
111
|
-
assert_equal(2,t["B3"].value)
|
112
|
-
assert_equal(3,t["A4"].value)
|
113
|
-
assert_equal(4,t["B4"].value)
|
112
|
+
assert_equal("a", t["A2"].value)
|
113
|
+
assert_equal("b", t["B2"].value)
|
114
|
+
assert_equal(1, t["A3"].value)
|
115
|
+
assert_equal(2, t["B3"].value)
|
116
|
+
assert_equal(3, t["A4"].value)
|
117
|
+
assert_equal(4, t["B4"].value)
|
118
|
+
assert_equal(["b", 2, 4], t["B"].map(&:value))
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_column_retrieval_trough_symbol
|
122
|
+
t = Workbook::Book.new([["a", "Andere Kolom"], [1, 2], [3, 4]]).sheet.table
|
123
|
+
assert_equal([1, 3], t[:a].map(&:value))
|
124
|
+
assert_equal([2, 4], t[:andere_kolom].map(&:value))
|
114
125
|
end
|
115
126
|
|
116
127
|
def test_multirowselect_through_collections
|
117
|
-
w = Workbook::Book.new [["a","b"],[1,2],[3,4]]
|
128
|
+
w = Workbook::Book.new [["a", "b"], [1, 2], [3, 4]]
|
118
129
|
t = w.sheet.table
|
119
|
-
assert_equal(Workbook::Table,t[0..2].class)
|
120
|
-
assert_equal(2,t[0..2][1][1])
|
130
|
+
assert_equal(Workbook::Table, t[0..2].class)
|
131
|
+
assert_equal(2, t[0..2][1][1])
|
121
132
|
end
|
122
133
|
|
123
134
|
def test_table
|
124
|
-
w = Workbook::Book.new [[nil,nil],["a","b"],[1,2],[3,4]]
|
135
|
+
w = Workbook::Book.new [[nil, nil], ["a", "b"], [1, 2], [3, 4]]
|
125
136
|
t = w.sheet.table
|
126
137
|
w2 = Workbook::Book.new
|
127
138
|
w2.sheet.table = t[2..3]
|
@@ -129,7 +140,7 @@ class TestTable< Minitest::Test
|
|
129
140
|
end
|
130
141
|
|
131
142
|
def test_array_style_assignment
|
132
|
-
w = Workbook::Book.new [["a","b"],[1,2],[3,4]]
|
143
|
+
w = Workbook::Book.new [["a", "b"], [1, 2], [3, 4]]
|
133
144
|
t = w.sheet.table
|
134
145
|
r = t[1].clone
|
135
146
|
assert_nil(r.table)
|
@@ -138,7 +149,7 @@ class TestTable< Minitest::Test
|
|
138
149
|
end
|
139
150
|
|
140
151
|
def test_delete_at
|
141
|
-
w = Workbook::Book.new [["a","b"],[1,2],[3,4]]
|
152
|
+
w = Workbook::Book.new [["a", "b"], [1, 2], [3, 4]]
|
142
153
|
t = w.sheet.table
|
143
154
|
t.delete_at 2
|
144
155
|
assert_equal(1, t.last.first.value)
|
@@ -146,50 +157,61 @@ class TestTable< Minitest::Test
|
|
146
157
|
|
147
158
|
def test_trim!
|
148
159
|
t = Workbook::Table.new
|
149
|
-
t << [1,2,3]
|
150
|
-
t << [1,2,nil,nil]
|
160
|
+
t << [1, 2, 3]
|
161
|
+
t << [1, 2, nil, nil]
|
151
162
|
t.trim!
|
152
|
-
assert_equal("1,2,3\n1,2,\n",t.to_csv)
|
163
|
+
assert_equal("1,2,3\n1,2,\n", t.to_csv)
|
153
164
|
t = Workbook::Table.new
|
154
|
-
t << [1,2,3]
|
165
|
+
t << [1, 2, 3]
|
155
166
|
t << [nil]
|
156
|
-
t << [1,2,nil,nil]
|
157
|
-
t << [nil,nil,nil,nil]
|
158
|
-
t << [nil,nil,nil,nil]
|
167
|
+
t << [1, 2, nil, nil]
|
168
|
+
t << [nil, nil, nil, nil]
|
169
|
+
t << [nil, nil, nil, nil]
|
159
170
|
t.trim!
|
160
|
-
assert_equal("1,2,3\n,,\n1,2,\n",t.to_csv)
|
171
|
+
assert_equal("1,2,3\n,,\n1,2,\n", t.to_csv)
|
161
172
|
end
|
173
|
+
|
162
174
|
def test_performance
|
163
175
|
table = Workbook::Table.new
|
164
|
-
headers = 100.times.collect{|a| "header#{a}"}
|
165
|
-
first_row = 100.times.collect{|a| Time.now}
|
176
|
+
headers = 100.times.collect { |a| "header#{a}" }
|
177
|
+
first_row = 100.times.collect { |a| Time.now }
|
166
178
|
table << headers.shuffle
|
167
179
|
table << first_row
|
168
180
|
1000.times do |times|
|
169
181
|
row = table[1].clone
|
170
182
|
table << row
|
171
183
|
headers.each do |a|
|
172
|
-
row[a.to_sym]=Time.now
|
184
|
+
row[a.to_sym] = Time.now
|
173
185
|
end
|
174
186
|
end
|
175
|
-
last_line = table.count-1
|
187
|
+
last_line = table.count - 1
|
176
188
|
delta_start = table[12][0].value - table[2][0].value
|
177
|
-
delta_end = table[last_line][0].value - table[last_line-10][0].value
|
178
|
-
average_run_time = (delta_start+delta_end / 20)
|
189
|
+
delta_end = table[last_line][0].value - table[last_line - 10][0].value
|
190
|
+
average_run_time = (delta_start + delta_end / 20)
|
179
191
|
if (delta_end - delta_start) > average_run_time
|
180
192
|
puts "Performance issue"
|
181
193
|
end
|
182
194
|
end
|
195
|
+
|
183
196
|
def test_columns
|
184
197
|
table = Workbook::Table.new([[]])
|
185
|
-
assert_equal(table.columns,[])
|
186
|
-
table = Workbook::Table.new([[:a
|
187
|
-
assert_equal(table.columns.count,2)
|
198
|
+
assert_equal(table.columns, [])
|
199
|
+
table = Workbook::Table.new([[:a, :b], [1, 2]])
|
200
|
+
assert_equal(table.columns.count, 2)
|
188
201
|
end
|
202
|
+
|
189
203
|
def test_dimensions
|
190
204
|
table = Workbook::Table.new([[]])
|
191
|
-
assert_equal([0,1],table.dimensions)
|
192
|
-
table = Workbook::Table.new([[:a
|
193
|
-
assert_equal([4,2],table.dimensions)
|
205
|
+
assert_equal([0, 1], table.dimensions)
|
206
|
+
table = Workbook::Table.new([[:a, :b], [1, 2, 3, 4]])
|
207
|
+
assert_equal([4, 2], table.dimensions)
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_push
|
211
|
+
table = Workbook::Table.new([["a", 2, 3]])
|
212
|
+
table.push(["b", 2, 3])
|
213
|
+
assert_equal("b", table[1][0].value)
|
214
|
+
table.push(["c", 2, 3], ["d", 2, 3])
|
215
|
+
assert_equal("d", table[3][0].value)
|
194
216
|
end
|
195
217
|
end
|
data/test/test_template.rb
CHANGED
@@ -1,41 +1,42 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
5
4
|
|
6
5
|
class TestTemplate < Minitest::Test
|
7
|
-
|
8
6
|
def test_initalize
|
9
7
|
t = Workbook::Template.new
|
10
|
-
assert_equal(true,(t.methods.include?(:add_raw)
|
11
|
-
assert_equal(true,(t.methods.include?(:has_raw_for?)
|
12
|
-
assert_equal(true,(t.methods.include?(:raws)
|
8
|
+
assert_equal(true, (t.methods.include?(:add_raw) || t.methods.include?("add_raw")))
|
9
|
+
assert_equal(true, (t.methods.include?(:has_raw_for?) || t.methods.include?("has_raw_for?")))
|
10
|
+
assert_equal(true, (t.methods.include?(:raws) || t.methods.include?("raws")))
|
13
11
|
end
|
14
12
|
|
15
13
|
def test_add_raw_and_has_raw_for
|
16
14
|
t = Workbook::Template.new
|
17
15
|
t.add_raw "asdfsadf"
|
18
|
-
assert_equal(false,t.has_raw_for?(Integer))
|
19
|
-
assert_equal(true,t.has_raw_for?(String))
|
16
|
+
assert_equal(false, t.has_raw_for?(Integer))
|
17
|
+
assert_equal(true, t.has_raw_for?(String))
|
20
18
|
end
|
19
|
+
|
21
20
|
def test_raws
|
22
21
|
t = Workbook::Template.new
|
23
22
|
t.add_raw "asdfsadf"
|
24
|
-
assert_equal({String=>"asdfsadf"}, t.raws)
|
23
|
+
assert_equal({String => "asdfsadf"}, t.raws)
|
25
24
|
end
|
25
|
+
|
26
26
|
def test_set_default_formats!
|
27
27
|
t = Workbook::Template.new
|
28
28
|
t.set_default_formats!
|
29
|
-
assert_equal({font_weight: "bold"},t.formats[:header][:default])
|
29
|
+
assert_equal({font_weight: "bold"}, t.formats[:header][:default])
|
30
30
|
end
|
31
|
+
|
31
32
|
def test_add_formats
|
32
33
|
t = Workbook::Template.new
|
33
|
-
t.add_format Workbook::Format.new({font:"Arial"})
|
34
|
-
t.add_format Workbook::Format.new({font:"Times"})
|
35
|
-
assert_equal(2,t.formats.keys.count)
|
36
|
-
named_format = Workbook::Format.new({font:"Times"})
|
34
|
+
t.add_format Workbook::Format.new({font: "Arial"})
|
35
|
+
t.add_format Workbook::Format.new({font: "Times"})
|
36
|
+
assert_equal(2, t.formats.keys.count)
|
37
|
+
named_format = Workbook::Format.new({font: "Times"})
|
37
38
|
named_format.name = 1
|
38
39
|
t.add_format named_format
|
39
|
-
assert_equal(2,t.formats.keys.count)
|
40
|
+
assert_equal(2, t.formats.keys.count)
|
40
41
|
end
|
41
42
|
end
|
data/test/test_types_date.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
5
4
|
|
6
5
|
class TestTypesDate < Minitest::Test
|
7
6
|
def test_init
|
8
|
-
w = Workbook::Types::Date.new(2001,2,2)
|
7
|
+
w = Workbook::Types::Date.new(2001, 2, 2)
|
9
8
|
assert_equal(Workbook::Types::Date, w.class)
|
10
|
-
assert_equal(Date.new(2001,2,2),w)
|
11
|
-
assert_equal(Date.new(2001,2,2),w.value)
|
9
|
+
assert_equal(Date.new(2001, 2, 2), w)
|
10
|
+
assert_equal(Date.new(2001, 2, 2), w.value)
|
12
11
|
assert_equal(true, w.is_a?(Date))
|
13
12
|
assert_equal(true, w.is_a?(Workbook::Modules::Cell))
|
14
|
-
|
15
13
|
end
|
16
14
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
4
|
+
module Readers
|
5
|
+
class TestCsvWriter < Minitest::Test
|
6
|
+
def test_to_csv
|
7
|
+
w = Workbook::Book.new
|
8
|
+
w.import File.join(File.dirname(__FILE__), "artifacts/simple_csv.csv")
|
9
|
+
# reads
|
10
|
+
# a,b,c,d
|
11
|
+
# 1,2,3,4
|
12
|
+
# 5,3,2,1
|
13
|
+
# "asdf",123,12,2001-02-02
|
14
|
+
#
|
15
|
+
assert_equal("untitled document.csv", w.sheet.table.write_to_csv)
|
16
|
+
csv_result = File.read("untitled document.csv").split("\n")
|
17
|
+
csv_original = File.read(File.join(File.dirname(__FILE__), "artifacts/simple_csv.csv")).split("\n")
|
18
|
+
assert_equal(csv_original[0], csv_result[0])
|
19
|
+
assert_equal(csv_original[1], csv_result[1])
|
20
|
+
assert_equal(csv_original[2], csv_result[2])
|
21
|
+
assert_equal(csv_original[3], csv_result[3])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,75 +1,76 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
5
4
|
|
6
5
|
module Writers
|
7
6
|
class TestHtmlWriter < Minitest::Test
|
8
7
|
def test_to_html
|
9
|
-
#jruby and ruby's output differ a bit... both produce valid results though
|
8
|
+
# jruby and ruby's output differ a bit... both produce valid results though
|
10
9
|
# match = Workbook::Book.new.to_html.match(/<table \/>/) ? true : false #jruby
|
11
10
|
# puts Workbook::Book.new.to_html
|
12
11
|
# match = (Workbook::Book.new.to_html.match(/<table><thead><\/thead><tbody><\/tbody><\/table>/) ? true : false) if match == false #ruby
|
13
12
|
# assert_equal(true, match)
|
14
|
-
html = Workbook::Book.new([[
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
assert_equal(true, match)
|
19
|
-
match = html.match(/<th class=\"a\" data-key=\"a\">a<\/th>/) ? true : false
|
20
|
-
assert_equal(true, match)
|
13
|
+
html = Workbook::Book.new([["a", "b"], [1, 2], [3, 4]]).to_html
|
14
|
+
|
15
|
+
assert_match(/<td>1<\/td>/, html)
|
16
|
+
assert_match(/<th class="a" data-key="a">a<\/th>/, html)
|
21
17
|
end
|
18
|
+
|
22
19
|
def test_to_html_format_names
|
23
|
-
b = Workbook::Book.new([[
|
20
|
+
b = Workbook::Book.new([["a", "b"], [1, 2], [3, 4]])
|
24
21
|
c = b[0][0][0][0]
|
25
|
-
c.format.name="testname"
|
22
|
+
c.format.name = "testname"
|
26
23
|
c = b[0][0][1][0]
|
27
|
-
c.format.name="testname"
|
24
|
+
c.format.name = "testname"
|
25
|
+
|
28
26
|
html = b.to_html
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
assert_equal(true, match)
|
27
|
+
|
28
|
+
assert_match(/<th class="testname a" data-key="a">a<\/th>/, html)
|
29
|
+
assert_match(/<td class="testname">1<\/td>/, html)
|
33
30
|
end
|
31
|
+
|
34
32
|
def test_build_cell_options
|
35
|
-
b = Workbook::Book.new([[
|
36
|
-
result = b.sheet.table.build_cell_options(b.sheet.table.first.first,{data: {a:"a"}})
|
37
|
-
|
33
|
+
b = Workbook::Book.new([["a", "b"], [1, 2], [3, 4]])
|
34
|
+
result = b.sheet.table.build_cell_options(b.sheet.table.first.first, {data: {a: "a"}})
|
35
|
+
|
36
|
+
assert_equal("a", result[:"data-a"])
|
38
37
|
end
|
38
|
+
|
39
39
|
def test_to_html_css
|
40
|
-
b = Workbook::Book.new([[
|
40
|
+
b = Workbook::Book.new([["a", "b"], [1, 2], [3, 4]])
|
41
41
|
c = b[0][0][0][0]
|
42
|
-
c.format[:background]="#f00"
|
42
|
+
c.format[:background] = "#f00"
|
43
43
|
c = b[0][0][1][0]
|
44
|
-
c.format[:background]="#ff0"
|
44
|
+
c.format[:background] = "#ff0"
|
45
|
+
|
45
46
|
html = b.to_html
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
assert_equal(true, match)
|
53
|
-
match = html.match(/<td style="background: #ff0">1<\/td>/) ? true : false
|
54
|
-
assert_equal(true, match)
|
47
|
+
assert_match(/<th class="a" data-key="a">a<\/th>/, html)
|
48
|
+
assert_match(/<td>1<\/td>/, html)
|
49
|
+
|
50
|
+
html = b.to_html({style_with_inline_css: true})
|
51
|
+
assert_match(/<th class="a" data-key="a" style="background: #f00">a<\/th>/, html)
|
52
|
+
assert_match(/<td style="background: #ff0">1<\/td>/, html)
|
55
53
|
end
|
54
|
+
|
56
55
|
def test_sheet_and_table_names
|
57
|
-
b = Workbook::Book.new([[
|
56
|
+
b = Workbook::Book.new([["a", "b"], [1, 2], [3, 4]])
|
58
57
|
b.sheet.name = "Sheet name"
|
59
58
|
b.sheet.table.name = "Table name"
|
59
|
+
|
60
60
|
html = b.to_html
|
61
|
-
|
62
|
-
|
61
|
+
|
62
|
+
assert_match(/<h1>Sheet name<\/h1>/, html)
|
63
|
+
assert_match(/<h2>Table name<\/h2>/, html)
|
63
64
|
end
|
65
|
+
|
64
66
|
def test_col_and_rowspans
|
65
67
|
w = Workbook::Book.new
|
66
|
-
w.import File.join(File.dirname(__FILE__),
|
68
|
+
w.import File.join(File.dirname(__FILE__), "artifacts/sheet_with_combined_cells.ods")
|
67
69
|
html = w.to_html
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
70
|
+
assert_match(/rowspan="2">15 nov 11 15 nov 11/, html)
|
71
|
+
|
72
|
+
assert_match(/colspan="2" rowspan="2">13 mrt 12 15 mrt 12 13 mrt 12 15 mrt 12/, html)
|
73
|
+
assert_match(/colspan="2">14 90589/, html)
|
73
74
|
end
|
74
75
|
end
|
75
76
|
end
|
@@ -1,21 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'helper')
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
5
4
|
|
6
5
|
module Writers
|
7
6
|
class TestJsonWriter < Minitest::Test
|
8
7
|
def test_to_array_of_hashes_with_values
|
9
|
-
assert_equal([],Workbook::Table.new.to_array_of_hashes_with_values)
|
10
|
-
assert_equal([],Workbook::Table.new([["a","b"]]).to_array_of_hashes_with_values)
|
11
|
-
assert_equal([{:
|
12
|
-
Workbook::Table.new([["a","b"],[1,2],[Date.new(2012,1,1),nil]]).to_array_of_hashes_with_values)
|
8
|
+
assert_equal([], Workbook::Table.new.to_array_of_hashes_with_values)
|
9
|
+
assert_equal([], Workbook::Table.new([["a", "b"]]).to_array_of_hashes_with_values)
|
10
|
+
assert_equal([{a: 1, b: 2}, {a: Date.new(2012, 1, 1), b: nil}],
|
11
|
+
Workbook::Table.new([["a", "b"], [1, 2], [Date.new(2012, 1, 1), nil]]).to_array_of_hashes_with_values)
|
13
12
|
end
|
13
|
+
|
14
14
|
def test_to_json
|
15
|
-
assert_equal("[]",Workbook::Table.new.to_json)
|
16
|
-
assert_equal("[]",Workbook::Table.new([["a","b"]]).to_json)
|
15
|
+
assert_equal("[]", Workbook::Table.new.to_json)
|
16
|
+
assert_equal("[]", Workbook::Table.new([["a", "b"]]).to_json)
|
17
17
|
assert_equal("[{\"a\":1,\"b\":2},{\"a\":\"2012-01-01\",\"b\":null}]",
|
18
|
-
Workbook::Table.new([["a","b"],[1,2],[Date.new(2012,1,1),nil]]).to_json)
|
18
|
+
Workbook::Table.new([["a", "b"], [1, 2], [Date.new(2012, 1, 1), nil]]).to_json)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_write_to_json
|
22
|
+
filename = Workbook::Table.new([["a", "b"], [1, 2], [Date.new(2012, 1, 1), nil]]).write_to_json("json_test.json")
|
23
|
+
json_written = File.read(filename)
|
24
|
+
assert_equal("[{\"a\":1,\"b\":2},{\"a\":\"2012-01-01\",\"b\":null}]",json_written)
|
25
|
+
|
19
26
|
end
|
20
27
|
end
|
21
28
|
end
|