roo 2.7.0 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ require "test_helper"
2
+
3
+ class TestRooBase < Minitest::Test
4
+ def test_info
5
+ # NOTE: unfortunately, the ods and xlsx versions of numbers1 are not
6
+ # identical, so this test fails for Open Office.
7
+ expected_templ = File.read("#{TESTDIR}/expected_results/numbers_info.yml")
8
+ with_each_spreadsheet(name: "numbers1", format: [:excelx]) do |workbook|
9
+ ext = get_extension(workbook)
10
+ expected = Kernel.format(expected_templ, ext)
11
+ assert_equal expected.strip, workbook.info.strip
12
+ end
13
+ end
14
+
15
+ def test_column
16
+ with_each_spreadsheet(name: "numbers1") do |workbook|
17
+ expected = [1.0, 5.0, nil, 10.0, Date.new(1961, 11, 21), "tata", nil, nil, nil, nil, "thisisa11", 41.0, nil, nil, 41.0, "einundvierzig", nil, Date.new(2007, 5, 31)]
18
+ assert_equal expected, workbook.column(1)
19
+ assert_equal expected, workbook.column("a")
20
+ end
21
+ end
22
+
23
+ def test_column_huge_document
24
+ skip_long_test
25
+ with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
26
+ workbook.default_sheet = workbook.sheets.first
27
+ assert_equal 3735, workbook.column("a").size
28
+ end
29
+ end
30
+
31
+ def test_simple_spreadsheet_find_by_condition
32
+ with_each_spreadsheet(name: "simple_spreadsheet") do |workbook|
33
+ workbook.header_line = 3
34
+ results = workbook.find(:all, conditions: { "Comment" => "Task 1" })
35
+ assert_equal Date.new(2007, 05, 07), results[1]["Date"]
36
+ assert_equal 10.75, results[1]["Start time"]
37
+ assert_equal 12.50, results[1]["End time"]
38
+ assert_equal 0, results[1]["Pause"]
39
+ assert_equal 1.75, results[1]["Sum"]
40
+ assert_equal "Task 1", results[1]["Comment"]
41
+ end
42
+ end
43
+
44
+ def test_bug_bbu
45
+ expected_templ = File.read("#{TESTDIR}/expected_results/bbu_info.txt")
46
+ with_each_spreadsheet(name: "bbu", format: [:openoffice, :excelx]) do |workbook|
47
+ ext = get_extension(workbook)
48
+ expected_result = Kernel.format(expected_templ, ext)
49
+ assert_equal expected_result.strip, workbook.info.strip
50
+
51
+ workbook.default_sheet = workbook.sheets[1] # empty sheet
52
+ assert_nil workbook.first_row
53
+ assert_nil workbook.last_row
54
+ assert_nil workbook.first_column
55
+ assert_nil workbook.last_column
56
+ end
57
+ end
58
+
59
+ def test_find_by_row_huge_document
60
+ skip_long_test
61
+ options = { name: "Bibelbund", format: [:openoffice, :excelx] }
62
+ with_each_spreadsheet(options) do |workbook|
63
+ workbook.default_sheet = workbook.sheets.first
64
+ result = workbook.find 20
65
+ assert result
66
+ assert_equal "Brief aus dem Sekretariat", result[0]["TITEL"]
67
+
68
+ result = workbook.find 22
69
+ assert result
70
+ assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.", result[0]["TITEL"]
71
+ end
72
+ end
73
+
74
+ def test_find_by_row
75
+ with_each_spreadsheet(name: "numbers1") do |workbook|
76
+ workbook.header_line = nil
77
+ result = workbook.find 16
78
+ assert result
79
+ assert_nil workbook.header_line
80
+ # keine Headerlines in diesem Beispiel definiert
81
+ assert_equal "einundvierzig", result[0]
82
+ # assert_equal false, results
83
+ result = workbook.find 15
84
+ assert result
85
+ assert_equal 41, result[0]
86
+ end
87
+ end
88
+
89
+ def test_find_by_row_if_header_line_is_not_nil
90
+ with_each_spreadsheet(name: "numbers1") do |workbook|
91
+ workbook.header_line = 2
92
+ refute_nil workbook.header_line
93
+ results = workbook.find 1
94
+ assert results
95
+ assert_equal 5, results[0]
96
+ assert_equal 6, results[1]
97
+ results = workbook.find 15
98
+ assert results
99
+ assert_equal "einundvierzig", results[0]
100
+ end
101
+ end
102
+
103
+ def test_find_by_conditions
104
+ skip_long_test
105
+ expected_results = [
106
+ {
107
+ "VERFASSER" => "Almassy, Annelene von",
108
+ "INTERNET" => nil,
109
+ "SEITE" => 316.0,
110
+ "KENNUNG" => "Aus dem Bibelbund",
111
+ "OBJEKT" => "Bibel+Gem",
112
+ "PC" => "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
113
+ "NUMMER" => "1982-3",
114
+ "TITEL" => "Brief aus dem Sekretariat"
115
+ },
116
+ {
117
+ "VERFASSER" => "Almassy, Annelene von",
118
+ "INTERNET" => nil,
119
+ "SEITE" => 222.0,
120
+ "KENNUNG" => "Aus dem Bibelbund",
121
+ "OBJEKT" => "Bibel+Gem",
122
+ "PC" => "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
123
+ "NUMMER" => "1983-2",
124
+ "TITEL" => "Brief aus dem Sekretariat"
125
+ }
126
+ ]
127
+
128
+ expected_results_size = 2
129
+ options = { name: "Bibelbund", format: [:openoffice, :excelx] }
130
+ with_each_spreadsheet(options) do |workbook|
131
+ results = workbook.find(:all, conditions: { "TITEL" => "Brief aus dem Sekretariat" })
132
+ assert_equal expected_results_size, results.size
133
+ assert_equal expected_results, results
134
+
135
+ conditions = {
136
+ "TITEL" => "Brief aus dem Sekretariat",
137
+ "VERFASSER" => "Almassy, Annelene von"
138
+ }
139
+ results = workbook.find(:all, conditions: conditions)
140
+ assert_equal expected_results, results
141
+ assert_equal expected_results_size, results.size
142
+
143
+ results = workbook.find(:all, conditions: { "VERFASSER" => "Almassy, Annelene von" })
144
+ assert_equal 13, results.size
145
+ end
146
+ end
147
+
148
+ def test_find_by_conditions_with_array_option
149
+ expected_results = [
150
+ [
151
+ "Brief aus dem Sekretariat",
152
+ "Almassy, Annelene von",
153
+ "Bibel+Gem",
154
+ "1982-3",
155
+ 316.0,
156
+ nil,
157
+ "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
158
+ "Aus dem Bibelbund",
159
+ ],
160
+ [
161
+ "Brief aus dem Sekretariat",
162
+ "Almassy, Annelene von",
163
+ "Bibel+Gem",
164
+ "1983-2",
165
+ 222.0,
166
+ nil,
167
+ "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
168
+ "Aus dem Bibelbund",
169
+ ]
170
+ ]
171
+ options = { name: "Bibelbund", format: [:openoffice, :excelx] }
172
+ with_each_spreadsheet(options) do |workbook|
173
+ conditions = {
174
+ "TITEL" => "Brief aus dem Sekretariat",
175
+ "VERFASSER" => "Almassy, Annelene von"
176
+ }
177
+ results = workbook.find(:all, conditions: conditions, array: true)
178
+ assert_equal 2, results.size
179
+ assert_equal expected_results, results
180
+ end
181
+ end
182
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class TestRooCSV < Minitest::Test
4
4
  def test_sheets
