roo 2.0.1 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.github/ISSUE_TEMPLATE +10 -0
  4. data/.gitignore +4 -0
  5. data/.travis.yml +10 -6
  6. data/CHANGELOG.md +116 -1
  7. data/Gemfile +3 -4
  8. data/Gemfile_ruby2 +30 -0
  9. data/Guardfile +1 -2
  10. data/README.md +56 -22
  11. data/Rakefile +1 -1
  12. data/lib/roo/base.rb +108 -245
  13. data/lib/roo/constants.rb +5 -0
  14. data/lib/roo/csv.rb +94 -87
  15. data/lib/roo/errors.rb +11 -0
  16. data/lib/roo/excelx/cell/base.rb +94 -0
  17. data/lib/roo/excelx/cell/boolean.rb +27 -0
  18. data/lib/roo/excelx/cell/date.rb +28 -0
  19. data/lib/roo/excelx/cell/datetime.rb +111 -0
  20. data/lib/roo/excelx/cell/empty.rb +19 -0
  21. data/lib/roo/excelx/cell/number.rb +87 -0
  22. data/lib/roo/excelx/cell/string.rb +19 -0
  23. data/lib/roo/excelx/cell/time.rb +43 -0
  24. data/lib/roo/excelx/cell.rb +33 -4
  25. data/lib/roo/excelx/comments.rb +33 -0
  26. data/lib/roo/excelx/coordinate.rb +12 -0
  27. data/lib/roo/excelx/extractor.rb +3 -4
  28. data/lib/roo/excelx/format.rb +64 -0
  29. data/lib/roo/excelx/shared.rb +32 -0
  30. data/lib/roo/excelx/shared_strings.rb +124 -4
  31. data/lib/roo/excelx/sheet.rb +12 -7
  32. data/lib/roo/excelx/sheet_doc.rb +108 -97
  33. data/lib/roo/excelx/styles.rb +1 -1
  34. data/lib/roo/excelx.rb +61 -103
  35. data/lib/roo/formatters/base.rb +15 -0
  36. data/lib/roo/formatters/csv.rb +84 -0
  37. data/lib/roo/formatters/matrix.rb +23 -0
  38. data/lib/roo/formatters/xml.rb +31 -0
  39. data/lib/roo/formatters/yaml.rb +40 -0
  40. data/lib/roo/libre_office.rb +1 -2
  41. data/lib/roo/link.rb +21 -2
  42. data/lib/roo/open_office.rb +468 -523
  43. data/lib/roo/spreadsheet.rb +3 -1
  44. data/lib/roo/tempdir.rb +21 -0
  45. data/lib/roo/utils.rb +7 -7
  46. data/lib/roo/version.rb +1 -1
  47. data/lib/roo.rb +8 -3
  48. data/roo.gemspec +2 -1
  49. data/spec/helpers.rb +5 -0
  50. data/spec/lib/roo/base_spec.rb +229 -0
  51. data/spec/lib/roo/csv_spec.rb +19 -0
  52. data/spec/lib/roo/excelx_spec.rb +97 -11
  53. data/spec/lib/roo/openoffice_spec.rb +18 -1
  54. data/spec/lib/roo/spreadsheet_spec.rb +20 -0
  55. data/spec/lib/roo/utils_spec.rb +1 -1
  56. data/spec/spec_helper.rb +5 -5
  57. data/test/all_ss.rb +12 -11
  58. data/test/excelx/cell/test_base.rb +63 -0
  59. data/test/excelx/cell/test_boolean.rb +36 -0
  60. data/test/excelx/cell/test_date.rb +38 -0
  61. data/test/excelx/cell/test_datetime.rb +45 -0
  62. data/test/excelx/cell/test_empty.rb +7 -0
  63. data/test/excelx/cell/test_number.rb +74 -0
  64. data/test/excelx/cell/test_string.rb +28 -0
  65. data/test/excelx/cell/test_time.rb +30 -0
  66. data/test/formatters/test_csv.rb +119 -0
  67. data/test/formatters/test_matrix.rb +76 -0
  68. data/test/formatters/test_xml.rb +78 -0
  69. data/test/formatters/test_yaml.rb +20 -0
  70. data/test/helpers/test_accessing_files.rb +60 -0
  71. data/test/helpers/test_comments.rb +43 -0
  72. data/test/helpers/test_formulas.rb +9 -0
  73. data/test/helpers/test_labels.rb +103 -0
  74. data/test/helpers/test_sheets.rb +55 -0
  75. data/test/helpers/test_styles.rb +62 -0
  76. data/test/roo/test_base.rb +182 -0
  77. data/test/roo/test_csv.rb +60 -0
  78. data/test/roo/test_excelx.rb +325 -0
  79. data/test/roo/test_libre_office.rb +9 -0
  80. data/test/roo/test_open_office.rb +289 -0
  81. data/test/test_helper.rb +116 -18
  82. data/test/test_roo.rb +362 -2088
  83. metadata +70 -4
  84. data/test/test_generic_spreadsheet.rb +0 -237
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellBase < Minitest::Test
4
+ def base
5
+ Roo::Excelx::Cell::Base
6
+ end
7
+
8
+ def value
9
+ 'Hello World'
10
+ end
11
+
12
+ def test_cell_type_is_base
13
+ cell = base.new(value, nil, [], nil, nil, nil)
14
+ assert_equal :base, cell.type
15
+ end
16
+
17
+ def test_cell_value
18
+ cell_value = value
19
+ cell = base.new(cell_value, nil, [], nil, nil, nil)
20
+ assert_equal cell_value, cell.cell_value
21
+ end
22
+
23
+ def test_not_empty?
24
+ cell = base.new(value, nil, [], nil, nil, nil)
25
+ refute cell.empty?
26
+ end
27
+
28
+ def test_cell_type_is_formula
29
+ formula = true
30
+ cell = base.new(value, formula, [], nil, nil, nil)
31
+ assert_equal :formula, cell.type
32
+ end
33
+
34
+ def test_formula?
35
+ formula = true
36
+ cell = base.new(value, formula, [], nil, nil, nil)
37
+ assert cell.formula?
38
+ end
39
+
40
+ def test_cell_type_is_link
41
+ link = 'http://example.com'
42
+ cell = base.new(value, nil, [], nil, link, nil)
43
+ assert_equal :link, cell.type
44
+ end
45
+
46
+ def test_link?
47
+ link = 'http://example.com'
48
+ cell = base.new(value, nil, [], nil, link, nil)
49
+ assert cell.link?
50
+ end
51
+
52
+ def test_link_value
53
+ link = 'http://example.com'
54
+ cell = base.new(value, nil, [], nil, link, nil)
55
+ assert_equal value, cell.value
56
+ end
57
+
58
+ def test_link_value_href
59
+ link = 'http://example.com'
60
+ cell = base.new(value, nil, [], nil, link, nil)
61
+ assert_equal link, cell.value.href
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellNumber < Minitest::Test
4
+ def boolean
5
+ Roo::Excelx::Cell::Boolean
6
+ end
7
+
8
+ def test_formatted_value
9
+ cell = boolean.new '1', nil, nil, nil, nil
10
+ assert_equal 'TRUE', cell.formatted_value
11
+
12
+ cell = boolean.new '0', nil, nil, nil, nil
13
+ assert_equal 'FALSE', cell.formatted_value
14
+ end
15
+
16
+ def test_to_s
17
+ cell = boolean.new '1', nil, nil, nil, nil
18
+ assert_equal 'TRUE', cell.to_s
19
+
20
+ cell = boolean.new '0', nil, nil, nil, nil
21
+ assert_equal 'FALSE', cell.to_s
22
+ end
23
+
24
+ def test_cell_value
25
+ cell = boolean.new '1', nil, nil, nil, nil
26
+ assert_equal '1', cell.cell_value
27
+ end
28
+
29
+ def test_value
30
+ cell = boolean.new '1', nil, nil, nil, nil
31
+ assert_equal true, cell.value
32
+
33
+ cell = boolean.new '0', nil, nil, nil, nil
34
+ assert_equal false, cell.value
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellDate < Minitest::Test
4
+ def date_cell
5
+ Roo::Excelx::Cell::Date
6
+ end
7
+
8
+ def base_date
9
+ ::Date.new(1899, 12, 30)
10
+ end
11
+
12
+ def base_date_1904
13
+ ::Date.new(1904, 01, 01)
14
+ end
15
+
16
+ def test_handles_1904_base_date
17
+ cell = date_cell.new('41791', nil, [:numeric_or_formula, 'mm-dd-yy'], 6, nil, base_date_1904, nil)
18
+ assert_equal ::Date.new(2018, 06, 02), cell.value
19
+ end
20
+
21
+ def test_formatted_value
22
+ cell = date_cell.new('41791', nil, [:numeric_or_formula, 'mm-dd-yy'], 6, nil, base_date, nil)
23
+ assert_equal '06-01-14', cell.formatted_value
24
+
25
+ cell = date_cell.new('41791', nil, [:numeric_or_formula, 'yyyy-mm-dd'], 6, nil, base_date, nil)
26
+ assert_equal '2014-06-01', cell.formatted_value
27
+ end
28
+
29
+ def test_value_is_date
30
+ cell = date_cell.new('41791', nil, [:numeric_or_formula, 'mm-dd-yy'], 6, nil, base_date, nil)
31
+ assert_kind_of ::Date, cell.value
32
+ end
33
+
34
+ def test_value
35
+ cell = date_cell.new('41791', nil, [:numeric_or_formula, 'mm-dd-yy'], 6, nil, base_date, nil)
36
+ assert_equal ::Date.new(2014, 06, 01), cell.value
37
+ end
38
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellDateTime < Minitest::Test
4
+ def test_cell_value_is_datetime
5
+ cell = datetime.new('30000.323212', nil, ['mm-dd-yy'], nil, nil, base_date, nil)
6
+ assert_kind_of ::DateTime, cell.value
7
+ end
8
+
9
+ def test_cell_type_is_datetime
10
+ cell = datetime.new('30000.323212', nil, [], nil, nil, base_date, nil)
11
+ assert_equal :datetime, cell.type
12
+ end
13
+
14
+ def test_standard_formatted_value
15
+ [
16
+ ['mm-dd-yy', '01-25-15'],
17
+ ['d-mmm-yy', '25-JAN-15'],
18
+ ['d-mmm ', '25-JAN'],
19
+ ['mmm-yy', 'JAN-15'],
20
+ ['m/d/yy h:mm', '1/25/15 8:15']
21
+ ].each do |format, formatted_value|
22
+ cell = datetime.new '42029.34375', nil, [format], nil, nil, base_date, nil
23
+ assert_equal formatted_value, cell.formatted_value
24
+ end
25
+ end
26
+
27
+ def test_custom_formatted_value
28
+ [
29
+ ['yyyy/mm/dd hh:mm:ss', '2015/01/25 08:15:00'],
30
+ ['h:mm:ss000 mm/yy', '8:15:00000 01/15'],
31
+ ['mmm yyy', '2015-01-25 08:15:00']
32
+ ].each do |format, formatted_value|
33
+ cell = datetime.new '42029.34375', nil, [format], nil, nil, base_date, nil
34
+ assert_equal formatted_value, cell.formatted_value
35
+ end
36
+ end
37
+
38
+ def datetime
39
+ Roo::Excelx::Cell::DateTime
40
+ end
41
+
42
+ def base_date
43
+ Date.new(1899, 12, 30)
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellEmpty < Minitest::Test
4
+ def empty
5
+ Roo::Excelx::Cell::Empty
6
+ end
7
+ end
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellNumber < Minitest::Test
4
+ def number
5
+ Roo::Excelx::Cell::Number
6
+ end
7
+
8
+ def test_float
9
+ cell = Roo::Excelx::Cell::Number.new '42.1', nil, ['General'], nil, nil, nil
10
+ assert_kind_of(Float, cell.value)
11
+ end
12
+
13
+ def test_integer
14
+ cell = Roo::Excelx::Cell::Number.new '42', nil, ['0'], nil, nil, nil
15
+ assert_kind_of(Integer, cell.value)
16
+ end
17
+
18
+ def test_scientific_notation
19
+ cell = Roo::Excelx::Cell::Number.new '1.2E-3', nil, ['0'], nil, nil, nil
20
+ assert_kind_of(Float, cell.value)
21
+ end
22
+
23
+ def test_simple_scientific_notation
24
+ cell = Roo::Excelx::Cell::Number.new '1E-3', nil, ['0'], nil, nil, nil
25
+ assert_kind_of(Float, cell.value)
26
+ end
27
+
28
+ def test_percent
29
+ cell = Roo::Excelx::Cell::Number.new '42.1', nil, ['0.00%'], nil, nil, nil
30
+ assert_kind_of(Float, cell.value)
31
+ end
32
+
33
+ def test_formats_with_negative_numbers
34
+ [
35
+ ['#,##0 ;(#,##0)', '(1,042)'],
36
+ ['#,##0 ;[Red](#,##0)', '[Red](1,042)'],
37
+ ['#,##0.00;(#,##0.00)', '(1,042.00)'],
38
+ ['#,##0.00;[Red](#,##0.00)', '[Red](1,042.00)']
39
+ ].each do |style_format, result|
40
+ cell = Roo::Excelx::Cell::Number.new '-1042', nil, [style_format], nil, nil, nil
41
+ assert_equal result, cell.formatted_value, "Style=#{style_format}"
42
+ end
43
+ end
44
+
45
+ def test_numbers_with_cell_errors
46
+ Roo::Excelx::ERROR_VALUES.each do |error|
47
+ cell = Roo::Excelx::Cell::Number.new error, nil, ['General'], nil, nil, nil
48
+ assert_equal error, cell.value
49
+ assert_equal error, cell.formatted_value
50
+ end
51
+ end
52
+
53
+ def test_formats
54
+ [
55
+ ['General', '1042'],
56
+ ['0', '1042'],
57
+ ['0.00', '1042.00'],
58
+ ['#,##0', '1,042'],
59
+ ['#,##0.00', '1,042.00'],
60
+ ['0%', '104200%'],
61
+ ['0.00%', '104200.00%'],
62
+ ['0.00E+00', '1.04E+03'],
63
+ ['#,##0 ;(#,##0)', '1,042'],
64
+ ['#,##0 ;[Red](#,##0)', '1,042'],
65
+ ['#,##0.00;(#,##0.00)', '1,042.00'],
66
+ ['#,##0.00;[Red](#,##0.00)', '1,042.00'],
67
+ ['##0.0E+0', '1.0E+03'],
68
+ ['@', '1042']
69
+ ].each do |style_format, result|
70
+ cell = Roo::Excelx::Cell::Number.new '1042', nil, [style_format], nil, nil, nil
71
+ assert_equal result, cell.formatted_value, "Style=#{style_format}"
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellString < Minitest::Test
4
+ def string
5
+ Roo::Excelx::Cell::String
6
+ end
7
+
8
+
9
+ def test_formatted_value
10
+ cell = string.new '1', nil, nil, nil, nil
11
+ assert_equal '1', cell.formatted_value
12
+ end
13
+
14
+ def test_to_s
15
+ cell = string.new '0', nil, nil, nil, nil
16
+ assert_equal '0', cell.to_s
17
+ end
18
+
19
+ def test_cell_value
20
+ cell = string.new '1', nil, nil, nil, nil
21
+ assert_equal '1', cell.cell_value
22
+ end
23
+
24
+ def test_value
25
+ cell = string.new '0', nil, nil, nil, nil
26
+ assert_equal '0', cell.value
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcelxCellTime < Minitest::Test
4
+ def roo_time
5
+ Roo::Excelx::Cell::Time
6
+ end
7
+
8
+ def base_date
9
+ Date.new(1899, 12, 30)
10
+ end
11
+
12
+ def test_formatted_value
13
+ value = '0.0751' # 6488.64 seconds, or 1:48:08.64
14
+ [
15
+ ['h:mm', '1:48'],
16
+ ['h:mm:ss', '1:48:09'],
17
+ ['mm:ss', '48:09'],
18
+ ['[h]:mm:ss', '[1]:48:09'],
19
+ ['mmss.0', '4809.0'] # Cell::Time always get rounded to the nearest second.
20
+ ].each do |style_format, result|
21
+ cell = roo_time.new(value, nil, [:numeric_or_formula, style_format], 6, nil, base_date, nil)
22
+ assert_equal result, cell.formatted_value, "Style=#{style_format} is not properly formatted"
23
+ end
24
+ end
25
+
26
+ def test_value
27
+ cell = roo_time.new('0.0751', nil, [:numeric_or_formula, 'h:mm'], 6, nil, base_date, nil)
28
+ assert_kind_of Integer, cell.value
29
+ end
30
+ end
@@ -0,0 +1,119 @@
1
+ require "test_helper"
2
+
3
+ class TestRooFormatterCSV < Minitest::Test
4
+ def test_date_time_to_csv
5
+ with_each_spreadsheet(name: "time-test") do |workbook|
6
+ Dir.mktmpdir do |tempdir|
7
+ csv_output = File.join(tempdir, "time_test.csv")
8
+ assert workbook.to_csv(csv_output)
9
+ assert File.exist?(csv_output)
10
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
11
+ # --strip-trailing-cr is needed because the test-file use 0A and
12
+ # the test on an windows box generates 0D 0A as line endings
13
+ end
14
+ end
15
+ end
16
+
17
+ def test_boolean_to_csv
18
+ with_each_spreadsheet(name: "boolean") do |workbook|
19
+ Dir.mktmpdir do |tempdir|
20
+ csv_output = File.join(tempdir,"boolean.csv")
21
+ assert workbook.to_csv(csv_output)
22
+ assert File.exist?(csv_output)
23
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}`
24
+ # --strip-trailing-cr is needed because the test-file use 0A and
25
+ # the test on an windows box generates 0D 0A as line endings
26
+ end
27
+ end
28
+ end
29
+
30
+ def test_link_to_csv
31
+ with_each_spreadsheet(name: "link", format: :excelx) do |workbook|
32
+ Dir.mktmpdir do |tempdir|
33
+ csv_output = File.join(tempdir, "link.csv")
34
+ assert workbook.to_csv(csv_output)
35
+ assert File.exist?(csv_output)
36
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}`
37
+ # --strip-trailing-cr is needed because the test-file use 0A and
38
+ # the test on an windows box generates 0D 0A as line endings
39
+ end
40
+ end
41
+ end
42
+
43
+ # "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
44
+ # wird.
45
+ # Besser: Methode um temporaeres Dir. portabel zu bestimmen
46
+ def test_huge_document_to_csv
47
+ skip_long_test
48
+
49
+ original_csv_path = File.join(TESTDIR, "Bibelbund.csv")
50
+ with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
51
+ Dir.mktmpdir do |tempdir|
52
+ new_csv_path = File.join(tempdir, "Bibelbund.csv")
53
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", workbook.cell(45, "A")
54
+ assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", workbook.cell(46, "A")
55
+ assert_equal "Tagebuch aus Chile Juli 1977", workbook.cell(55, "A")
56
+ assert workbook.to_csv(new_csv_path)
57
+ assert File.exist?(new_csv_path)
58
+ assert FileUtils.identical?(original_csv_path, new_csv_path), "error in class #{workbook.class}"
59
+ end
60
+ end
61
+ end
62
+
63
+ def test_bug_empty_sheet
64
+ with_each_spreadsheet(name: "formula", format: [:openoffice, :excelx]) do |workbook|
65
+ workbook.default_sheet = "Sheet3" # is an empty sheet
66
+ Dir.mktmpdir do |tempdir|
67
+ workbook.to_csv(File.join(tempdir, "emptysheet.csv"))
68
+ assert_equal "", `cat #{File.join(tempdir, "emptysheet.csv")}`
69
+ end
70
+ end
71
+ end
72
+
73
+ def test_bug_quotes_excelx
74
+ skip_long_test
75
+ # TODO: run this test with a much smaller document
76
+ with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
77
+ workbook.default_sheet = workbook.sheets.first
78
+ assert_equal(
79
+ 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
80
+ workbook.cell("A", 76)
81
+ )
82
+ workbook.to_csv("csv#{$$}")
83
+ assert_equal(
84
+ 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
85
+ workbook.cell("A", 78)
86
+ )
87
+ File.delete_if_exist("csv#{$$}")
88
+ end
89
+ end
90
+
91
+ def test_bug_datetime_to_csv
92
+ with_each_spreadsheet(name: "datetime") do |workbook|
93
+ Dir.mktmpdir do |tempdir|
94
+ datetime_csv_file = File.join(tempdir, "datetime.csv")
95
+
96
+ assert workbook.to_csv(datetime_csv_file)
97
+ assert File.exist?(datetime_csv_file)
98
+ assert_equal "", file_diff("#{TESTDIR}/so_datetime.csv", datetime_csv_file)
99
+ end
100
+ end
101
+ end
102
+
103
+ def test_true_class
104
+ assert_equal "true", cell_to_csv(1, 1)
105
+ end
106
+
107
+ def test_false_class
108
+ assert_equal "false", cell_to_csv(2, 1)
109
+ end
110
+
111
+ def test_date_class
112
+ assert_equal "2017-01-01", cell_to_csv(3, 1)
113
+ end
114
+
115
+ def cell_to_csv(row, col)
116
+ filename = File.join(TESTDIR, "formula_cell_types.xlsx")
117
+ Roo::Spreadsheet.open(filename).send("cell_to_csv", row, col, "Sheet1")
118
+ end
119
+ end
@@ -0,0 +1,76 @@
1
+ require "test_helper"
2
+ require "matrix"
3
+
4
+ class TestRooFormatterMatrix < Minitest::Test
5
+ def test_matrix
6
+ expected_result = Matrix[
7
+ [1.0, 2.0, 3.0],
8
+ [4.0, 5.0, 6.0],
9
+ [7.0, 8.0, 9.0]
10
+ ]
11
+ with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook|
12
+ workbook.default_sheet = workbook.sheets.first
13
+ assert_equal expected_result, workbook.to_matrix
14
+ end
15
+ end
16
+
17
+ def test_matrix_selected_range
18
+ expected_result = Matrix[
19
+ [1.0, 2.0, 3.0],
20
+ [4.0, 5.0, 6.0],
21
+ [7.0, 8.0, 9.0]
22
+ ]
23
+ with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook|
24
+ workbook.default_sheet = "Sheet2"
25
+ assert_equal expected_result, workbook.to_matrix(3, 4, 5, 6)
26
+ end
27
+ end
28
+
29
+ def test_matrix_all_nil
30
+ expected_result = Matrix[
31
+ [nil, nil, nil],
32
+ [nil, nil, nil],
33
+ [nil, nil, nil]
34
+ ]
35
+ with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook|
36
+ workbook.default_sheet = "Sheet2"
37
+ assert_equal expected_result, workbook.to_matrix(10, 10, 12, 12)
38
+ end
39
+ end
40
+
41
+ def test_matrix_values_and_nil
42
+ expected_result = Matrix[
43
+ [1.0, nil, 3.0],
44
+ [4.0, 5.0, 6.0],
45
+ [7.0, 8.0, nil]
46
+ ]
47
+ with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook|
48
+ workbook.default_sheet = "Sheet3"
49
+ assert_equal expected_result, workbook.to_matrix(1, 1, 3, 3)
50
+ end
51
+ end
52
+
53
+ def test_matrix_specifying_sheet
54
+ expected_result = Matrix[
55
+ [1.0, nil, 3.0],
56
+ [4.0, 5.0, 6.0],
57
+ [7.0, 8.0, nil]
58
+ ]
59
+ with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook|
60
+ workbook.default_sheet = workbook.sheets.first
61
+ assert_equal expected_result, workbook.to_matrix(nil, nil, nil, nil, "Sheet3")
62
+ end
63
+ end
64
+
65
+ # #to_matrix of an empty sheet should return an empty matrix and not result in
66
+ # an error message
67
+ # 2011-06-25
68
+ def test_bug_to_matrix_empty_sheet
69
+ options = { name: "emptysheets", format: [:openoffice, :excelx] }
70
+ with_each_spreadsheet(options) do |workbook|
71
+ workbook.default_sheet = workbook.sheets.first
72
+ workbook.to_matrix
73
+ assert_equal(Matrix.empty(0, 0), workbook.to_matrix)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,78 @@
1
+ require "test_helper"
2
+
3
+ class TestRooFormatterXML < Minitest::Test
4
+ def test_to_xml
5
+ expected_sheet_count = 5
6
+ options = { name: "numbers1", encoding: "utf8" }
7
+ with_each_spreadsheet(options) do |workbook|
8
+ skip if defined? JRUBY_VERSION
9
+ workbook.to_xml
10
+ sheetname = workbook.sheets.first
11
+ doc = Nokogiri::XML(workbook.to_xml)
12
+
13
+ assert_equal expected_sheet_count, doc.xpath("//spreadsheet/sheet").count
14
+
15
+ doc.xpath("//spreadsheet/sheet").each do |xml_sheet|
16
+ all_cells = init_all_cells(workbook, sheetname)
17
+ cells = xml_sheet.children.reject(&:text?)
18
+
19
+ assert_equal sheetname, xml_sheet.attribute("name").value
20
+ assert_equal all_cells.size, cells.size
21
+
22
+ cells.each_with_index do |cell, i|
23
+ expected = [
24
+ all_cells[i][:row],
25
+ all_cells[i][:column],
26
+ all_cells[i][:content],
27
+ all_cells[i][:type],
28
+ ]
29
+ result = [
30
+ cell.attribute("row").value,
31
+ cell.attribute("column").value,
32
+ cell.text,
33
+ cell.attribute("type").value,
34
+ ]
35
+
36
+ assert_equal expected, result
37
+ end # end of sheet
38
+ sheetname = workbook.sheets[workbook.sheets.index(sheetname) + 1]
39
+ end
40
+ end
41
+ end
42
+
43
+ def test_bug_to_xml_with_empty_sheets
44
+ with_each_spreadsheet(name: "emptysheets", format: [:openoffice, :excelx]) do |workbook|
45
+ workbook.sheets.each do |sheet|
46
+ assert_nil workbook.first_row, "first_row not nil in sheet #{sheet}"
47
+ assert_nil workbook.last_row, "last_row not nil in sheet #{sheet}"
48
+ assert_nil workbook.first_column, "first_column not nil in sheet #{sheet}"
49
+ assert_nil workbook.last_column, "last_column not nil in sheet #{sheet}"
50
+ assert_nil workbook.first_row(sheet), "first_row not nil in sheet #{sheet}"
51
+ assert_nil workbook.last_row(sheet), "last_row not nil in sheet #{sheet}"
52
+ assert_nil workbook.first_column(sheet), "first_column not nil in sheet #{sheet}"
53
+ assert_nil workbook.last_column(sheet), "last_column not nil in sheet #{sheet}"
54
+ end
55
+ workbook.to_xml
56
+ end
57
+ end
58
+
59
+ # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
60
+ # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
61
+ # nicht immer in der gleichen Reihenfolge erzeugt.
62
+ def init_all_cells(workbook, sheet)
63
+ all = []
64
+ workbook.first_row(sheet).upto(workbook.last_row(sheet)) do |row|
65
+ workbook.first_column(sheet).upto(workbook.last_column(sheet)) do |col|
66
+ next if workbook.empty?(row, col, sheet)
67
+
68
+ all << {
69
+ row: row.to_s,
70
+ column: col.to_s,
71
+ content: workbook.cell(row, col, sheet).to_s,
72
+ type: workbook.celltype(row, col, sheet).to_s,
73
+ }
74
+ end
75
+ end
76
+ all
77
+ end
78
+ end
@@ -0,0 +1,20 @@
1
+ require "test_helper"
2
+
3
+ class TestRooFormatterYAML < Minitest::Test
4
+ def test_date_time_yaml
5
+ name = "time-test"
6
+ expected = File.open(TESTDIR + "/expected_results/#{name}.yml").read
7
+ with_each_spreadsheet(name: name) do |workbook|
8
+ assert_equal expected, workbook.to_yaml
9
+ end
10
+ end
11
+
12
+ def test_bug_to_yaml_empty_sheet
13
+ formats = [:openoffice, :excelx]
14
+ with_each_spreadsheet(name: "emptysheets", format: formats) do |workbook|
15
+ workbook.default_sheet = workbook.sheets.first
16
+ workbook.to_yaml
17
+ assert_equal "", workbook.to_yaml
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ # Tests for "Accessing Files" which includes opening and closing files.
2
+ module TestAccesingFiles
3
+ def test_close
4
+ with_each_spreadsheet(name: "numbers1") do |oo|
5
+ next unless (tempdir = oo.instance_variable_get("@tmpdir"))
6
+ oo.close
7
+ refute File.exist?(tempdir), "Expected #{tempdir} to be cleaned up"
8
+ end
9
+ end
10
+
11
+ # NOTE: Ruby 2.4.0 changed the way GC works. The last Roo object created by
12
+ # with_each_spreadsheet wasn't getting GC'd until after the process
13
+ # ended.
14
+ #
15
+ # That behavior change broke this test. In order to fix it, I forked the
16
+ # process and passed the temp directories from the forked process in
17
+ # order to check if they were removed properly.
18
+ def test_finalize
19
+ skip if defined? JRUBY_VERSION
20
+
21
+ read, write = IO.pipe
22
+ pid = Process.fork do
23
+ with_each_spreadsheet(name: "numbers1") do |oo|
24
+ write.puts oo.instance_variable_get("@tmpdir")
25
+ end
26
+ end
27
+
28
+ Process.wait(pid)
29
+ write.close
30
+ tempdirs = read.read.split("\n")
31
+ read.close
32
+
33
+ refute tempdirs.empty?
34
+ tempdirs.each do |tempdir|
35
+ refute File.exist?(tempdir), "Expected #{tempdir} to be cleaned up"
36
+ end
37
+ end
38
+
39
+ def test_cleanup_on_error
40
+ # NOTE: This test was occasionally failing because when it started running
41
+ # other tests would have already added folders to the temp directory,
42
+ # polluting the directory. You'd end up in a situation where there
43
+ # would be less folders AFTER this ran than originally started.
44
+ #
45
+ # Instead, just use a custom temp directory to test the functionality.
46
+ ENV["ROO_TMP"] = Dir.tmpdir + "/test_cleanup_on_error"
47
+ Dir.mkdir(ENV["ROO_TMP"]) unless File.exist?(ENV["ROO_TMP"])
48
+ expected_dir_contents = Dir.open(ENV["ROO_TMP"]).to_a
49
+ with_each_spreadsheet(name: "non_existent_file", ignore_errors: true) {}
50
+
51
+ assert_equal expected_dir_contents, Dir.open(ENV["ROO_TMP"]).to_a
52
+ Dir.rmdir ENV["ROO_TMP"] if File.exist?(ENV["ROO_TMP"])
53
+ ENV.delete "ROO_TMP"
54
+ end
55
+
56
+ def test_name_with_leading_slash
57
+ xlsx = Roo::Excelx.new(File.join(TESTDIR, "/name_with_leading_slash.xlsx"))
58
+ assert_equal 1, xlsx.sheets.count
59
+ end
60
+ end