roo 2.6.0 → 2.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +16 -0
  4. data/.github/pull_request_template.md +14 -0
  5. data/.rubocop.yml +186 -0
  6. data/.travis.yml +14 -11
  7. data/CHANGELOG.md +64 -2
  8. data/Gemfile +2 -4
  9. data/LICENSE +2 -0
  10. data/README.md +36 -10
  11. data/lib/roo/base.rb +82 -225
  12. data/lib/roo/constants.rb +5 -3
  13. data/lib/roo/csv.rb +100 -97
  14. data/lib/roo/excelx/cell/base.rb +26 -12
  15. data/lib/roo/excelx/cell/boolean.rb +9 -6
  16. data/lib/roo/excelx/cell/date.rb +7 -7
  17. data/lib/roo/excelx/cell/datetime.rb +50 -44
  18. data/lib/roo/excelx/cell/empty.rb +3 -2
  19. data/lib/roo/excelx/cell/number.rb +44 -47
  20. data/lib/roo/excelx/cell/string.rb +3 -3
  21. data/lib/roo/excelx/cell/time.rb +17 -16
  22. data/lib/roo/excelx/cell.rb +10 -6
  23. data/lib/roo/excelx/comments.rb +3 -3
  24. data/lib/roo/excelx/coordinate.rb +11 -4
  25. data/lib/roo/excelx/extractor.rb +21 -3
  26. data/lib/roo/excelx/format.rb +38 -31
  27. data/lib/roo/excelx/images.rb +26 -0
  28. data/lib/roo/excelx/relationships.rb +12 -4
  29. data/lib/roo/excelx/shared.rb +10 -3
  30. data/lib/roo/excelx/shared_strings.rb +9 -15
  31. data/lib/roo/excelx/sheet.rb +49 -10
  32. data/lib/roo/excelx/sheet_doc.rb +89 -48
  33. data/lib/roo/excelx/styles.rb +3 -3
  34. data/lib/roo/excelx/workbook.rb +7 -3
  35. data/lib/roo/excelx.rb +50 -19
  36. data/lib/roo/formatters/base.rb +15 -0
  37. data/lib/roo/formatters/csv.rb +84 -0
  38. data/lib/roo/formatters/matrix.rb +23 -0
  39. data/lib/roo/formatters/xml.rb +31 -0
  40. data/lib/roo/formatters/yaml.rb +40 -0
  41. data/lib/roo/helpers/default_attr_reader.rb +20 -0
  42. data/lib/roo/helpers/weak_instance_cache.rb +41 -0
  43. data/lib/roo/open_office.rb +17 -9
  44. data/lib/roo/spreadsheet.rb +1 -1
  45. data/lib/roo/tempdir.rb +5 -10
  46. data/lib/roo/utils.rb +70 -20
  47. data/lib/roo/version.rb +1 -1
  48. data/lib/roo.rb +4 -1
  49. data/roo.gemspec +14 -11
  50. data/spec/lib/roo/base_spec.rb +45 -3
  51. data/spec/lib/roo/excelx/relationships_spec.rb +43 -0
  52. data/spec/lib/roo/excelx/sheet_doc_spec.rb +11 -0
  53. data/spec/lib/roo/excelx_spec.rb +150 -31
  54. data/spec/lib/roo/strict_spec.rb +43 -0
  55. data/spec/lib/roo/utils_spec.rb +25 -3
  56. data/spec/lib/roo/weak_instance_cache_spec.rb +92 -0
  57. data/spec/lib/roo_spec.rb +0 -0
  58. data/spec/spec_helper.rb +2 -6
  59. data/test/excelx/cell/test_attr_reader_default.rb +72 -0
  60. data/test/excelx/cell/test_base.rb +5 -0
  61. data/test/excelx/cell/test_datetime.rb +6 -6
  62. data/test/excelx/cell/test_empty.rb +11 -0
  63. data/test/excelx/cell/test_number.rb +9 -0
  64. data/test/excelx/cell/test_string.rb +20 -0
  65. data/test/excelx/cell/test_time.rb +5 -5
  66. data/test/excelx/test_coordinate.rb +51 -0
  67. data/test/formatters/test_csv.rb +136 -0
  68. data/test/formatters/test_matrix.rb +76 -0
  69. data/test/formatters/test_xml.rb +78 -0
  70. data/test/formatters/test_yaml.rb +20 -0
  71. data/test/helpers/test_accessing_files.rb +60 -0
  72. data/test/helpers/test_comments.rb +43 -0
  73. data/test/helpers/test_formulas.rb +9 -0
  74. data/test/helpers/test_labels.rb +103 -0
  75. data/test/helpers/test_sheets.rb +55 -0
  76. data/test/helpers/test_styles.rb +62 -0
  77. data/test/roo/test_base.rb +182 -0
  78. data/test/roo/test_csv.rb +88 -0
  79. data/test/roo/test_excelx.rb +330 -0
  80. data/test/roo/test_libre_office.rb +9 -0
  81. data/test/roo/test_open_office.rb +289 -0
  82. data/test/test_helper.rb +129 -14
  83. data/test/test_roo.rb +32 -1787
  84. metadata +81 -29
  85. data/.github/ISSUE_TEMPLATE +0 -10
  86. data/Gemfile_ruby2 +0 -29