@@ -42,6 +42,14 @@ class TestRooCSV < Minitest::Test
42
42
  assert_equal headers, parsed[1].keys
43
43
  end
44
44
 
45
+ def test_iso_8859_1
46
+ file = File.open(File.join(TESTDIR, "iso_8859_1.csv"))
47
+ options = { csv_options: { col_sep: ";", encoding: Encoding::ISO_8859_1 } }
48
+ workbook = Roo::CSV.new(file.path, options)
49
+ result = workbook.last_column
50
+ assert_equal(19, result)
51
+ end
52
+
45
53
  def roo_class
46
54
  Roo::CSV
47
55
  end
@@ -1,6 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
- class TestRooExcelx < Minitest::Test
3
+ class TestRworkbookExcelx < Minitest::Test
4
4
  def test_download_uri_with_invalid_host
5
5
  assert_raises(RuntimeError) do
6
6
  Roo::Excelx.new("http://example.com/file.xlsx")
@@ -20,20 +20,20 @@ class TestRooExcelx < Minitest::Test
20
20
 
21
21
  def test_should_raise_file_not_found_error
22
22
  assert_raises(IOError) do
23
- Roo::Excelx.new(File.join("testnichtvorhanden", "Bibelbund.xlsx"))
23
+ roo_class.new(File.join("testnichtvorhanden", "Bibelbund.xlsx"))
24
24
  end
25
25
  end
26
26
 
27
27
  def test_file_warning_default
28
- assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR, "numbers1.ods")) }
29
- assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR, "numbers1.xls")) }
28
+ assert_raises(TypeError) { roo_class.new(File.join(TESTDIR, "numbers1.ods")) }
29
+ assert_raises(TypeError) { roo_class.new(File.join(TESTDIR, "numbers1.xls")) }
30
30
  end
31
31
 
32
32
  def test_file_warning_error
33
33
  %w(ods xls).each do |extension|
34
34
  assert_raises(TypeError) do
35
35
  options = { packed: false, file_warning: :error }
36
- Roo::Excelx.new(File.join(TESTDIR, "numbers1. #{extension}"), options)
36
+ roo_class.new(File.join(TESTDIR, "numbers1. #{extension}"), options)
37
37
  end
38
38
  end
39
39
  end
@@ -41,13 +41,13 @@ class TestRooExcelx < Minitest::Test
41
41
  def test_file_warning_warning
42
42
  options = { packed: false, file_warning: :warning }
43
43
  assert_raises(ArgumentError) do
44
- Roo::Excelx.new(File.join(TESTDIR, "numbers1.ods"), options)
44
+ roo_class.new(File.join(TESTDIR, "numbers1.ods"), options)
45
45
  end
46
46
  end
47
47
 
48
48
  def test_file_warning_ignore
49
49
  options = { packed: false, file_warning: :ignore }
50
- sheet = Roo::Excelx.new(File.join(TESTDIR, "type_excelx.ods"), options)
50
+ sheet = roo_class.new(File.join(TESTDIR, "type_excelx.ods"), options)
51
51
  assert sheet, "Should not throw an error"
52
52
  end
53
53
 
@@ -66,13 +66,13 @@ class TestRooExcelx < Minitest::Test
66
66
  # where the expected result is
67
67
  # "A: TestString"
68
68
  # "B: TestString"
69
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "formula_string_error.xlsx"))
69
+ xlsx = roo_class.new(File.join(TESTDIR, "formula_string_error.xlsx"))
70
70
  assert_equal "Teststring", xlsx.cell("a", 1)
71
71
  assert_equal "Teststring", xlsx.cell("a", 2)
72
72
  end
73
73
 
74
74
  def test_parsing_xslx_from_numbers
75
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "numbers-export.xlsx"))
75
+ xlsx = roo_class.new(File.join(TESTDIR, "numbers-export.xlsx"))
76
76
 
77
77
  xlsx.default_sheet = xlsx.sheets.first
78
78
  assert_equal "Sheet 1", xlsx.cell("a", 1)
@@ -100,7 +100,7 @@ class TestRooExcelx < Minitest::Test
100
100
 
101
101
  def test_expand_merged_range
102
102
  options = { expand_merged_ranges: true }
103
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"), options)
103
+ xlsx = roo_class.new(File.join(TESTDIR, "merged_ranges.xlsx"), options)
104
104
 