@@ -1,12 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class TestRooExcelxCellTime < Minitest::Test
4
- def time
4
+ def roo_time
5
5
  Roo::Excelx::Cell::Time
6
6
  end
7
7
 
8
- def base_date
9
- Date.new(1899, 12, 30)
8
+ def base_timestamp
9
+ DateTime.new(1899, 12, 30).to_time.to_i
10
10
  end
11
11
 
12
12
  def test_formatted_value
@@ -18,13 +18,13 @@ class TestRooExcelxCellTime < Minitest::Test
18
18
  ['[h]:mm:ss', '[1]:48:09'],
19
19
  ['mmss.0', '4809.0'] # Cell::Time always get rounded to the nearest second.
20
20
  ].each do |style_format, result|
21
- cell = time.new(value, nil, [:numeric_or_formula, style_format], 6, nil, base_date, nil)
21
+ cell = roo_time.new(value, nil, [:numeric_or_formula, style_format], 6, nil, base_timestamp, nil)
22
22
  assert_equal result, cell.formatted_value, "Style=#{style_format} is not properly formatted"
23
23
  end
24
24
  end
25
25
 
26
26
  def test_value
27
- cell = time.new('0.0751', nil, [:numeric_or_formula, 'h:mm'], 6, nil, base_date, nil)
27
+ cell = roo_time.new('0.0751', nil, [:numeric_or_formula, 'h:mm'], 6, nil, base_timestamp, nil)
28
28
  assert_kind_of Integer, cell.value
29
29
  end