105
105
  [
106
106
  {
@@ -134,7 +134,7 @@ class TestRooExcelx < Minitest::Test
134
134
  end
135
135
 
136
136
  def test_noexpand_merged_range
137
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"))
137
+ xlsx = roo_class.new(File.join(TESTDIR, "merged_ranges.xlsx"))
138
138
 
139
139
  [
140
140
  {
@@ -171,11 +171,150 @@ class TestRooExcelx < Minitest::Test
171
171
  file = filename(:numbers1)
172
172
  file_contents = File.read File.join(TESTDIR, file), encoding: "BINARY"
173
173
  stream = StringIO.new(file_contents)
174
- xlsx = Roo::Excelx.new(stream)
174
+ xlsx = roo_class.new(stream)
175
175
  expected_sheet_names = ["Tabelle1", "Name of Sheet 2", "Sheet3", "Sheet4", "Sheet5"]
176
176
  assert_equal expected_sheet_names, xlsx.sheets
177
177
  end
178
178
 
179
+ def test_header_offset
180
+ xlsx = roo_class.new(File.join(TESTDIR, "header_offset.xlsx"))
181
+ data = xlsx.parse(column_1: "Header A1", column_2: "Header B1")
182
+ assert_equal "Data A2", data[0][:column_1]
183
+ assert_equal "Data B2", data[0][:column_2]
184
+ end
185
+
186
+ def test_formula_excelx
187
+ with_each_spreadsheet(name: "formula", format: :excelx) do |workbook|
188
+ assert_equal 1, workbook.cell("A", 1)
189
+ assert_equal 2, workbook.cell("A", 2)
190
+ assert_equal 3, workbook.cell("A", 3)
191
+ assert_equal 4, workbook.cell("A", 4)
192
+ assert_equal 5, workbook.cell("A", 5)
193
+ assert_equal 6, workbook.cell("A", 6)
194
+ assert_equal 21, workbook.cell("A", 7)
195
+ assert_equal :formula, workbook.celltype("A", 7)
196
+ assert_nil workbook.formula("A", 6)
197
+
198
+ expected_result = [
199
+ [7, 1, "SUM(A1:A6)"],
200
+ [7, 2, "SUM($A$1:B6)"],
201
+ ]
202
+ assert_equal expected_result, workbook.formulas(workbook.sheets.first)
203
+
204
+ # setting a cell
205
+ workbook.set("A", 15, 41)
206
+ assert_equal 41, workbook.cell("A", 15)
207
+ workbook.set("A", 16, "41")
208
+ assert_equal "41", workbook.cell("A", 16)
209
+ workbook.set("A", 17, 42.5)
210
+ assert_equal 42.5, workbook.cell("A", 17)
211
+ end
212
+ end
213
+
214
+ # TODO: temporaerer Test
215
+ def test_seiten_als_date
216
+ skip_long_test
217
+
218
+ with_each_spreadsheet(name: "Bibelbund", format: :excelx) do |workbook|
219
+ assert_equal "Bericht aus dem Sekretariat", workbook.cell(13, 1)
220
+ assert_equal "1981-4", workbook.cell(13, "D")
221
+ assert_equal String, workbook.excelx_type(13, "E")[1].class
222
+ assert_equal [:numeric_or_formula, "General"], workbook.excelx_type(13, "E")
223
+ assert_equal "428", workbook.excelx_value(13, "E")
224
+ assert_equal 428.0, workbook.cell(13, "E")
225
+ end
226
+ end
227
+
228
+ def test_bug_simple_spreadsheet_time_bug
229
+ # really a bug? are cells really of type time?
230
+ # No! :float must be the correct type
231
+ with_each_spreadsheet(name: "simple_spreadsheet", format: :excelx) do |workbook|
232
+ # puts workbook.cell("B", 5).to_s
233
+ # assert_equal :time, workbook.celltype("B", 5)
234
+ assert_equal :float, workbook.celltype("B", 5)
235
+ assert_equal 10.75, workbook.cell("B", 5)
236
+ assert_equal 12.50, workbook.cell("C", 5)
237
+ assert_equal 0, workbook.cell("D", 5)
238
+ assert_equal 1.75, workbook.cell("E", 5)
239
+ assert_equal "Task 1", workbook.cell("F", 5)
240
+ assert_equal Date.new(2007, 5, 7), workbook.cell("A", 5)
241
+ end
242
+ end
243
+
244
+ def test_simple2_excelx
245
+ with_each_spreadsheet(name: "simple_spreadsheet", format: :excelx) do |workbook|
246
+ assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], workbook.excelx_type("A", 4)
247
+ assert_equal [:numeric_or_formula, "#,##0.00"], workbook.excelx_type("B", 4)
248
+ assert_equal [:numeric_or_formula, "#,##0.00"], workbook.excelx_type("c", 4)
249
+ assert_equal [:numeric_or_formula, "General"], workbook.excelx_type("d", 4)
250
+ assert_equal [:numeric_or_formula, "General"], workbook.excelx_type("e", 4)
251
+ assert_equal :string, workbook.excelx_type("f", 4)
252
+
253
+ assert_equal "39209", workbook.excelx_value("a", 4)
254
+ assert_equal "yyyy\\-mm\\-dd", workbook.excelx_format("a", 4)
255
+ assert_equal "9.25", workbook.excelx_value("b", 4)
256
+ assert_equal "10.25", workbook.excelx_value("c", 4)
257
+ assert_equal "0", workbook.excelx_value("d", 4)
258
+ # ... Sum-Spalte
259
+ # assert_equal "Task 1", workbook.excelx_value("f", 4)
260
+ assert_equal "Task 1", workbook.cell("f", 4)
261
+ assert_equal Date.new(2007, 05, 07), workbook.cell("a", 4)
262
+ assert_equal "9.25", workbook.excelx_value("b", 4)
263
+ assert_equal "#,##0.00", workbook.excelx_format("b", 4)
264
+ assert_equal 9.25, workbook.cell("b", 4)
265
+ assert_equal :float, workbook.celltype("b", 4)
266
+ assert_equal :float, workbook.celltype("d", 4)
267
+ assert_equal 0, workbook.cell("d", 4)
268
+ assert_equal :formula, workbook.celltype("e", 4)
269
+ assert_equal 1, workbook.cell("e", 4)
270
+ assert_equal "C4-B4-D4", workbook.formula("e", 4)
271
+ assert_equal :string, workbook.celltype("f", 4)
272
+ assert_equal "Task 1", workbook.cell("f", 4)
273
+ end
274
+ end
275
+
276
+ def test_bug_pfand_from_windows_phone_xlsx
277
+ # skip_jruby_incompatible_test
278
+ # TODO: Does JRUBY need to skip this test
279
+ return if defined? JRUBY_VERSION
280
+
281
+ options = { name: "Pfand_from_windows_phone", format: :excelx }
282
+ with_each_spreadsheet(options) do |workbook|
283
+ workbook.default_sheet = workbook.sheets.first
284
+ assert_equal ["Blatt1", "Blatt2", "Blatt3"], workbook.sheets
285
+ assert_equal "Summe", workbook.cell("b", 1)
286
+
287
+ assert_equal Date.new(2011, 9, 14), workbook.cell("a", 2)
288
+ assert_equal :date, workbook.celltype("a", 2)
289
+ assert_equal Date.new(2011, 9, 15), workbook.cell("a", 3)
290
+ assert_equal :date, workbook.celltype("a", 3)
291
+
292
+ assert_equal 3.81, workbook.cell("b", 2)
293
+ assert_equal "SUM(C2:L2)", workbook.formula("b", 2)
294
+ assert_equal 0.7, workbook.cell("c", 2)
295
+ end # each
296
+ end
297
+
298
+ def test_excelx_links
299
+ with_each_spreadsheet(name: "link", format: :excelx) do |workbook|
300
+ assert_equal "Google", workbook.cell(1, 1)
301
+ assert_equal "http://www.google.com", workbook.cell(1, 1).href
302
+ end
303
+ end
304
+
305
+ # Excel has two base date formats one from 1900 and the other from 1904.
306
+ # see #test_base_dates_in_excel
307
+ def test_base_dates_in_excelx
308
+ with_each_spreadsheet(name: "1900_base", format: :excelx) do |workbook|
309
+ assert_equal Date.new(2009, 06, 15), workbook.cell(1, 1)
310
+ assert_equal :date, workbook.celltype(1, 1)
311
+ end
312
+ with_each_spreadsheet(name: "1904_base", format: :excelx) do |workbook|
313
+ assert_equal Date.new(2009, 06, 15), workbook.cell(1, 1)
314
+ assert_equal :date, workbook.celltype(1, 1)
315
+ end
316
+ end
317
+
179
318
  def roo_class
180
319
  Roo::Excelx
181
320
  end
@@ -8,8 +8,8 @@ class TestRooOpenOffice < Minitest::Test
8
8
  start_local_server(file, port) do
9
9
  url = "#{local_server(port)}/#{file}"
10
10
 
11
- oo = roo_class.new(url, packed: :zip)
12
- assert_in_delta 0.001, 505.14, oo.cell("c", 33).to_f
11
+ workbook = roo_class.new(url, packed: :zip)
12
+ assert_in_delta 0.001, 505.14, workbook.cell("c", 33).to_f
13
13
  end
14
14
  end
15
15
 
@@ -30,9 +30,9 @@ class TestRooOpenOffice < Minitest::Test
30
30
  end
31
31
 
32
32
  def test_openoffice_zipped
33
- oo = roo_class.new(File.join(TESTDIR, "bode-v1.ods.zip"), packed: :zip)
34
- assert oo
35
- assert_equal 'ist "e" im Nenner von H(s)', oo.cell("b", 5)
33
+ workbook = roo_class.new(File.join(TESTDIR, "bode-v1.ods.zip"), packed: :zip)
34
+ assert workbook
35
+ assert_equal 'ist "e" im Nenner von H(s)', workbook.cell("b", 5)
36
36
  end
37
37
 
38
38
  def test_should_raise_file_not_found_error
@@ -41,7 +41,6 @@ class TestRooOpenOffice < Minitest::Test
41
41
  end
42
42
  end
43
43
 
44
-
45
44
  def test_file_warning_default_is_error
46
45
  expected_message = "test/files/numbers1.xls is not an openoffice spreadsheet"
47
46
  assert_raises(TypeError, expected_message) do
@@ -54,45 +53,44 @@ class TestRooOpenOffice < Minitest::Test
54
53
  end
55
54
 
56
55
  def test_file_warning_error
56
+ options = { packed: false, file_warning: :error }
57
+
57
58
  assert_raises(TypeError) do
58
- roo_class.new(File.join(TESTDIR, "numbers1.xls"),
59
- packed: false,
60
- file_warning: :error
61
- )
59
+ roo_class.new(File.join(TESTDIR, "numbers1.xls"), options)
62
60
  end
63
61
 
64
62
  assert_raises(TypeError) do
65
- roo_class.new(File.join(TESTDIR, "numbers1.xlsx"),
66
- packed: false,
67
- file_warning: :error)
63
+ roo_class.new(File.join(TESTDIR, "numbers1.xlsx"), options)
68
64
  end
69
65
  end
70
66
 
71
67
  def test_file_warning_warning
72
68
  assert_raises(ArgumentError) do
73
- roo_class.new(File.join(TESTDIR, "numbers1.xlsx"),
74
- packed: false,
75
- file_warning: :warning)
69
+ options = { packed: false, file_warning: :warning }
70
+ roo_class.new(File.join(TESTDIR, "numbers1.xlsx"), options)
76
71
  end
77
72
  end
78
73
 
79
74
  def test_file_warning_ignore
80
- assert roo_class.new(File.join(TESTDIR, "type_openoffice.xlsx"),
81
- packed: false,
82
- file_warning: :ignore), "Should not throw an error"
75
+ options = { packed: false, file_warning: :ignore }
76
+ assert roo_class.new(File.join(TESTDIR, "type_openoffice.xlsx"), options), "Should not throw an error"
83
77
  end
84
78
 
85
79
  def test_encrypted_file
86
- oo = roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "letmein")
87
- assert_equal "Hello World", oo.cell("a", 1)
80
+ workbook = roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "letmein")
81
+ assert_equal "Hello World", workbook.cell("a", 1)
88
82
  end
89
83
 
90
84
  def test_encrypted_file_requires_password
91
- assert_raises(ArgumentError) { roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods")) }
85
+ assert_raises(ArgumentError) do
86
+ roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"))
87
+ end
92
88
  end
93
89
 
94
90
  def test_encrypted_file_with_incorrect_password
95
- assert_raises(ArgumentError) { roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "badpassword") }
91
+ assert_raises(ArgumentError) do
92
+ roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "badpassword")
93
+ end
96
94
  end
97
95
 
98
96
  # 2011-08-11
@@ -104,16 +102,181 @@ class TestRooOpenOffice < Minitest::Test
104
102
  # Bei den OpenOffice-Dateien ist in diesem Feld in der XML-
105
103
  # Datei of: als Prefix enthalten, waehrend in dieser Datei
106
104
  # irgendetwas mit oooc: als Prefix verwendet wird.
107
- oo = Roo::OpenOffice.new(File.join(TESTDIR,'dreimalvier.ods'))
108
- assert_equal '=SUM([.A1:.D1])', oo.formula('e',1)
109
- assert_equal '=SUM([.A2:.D2])', oo.formula('e',2)
110
- assert_equal '=SUM([.A3:.D3])', oo.formula('e',3)
111
- assert_equal [
112
- [1,5,'=SUM([.A1:.D1])'],
113
- [2,5,'=SUM([.A2:.D2])'],
114
- [3,5,'=SUM([.A3:.D3])'],
115
- ], oo.formulas
105
+ workbook = roo_class.new(File.join(TESTDIR, "dreimalvier.ods"))
106
+ assert_equal "=SUM([.A1:.D1])", workbook.formula("e", 1)
107
+ assert_equal "=SUM([.A2:.D2])", workbook.formula("e", 2)
108
+ assert_equal "=SUM([.A3:.D3])", workbook.formula("e", 3)
109
+ expected_formulas = [
110
+ [1, 5, "=SUM([.A1:.D1])"],
111
+ [2, 5, "=SUM([.A2:.D2])"],
112
+ [3, 5, "=SUM([.A3:.D3])"],
113
+ ]
114
+ assert_equal expected_formulas, workbook.formulas
115
+ end
116
+
117
+ def test_header_with_brackets_open_office
118
+ options = { name: "advanced_header", format: :openoffice }
119
+ with_each_spreadsheet(options) do |workbook|
120
+ parsed_head = workbook.parse(headers: true)
121
+ assert_equal "Date(yyyy-mm-dd)", workbook.cell("A", 1)
122
+ assert_equal parsed_head[0].keys, ["Date(yyyy-mm-dd)"]
123
+ assert_equal parsed_head[0].values, ["Date(yyyy-mm-dd)"]
124
+ end
125
+ end
126
+
127
+ def test_office_version
128
+ with_each_spreadsheet(name: "numbers1", format: :openoffice) do |workbook|
129
+ assert_equal "1.0", workbook.officeversion
130
+ end
131
+ end
132
+
133
+ def test_bug_contiguous_cells
134
+ with_each_spreadsheet(name: "numbers1", format: :openoffice) do |workbook|
135
+ workbook.default_sheet = "Sheet4"
136
+ assert_equal Date.new(2007, 06, 16), workbook.cell("a", 1)
137
+ assert_equal 10, workbook.cell("b", 1)
138
+ assert_equal 10, workbook.cell("c", 1)
139
+ assert_equal 10, workbook.cell("d", 1)
140
+ assert_equal 10, workbook.cell("e", 1)
141
+ end
142
+ end
143
+
144
+ def test_italo_table
145
+ with_each_spreadsheet(name: "simple_spreadsheet_from_italo", format: :openoffice) do |workbook|
146
+ assert_equal "1", workbook.cell("A", 1)
147
+ assert_equal "1", workbook.cell("B", 1)
148
+ assert_equal "1", workbook.cell("C", 1)
149
+ assert_equal 1, workbook.cell("A", 2).to_i
150
+ assert_equal 2, workbook.cell("B", 2).to_i
151
+ assert_equal 1, workbook.cell("C", 2).to_i
152
+ assert_equal 1, workbook.cell("A", 3)
153
+ assert_equal 3, workbook.cell("B", 3)
154
+ assert_equal 1, workbook.cell("C", 3)
155
+ assert_equal "A", workbook.cell("A", 4)
156
+ assert_equal "A", workbook.cell("B", 4)
157
+ assert_equal "A", workbook.cell("C", 4)
158
+ assert_equal 0.01, workbook.cell("A", 5)
159
+ assert_equal 0.01, workbook.cell("B", 5)
160
+ assert_equal 0.01, workbook.cell("C", 5)
161
+ assert_equal 0.03, workbook.cell("a", 5) + workbook.cell("b", 5) + workbook.cell("c", 5)
162
+
163
+ # Cells values in row 1:
164
+ assert_equal "1:string", [workbook.cell(1, 1), workbook.celltype(1, 1)].join(":")
165
+ assert_equal "1:string", [workbook.cell(1, 2), workbook.celltype(1, 2)].join(":")
166
+ assert_equal "1:string", [workbook.cell(1, 3), workbook.celltype(1, 3)].join(":")
116
167
 