30
30
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class TestRooExcelxCoordinate < Minitest::Test
6
+ def row
7
+ 10
8
+ end
9
+
10
+ def column
11
+ 20
12
+ end
13
+
14
+ def coordinate
15
+ Roo::Excelx::Coordinate.new(row, column)
16
+ end
17
+
18
+ def array
19
+ [row, column]
20
+ end
21
+
22
+ def test_row
23
+ assert_same row, coordinate.row
24
+ end
25
+
26
+ def test_column
27
+ assert_same column, coordinate.column
28
+ end
29
+
30
+ def test_frozen?
31
+ assert coordinate.frozen?
32
+ end
33
+
34
+ def test_equality
35
+ hash = {}
36
+ hash[coordinate] = true
37
+ assert hash.key?(coordinate)
38
+ assert hash.key?(array)
39
+ end
40
+
41
+ def test_expand
42
+ r, c = coordinate
43
+ assert_same row, r
44
+ assert_same column, c
45
+ end
46
+
47
+ def test_aref
48
+ assert_same row, coordinate[0]
49
+ assert_same column, coordinate[1]
50
+ end
51
+ end
@@ -0,0 +1,136 @@
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_bug_datetime_offset_change
104
+ # DO NOT REMOVE Asia/Calcutta
105
+ [nil, "US/Eastern", "US/Pacific", "Asia/Calcutta"].each do |zone|
106
+ with_timezone(zone) do
107
+ with_each_spreadsheet(name: "datetime_timezone_ist_offset_change", format: %i[excelx openoffice libreoffice]) do |workbook|
108
+ Dir.mktmpdir do |tempdir|
109
+ datetime_csv_file = File.join(tempdir, "datetime_timezone_ist_offset_change.csv")
110
+
111
+ assert workbook.to_csv(datetime_csv_file)
112
+ assert File.exist?(datetime_csv_file)
113
+ assert_equal "", file_diff("#{TESTDIR}/so_datetime_timezone_ist_offset_change.csv", datetime_csv_file)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ def test_true_class
121
+ assert_equal "true", cell_to_csv(1, 1)
122
+ end
123
+
124
+ def test_false_class
125
+ assert_equal "false", cell_to_csv(2, 1)
126
+ end
127
+
128
+ def test_date_class
129
+ assert_equal "2017-01-01", cell_to_csv(3, 1)
130
+ end
131
+
132
+ def cell_to_csv(row, col)
133
+ filename = File.join(TESTDIR, "formula_cell_types.xlsx")
134
+ Roo::Spreadsheet.open(filename).send("cell_to_csv", row, col, "Sheet1")
135
+ end
136
+ 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["name"]
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["row"],
31
+ cell["column"],
32
+ cell.text,
33
+ cell["type"],
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
@@ -0,0 +1,43 @@
1
+ module TestComments
2
+ def test_comment
3
+ options = { name: "comments", format: [:openoffice, :libreoffice, :excelx] }
4
+ with_each_spreadsheet(options) do |oo|
5
+ assert_equal "Kommentar fuer B4", oo.comment("b", 4)
6
+ assert_equal "Kommentar fuer B5", oo.comment("b", 5)
7
+ end
8
+ end
9
+
10
+ def test_no_comment
11
+ options = { name: "comments", format: [:openoffice, :excelx] }
12
+ with_each_spreadsheet(options) do |oo|
13
+ # There are no comments at the second sheet.
14
+ assert_nil oo.comment("b", 4, oo.sheets[1])
15
+ end
16
+ end
17
+
18
+ def test_comments
19
+ options = { name: "comments", format: [:openoffice, :libreoffice, :excelx] }
20
+ expexted_comments = [
21
+ [4, 2, "Kommentar fuer B4"],
22
+ [5, 2, "Kommentar fuer B5"],
23
+ ]
24
+
25
+ with_each_spreadsheet(options) do |oo|
26
+ assert_equal expexted_comments, oo.comments(oo.sheets.first), "comments error in class #{oo.class}"
27
+ end
28
+ end
29
+
30
+ def test_empty_sheet_comments
31
+ options = { name: "emptysheets", format: [:openoffice, :excelx] }
32
+ with_each_spreadsheet(options) do |oo|
33
+ assert_equal [], oo.comments, "An empty sheet's formulas should be an empty array"
34
+ end
35
+ end
36
+
37
+ def test_comments_from_google_sheet_exported_as_xlsx
38
+ expected_comments = [[1, 1, "this is a comment\n\t-Steven Daniels"]]
39
+ with_each_spreadsheet(name: "comments-google", format: [:excelx]) do |oo|
40
+ assert_equal expected_comments, oo.comments(oo.sheets.first), "comments error in class #{oo.class}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module TestFormulas
2
+ def test_empty_sheet_formulas
3
+ options = { name: "emptysheets", format: [:openoffice, :excelx] }
4
+ with_each_spreadsheet(options) do |oo|
5
+ oo.default_sheet = oo.sheets.first
6
+ assert_equal [], oo.formulas, "An empty sheet's formulas should be an empty array"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,103 @@
1
+ module TestLabels
2
+ def test_labels
3
+ options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] }
4
+ expected_labels = [
5
+ ["anton", [5, 3, "Sheet1"]],
6
+ ["berta", [4, 2, "Sheet1"]],
7
+ ["caesar", [7, 2, "Sheet1"]],
8
+ ]
9
+ with_each_spreadsheet(options) do |oo|
10
+ assert_equal expected_labels, oo.labels, "error with labels array in class #{oo.class}"
11
+ end
12
+ end
13
+
14
+ def test_labeled_cells
15
+ options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] }
16
+ with_each_spreadsheet(options) do |oo|
17
+ oo.default_sheet = oo.sheets.first
18
+ begin
19
+ row, col = oo.label("anton")
20
+ rescue ArgumentError
21
+ puts "labels error at #{oo.class}"
22
+ raise
23
+ end
24
+ assert_equal 5, row
25
+ assert_equal 3, col
26
+
27
+ row, col = oo.label("anton")
28
+ assert_equal "Anton", oo.cell(row, col)
29
+
30
+ row, col = oo.label("berta")
31
+ assert_equal "Bertha", oo.cell(row, col)
32
+
33
+ row, col = oo.label("caesar")
34
+ assert_equal "Cäsar", oo.cell(row, col)
35
+
36
+ row, col = oo.label("never")
37
+ assert_nil row
38
+ assert_nil col
39
+
40
+ row, col, sheet = oo.label("anton")
41
+ assert_equal 5, row
42
+ assert_equal 3, col
43
+ assert_equal "Sheet1", sheet
44
+
45
+ assert_equal "Anton", oo.anton
46
+ assert_raises(NoMethodError) do
47
+ row, col = oo.never
48
+ end
49
+
50
+ # Reihenfolge row, col,sheet analog zu #label
51
+ expected_labels = [
52
+ ["anton", [5, 3, "Sheet1"]],
53
+ ["berta", [4, 2, "Sheet1"]],
54
+ ["caesar", [7, 2, "Sheet1"]],
55
+ ]
56
+ assert_equal expected_labels, oo.labels, "error with labels array in class #{oo.class}"
57
+ end
58
+ end
59
+
60
+ def test_label
61
+ options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] }
62
+ with_each_spreadsheet(options) do |oo|
63
+ begin
64
+ row, col = oo.label("anton")
65
+ rescue ArgumentError
66
+ puts "labels error at #{oo.class}"
67
+ raise
68
+ end
69
+
70
+ assert_equal 5, row, "error with label in class #{oo.class}"
71
+ assert_equal 3, col, "error with label in class #{oo.class}"
72
+
73
+ row, col = oo.label("anton")
74
+ assert_equal "Anton", oo.cell(row, col), "error with label in class #{oo.class}"
75
+
76
+ row, col = oo.label("berta")
77
+ assert_equal "Bertha", oo.cell(row, col), "error with label in class #{oo.class}"
78
+
79
+ row, col = oo.label("caesar")
80
+ assert_equal "Cäsar", oo.cell(row, col), "error with label in class #{oo.class}"
81
+
82
+ row, col = oo.label("never")
83
+ assert_nil row
84
+ assert_nil col
85
+
86
+ row, col, sheet = oo.label("anton")
87
+ assert_equal 5, row
88
+ assert_equal 3, col
89
+ assert_equal "Sheet1", sheet
90
+ end
91
+ end
92
+
93
+ def test_method_missing_anton
94
+ options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] }
95
+ with_each_spreadsheet(options) do |oo|
96
+ # oo.default_sheet = oo.sheets.first
97
+ assert_equal "Anton", oo.anton
98
+ assert_raises(NoMethodError) do
99
+ oo.never
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,55 @@
1
+ # NOTE: Putting these tests into modules in order to share them across different
2
+ # test classes, i.e. both TestRooExcelx and TestRooOpenOffice should run
3
+ # sheet related tests.
4
+ #
5
+ # This will allow me to reuse these test cases when I add new classes for
6
+ # Roo's future API.
7
+ # Sheet related tests
8
+ module TestSheets
9
+ def test_sheets
10
+ sheet_names = ["Tabelle1", "Name of Sheet 2", "Sheet3", "Sheet4", "Sheet5"]
11
+ with_each_spreadsheet(name: "numbers1") do |oo|
12
+ assert_equal sheet_names, oo.sheets
13
+ assert_raises(RangeError) { oo.default_sheet = "no_sheet" }
14
+ assert_raises(TypeError) { oo.default_sheet = [1, 2, 3] }
15
+ oo.sheets.each do |sheet_name|
16
+ oo.default_sheet = sheet_name
17
+ assert_equal sheet_name, oo.default_sheet
18
+ end
19
+ end
20
+ end
21
+
22
+ def test_sheetname
23
+ bad_sheet_name = "non existing sheet name"
24
+ with_each_spreadsheet(name: "numbers1") do |oo|
25
+ oo.default_sheet = "Name of Sheet 2"
26
+ assert_equal "I am sheet 2", oo.cell("C", 5)
27
+ assert_raises(RangeError) { oo.default_sheet = bad_sheet_name }
28
+ assert_raises(RangeError) { oo.default_sheet = bad_sheet_name }
29
+ assert_raises(RangeError) { oo.cell("C", 5, bad_sheet_name) }
30
+ assert_raises(RangeError) { oo.celltype("C", 5, bad_sheet_name) }
31
+ assert_raises(RangeError) { oo.empty?("C", 5, bad_sheet_name) }
32
+ assert_raises(RangeError) { oo.formula?("C", 5, bad_sheet_name) }
33
+ assert_raises(RangeError) { oo.formula("C", 5, bad_sheet_name) }
34
+ assert_raises(RangeError) { oo.set("C", 5, 42, bad_sheet_name) }
35
+ assert_raises(RangeError) { oo.formulas(bad_sheet_name) }
36
+ assert_raises(RangeError) { oo.to_yaml({}, 1, 1, 1, 1, bad_sheet_name) }
37
+ end
38
+ end
39
+
40
+ def test_info_doesnt_set_default_sheet
41
+ sheet_name = "Sheet3"
42
+ with_each_spreadsheet(name: "numbers1") do |oo|
43
+ oo.default_sheet = sheet_name
44
+ oo.info
45
+ assert_equal sheet_name, oo.default_sheet
46
+ end
47
+ end
48
+
49
+ def test_bug_numbered_sheet_names
50
+ options = { name: "bug-numbered-sheet-names", format: :excelx }
51
+ with_each_spreadsheet(options) do |oo|
52
+ oo.each_with_pagename {}
53
+ end
54
+ end
55
+ end