168
+ # Cells values in row 2:
169
+ assert_equal "1:string", [workbook.cell(2, 1), workbook.celltype(2, 1)].join(":")
170
+ assert_equal "2:string", [workbook.cell(2, 2), workbook.celltype(2, 2)].join(":")
171
+ assert_equal "1:string", [workbook.cell(2, 3), workbook.celltype(2, 3)].join(":")
172
+
173
+ # Cells values in row 3:
174
+ assert_equal "1:float", [workbook.cell(3, 1), workbook.celltype(3, 1)].join(":")
175
+ assert_equal "3:float", [workbook.cell(3, 2), workbook.celltype(3, 2)].join(":")
176
+ assert_equal "1:float", [workbook.cell(3, 3), workbook.celltype(3, 3)].join(":")
177
+
178
+ # Cells values in row 4:
179
+ assert_equal "A:string", [workbook.cell(4, 1), workbook.celltype(4, 1)].join(":")
180
+ assert_equal "A:string", [workbook.cell(4, 2), workbook.celltype(4, 2)].join(":")
181
+ assert_equal "A:string", [workbook.cell(4, 3), workbook.celltype(4, 3)].join(":")
182
+
183
+ # Cells values in row 5:
184
+ assert_equal "0.01:percentage", [workbook.cell(5, 1), workbook.celltype(5, 1)].join(":")
185
+ assert_equal "0.01:percentage", [workbook.cell(5, 2), workbook.celltype(5, 2)].join(":")
186
+ assert_equal "0.01:percentage", [workbook.cell(5, 3), workbook.celltype(5, 3)].join(":")
187
+ end
188
+ end
189
+
190
+ def test_formula_openoffice
191
+ with_each_spreadsheet(name: "formula", format: :openoffice) do |workbook|
192
+ assert_equal 1, workbook.cell("A", 1)
193
+ assert_equal 2, workbook.cell("A", 2)
194
+ assert_equal 3, workbook.cell("A", 3)
195
+ assert_equal 4, workbook.cell("A", 4)
196
+ assert_equal 5, workbook.cell("A", 5)
197
+ assert_equal 6, workbook.cell("A", 6)
198
+ assert_equal 21, workbook.cell("A", 7)
199
+ assert_equal :formula, workbook.celltype("A", 7)
200
+ assert_equal "=[Sheet2.A1]", workbook.formula("C", 7)
201
+ assert_nil workbook.formula("A", 6)
202
+ expected_formulas = [
203
+ [7, 1, "=SUM([.A1:.A6])"],
204
+ [7, 2, "=SUM([.$A$1:.B6])"],
205
+ [7, 3, "=[Sheet2.A1]"],
206
+ [8, 2, "=SUM([.$A$1:.B7])"],
207
+ ]
208
+ assert_equal expected_formulas, workbook.formulas(workbook.sheets.first)
209
+
210
+ # setting a cell
211
+ workbook.set("A", 15, 41)
212
+ assert_equal 41, workbook.cell("A", 15)
213
+ workbook.set("A", 16, "41")
214
+ assert_equal "41", workbook.cell("A", 16)
215
+ workbook.set("A", 17, 42.5)
216
+ assert_equal 42.5, workbook.cell("A", 17)
217
+ end
218
+ end
219
+
220
+ def test_bug_ric
221
+ with_each_spreadsheet(name: "ric", format: :openoffice) do |workbook|
222
+ assert workbook.empty?("A", 1)
223
+ assert workbook.empty?("B", 1)
224
+ assert workbook.empty?("C", 1)
225
+ assert workbook.empty?("D", 1)
226
+ expected = 1
227
+ letter = "e"
228
+ while letter <= "u"
229
+ assert_equal expected, workbook.cell(letter, 1)
230
+ letter.succ!
231
+ expected += 1
232
+ end
233
+ assert_equal "J", workbook.cell("v", 1)
234
+ assert_equal "P", workbook.cell("w", 1)
235
+ assert_equal "B", workbook.cell("x", 1)
236
+ assert_equal "All", workbook.cell("y", 1)
237
+ assert_equal 0, workbook.cell("a", 2)
238
+ assert workbook.empty?("b", 2)
239
+ assert workbook.empty?("c", 2)
240
+ assert workbook.empty?("d", 2)
241
+ assert_equal "B", workbook.cell("e", 2)
242
+ assert_equal "B", workbook.cell("f", 2)
243
+ assert_equal "B", workbook.cell("g", 2)
244
+ assert_equal "B", workbook.cell("h", 2)
245
+ assert_equal "B", workbook.cell("i", 2)
246
+ assert_equal "B", workbook.cell("j", 2)
247
+ assert_equal "B", workbook.cell("k", 2)
248
+ assert_equal "B", workbook.cell("l", 2)
249
+ assert_equal "B", workbook.cell("m", 2)
250
+ assert_equal "B", workbook.cell("n", 2)
251
+ assert_equal "B", workbook.cell("o", 2)
252
+ assert_equal "B", workbook.cell("p", 2)
253
+ assert_equal "B", workbook.cell("q", 2)
254
+ assert_equal "B", workbook.cell("r", 2)
255
+ assert_equal "B", workbook.cell("s", 2)
256
+ assert workbook.empty?("t", 2)
257
+ assert workbook.empty?("u", 2)
258
+ assert_equal 0, workbook.cell("v", 2)
259
+ assert_equal 0, workbook.cell("w", 2)
260
+ assert_equal 15, workbook.cell("x", 2)
261
+ assert_equal 15, workbook.cell("y", 2)
262
+ end
263
+ end
264
+
265
+ def test_mehrteilig
266
+ with_each_spreadsheet(name: "Bibelbund1", format: :openoffice) do |workbook|
267
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", workbook.cell(45, "A")
268
+ end
269
+ end
270
+
271
+ def test_cell_openoffice_html_escape
272
+ with_each_spreadsheet(name: "html-escape", format: :openoffice) do |workbook|
273
+ assert_equal "'", workbook.cell(1, 1)
274
+ assert_equal "&", workbook.cell(2, 1)
275
+ assert_equal ">", workbook.cell(3, 1)
276
+ assert_equal "<", workbook.cell(4, 1)
277
+ assert_equal "`", workbook.cell(5, 1)
278
+ # test_openoffice_zipped will catch issues with &quot;
279
+ end
117
280
  end
118
281
 
119
282
  def roo_class