roo 2.7.0 → 2.8.3

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 (75) hide show
  1. checksums.yaml +5 -5
  2. data/.github/issue_template.md +16 -0
  3. data/.github/pull_request_template.md +14 -0
  4. data/.rubocop.yml +186 -0
  5. data/.travis.yml +12 -7
  6. data/CHANGELOG.md +53 -2
  7. data/LICENSE +2 -0
  8. data/README.md +29 -13
  9. data/lib/roo/base.rb +69 -61
  10. data/lib/roo/constants.rb +5 -3
  11. data/lib/roo/csv.rb +20 -12
  12. data/lib/roo/excelx/cell/base.rb +26 -12
  13. data/lib/roo/excelx/cell/boolean.rb +9 -6
  14. data/lib/roo/excelx/cell/date.rb +7 -7
  15. data/lib/roo/excelx/cell/datetime.rb +14 -18
  16. data/lib/roo/excelx/cell/empty.rb +3 -2
  17. data/lib/roo/excelx/cell/number.rb +35 -34
  18. data/lib/roo/excelx/cell/string.rb +3 -3
  19. data/lib/roo/excelx/cell/time.rb +4 -3
  20. data/lib/roo/excelx/cell.rb +10 -6
  21. data/lib/roo/excelx/comments.rb +3 -3
  22. data/lib/roo/excelx/coordinate.rb +11 -4
  23. data/lib/roo/excelx/extractor.rb +21 -3
  24. data/lib/roo/excelx/format.rb +38 -31
  25. data/lib/roo/excelx/images.rb +26 -0
  26. data/lib/roo/excelx/relationships.rb +12 -4
  27. data/lib/roo/excelx/shared.rb +10 -3
  28. data/lib/roo/excelx/shared_strings.rb +9 -15
  29. data/lib/roo/excelx/sheet.rb +49 -10
  30. data/lib/roo/excelx/sheet_doc.rb +89 -48
  31. data/lib/roo/excelx/styles.rb +3 -3
  32. data/lib/roo/excelx/workbook.rb +7 -3
  33. data/lib/roo/excelx.rb +42 -16
  34. data/lib/roo/helpers/default_attr_reader.rb +20 -0
  35. data/lib/roo/helpers/weak_instance_cache.rb +41 -0
  36. data/lib/roo/open_office.rb +8 -6
  37. data/lib/roo/spreadsheet.rb +1 -1
  38. data/lib/roo/utils.rb +70 -20
  39. data/lib/roo/version.rb +1 -1
  40. data/lib/roo.rb +4 -1
  41. data/roo.gemspec +13 -11
  42. data/spec/lib/roo/base_spec.rb +45 -3
  43. data/spec/lib/roo/excelx/relationships_spec.rb +43 -0
  44. data/spec/lib/roo/excelx/sheet_doc_spec.rb +11 -0
  45. data/spec/lib/roo/excelx_spec.rb +150 -31
  46. data/spec/lib/roo/strict_spec.rb +43 -0
  47. data/spec/lib/roo/utils_spec.rb +25 -3
  48. data/spec/lib/roo/weak_instance_cache_spec.rb +92 -0
  49. data/spec/lib/roo_spec.rb +0 -0
  50. data/spec/spec_helper.rb +1 -1
  51. data/test/excelx/cell/test_attr_reader_default.rb +72 -0
  52. data/test/excelx/cell/test_base.rb +5 -0
  53. data/test/excelx/cell/test_datetime.rb +6 -6
  54. data/test/excelx/cell/test_empty.rb +11 -0
  55. data/test/excelx/cell/test_number.rb +9 -0
  56. data/test/excelx/cell/test_string.rb +20 -0
  57. data/test/excelx/cell/test_time.rb +4 -4
  58. data/test/excelx/test_coordinate.rb +51 -0
  59. data/test/formatters/test_csv.rb +19 -2
  60. data/test/formatters/test_xml.rb +13 -9
  61. data/test/helpers/test_accessing_files.rb +60 -0
  62. data/test/helpers/test_comments.rb +43 -0
  63. data/test/helpers/test_formulas.rb +9 -0
  64. data/test/helpers/test_labels.rb +103 -0
  65. data/test/helpers/test_sheets.rb +55 -0
  66. data/test/helpers/test_styles.rb +62 -0
  67. data/test/roo/test_base.rb +182 -0
  68. data/test/roo/test_csv.rb +37 -1
  69. data/test/roo/test_excelx.rb +157 -13
  70. data/test/roo/test_open_office.rb +196 -33
  71. data/test/test_helper.rb +66 -22
  72. data/test/test_roo.rb +32 -881
  73. metadata +32 -14
  74. data/.github/ISSUE_TEMPLATE +0 -10
  75. data/Gemfile_ruby2 +0 -30
@@ -0,0 +1,72 @@
1
+ require "test_helper"
2
+
3
+ class TestAttrReaderDefault < Minitest::Test
4
+ def base
5
+ Roo::Excelx::Cell::Base
6
+ end
7
+
8
+ def boolean
9
+ Roo::Excelx::Cell::Boolean
10
+ end
11
+
12
+ def class_date
13
+ Roo::Excelx::Cell::Date
14
+ end
15
+
16
+ def datetime
17
+ Roo::Excelx::Cell::DateTime
18
+ end
19
+
20
+ def empty
21
+ Roo::Excelx::Cell::Empty
22
+ end
23
+
24
+ def number
25
+ Roo::Excelx::Cell::Number
26
+ end
27
+
28
+ def string
29
+ Roo::Excelx::Cell::String
30
+ end
31
+
32
+ def base_date
33
+ ::Date.new(1899, 12, 30)
34
+ end
35
+
36
+ def base_timestamp
37
+ ::Date.new(1899, 12, 30).to_datetime.to_time.to_i
38
+ end
39
+
40
+ def class_time
41
+ Roo::Excelx::Cell::Time
42
+ end
43
+
44
+ def test_cell_default_values
45
+ assert_values base.new(nil, nil, [], 1, nil, nil), default_type: :base, :@default_type => nil, style: 1, :@style => nil
46
+ assert_values boolean.new("1", nil, nil, nil, nil), default_type: :boolean, :@default_type => nil, cell_type: :boolean, :@cell_type => nil
47
+ assert_values class_date.new("41791", nil, [:numeric_or_formula, "mm-dd-yy"], 6, nil, base_date, nil), default_type: :date, :@default_type => nil
48
+ assert_values class_time.new("0.521", nil, [:numeric_or_formula, "hh:mm"], 6, nil, base_timestamp, nil), default_type: :time, :@default_type => nil
49
+ assert_values datetime.new("41791.521", nil, [:numeric_or_formula, "mm-dd-yy hh:mm"], 6, nil, base_timestamp, nil), default_type: :datetime, :@default_type => nil
50
+ assert_values empty.new(nil), default_type: nil, :@default_type => nil, style: nil, :@style => nil
51
+ assert_values number.new("42", nil, ["0"], nil, nil, nil), default_type: :float, :@default_type => nil
52
+ assert_values string.new("1", nil, nil, nil, nil), default_type: :string, :@default_type => nil, cell_type: :string, :@cell_type => nil
53
+
54
+ assert_values base.new(nil, nil, [], 2, nil, nil), style: 2, :@style => 2
55
+ end
56
+
57
+ def assert_values(object, value_hash)
58
+ value_hash.each do |attr_name, expected_value|
59
+ value = if attr_name.to_s.include?("@")
60
+ object.instance_variable_defined?(attr_name) ? object.instance_variable_get(attr_name) : nil
61
+ else
62
+ object.public_send(attr_name)
63
+ end
64
+
65
+ if expected_value
66
+ assert_equal expected_value, value
67
+ else
68
+ assert_nil value
69
+ end
70
+ end
71
+ end
72
+ end
@@ -25,6 +25,11 @@ class TestRooExcelxCellBase < Minitest::Test
25
25
  refute cell.empty?
26
26
  end
27
27
 
28
+ def test_presence
29
+ cell = base.new(value, nil, [], nil, nil, nil)
30
+ assert_equal cell, cell.presence
31
+ end
32
+
28
33
  def test_cell_type_is_formula
29
34
  formula = true
30
35
  cell = base.new(value, formula, [], nil, nil, nil)
@@ -2,12 +2,12 @@ require 'test_helper'
2
2
 
3
3
  class TestRooExcelxCellDateTime < Minitest::Test
4
4
  def test_cell_value_is_datetime
5
- cell = datetime.new('30000.323212', nil, ['mm-dd-yy'], nil, nil, base_date, nil)
5
+ cell = datetime.new('30000.323212', nil, ['mm-dd-yy'], nil, nil, base_timestamp, nil)
6
6
  assert_kind_of ::DateTime, cell.value
7
7
  end
8
8
 
9
9
  def test_cell_type_is_datetime
10
- cell = datetime.new('30000.323212', nil, [], nil, nil, base_date, nil)
10
+ cell = datetime.new('30000.323212', nil, [], nil, nil, base_timestamp, nil)
11
11
  assert_equal :datetime, cell.type
12
12
  end
13
13
 
@@ -19,7 +19,7 @@ class TestRooExcelxCellDateTime < Minitest::Test
19
19
  ['mmm-yy', 'JAN-15'],
20
20
  ['m/d/yy h:mm', '1/25/15 8:15']
21
21
  ].each do |format, formatted_value|
22
- cell = datetime.new '42029.34375', nil, [format], nil, nil, base_date, nil
22
+ cell = datetime.new '42029.34375', nil, [format], nil, nil, base_timestamp, nil
23
23
  assert_equal formatted_value, cell.formatted_value
24
24
  end
25
25
  end
@@ -30,7 +30,7 @@ class TestRooExcelxCellDateTime < Minitest::Test
30
30
  ['h:mm:ss000 mm/yy', '8:15:00000 01/15'],
31
31
  ['mmm yyy', '2015-01-25 08:15:00']
32
32
  ].each do |format, formatted_value|
33
- cell = datetime.new '42029.34375', nil, [format], nil, nil, base_date, nil
33
+ cell = datetime.new '42029.34375', nil, [format], nil, nil, base_timestamp, nil
34
34
  assert_equal formatted_value, cell.formatted_value
35
35
  end
36
36
  end
@@ -39,7 +39,7 @@ class TestRooExcelxCellDateTime < Minitest::Test
39
39
  Roo::Excelx::Cell::DateTime
40
40
  end
41
41
 
42
- def base_date
43
- Date.new(1899, 12, 30)
42
+ def base_timestamp
43
+ DateTime.new(1899, 12, 30).to_time.to_i
44
44
  end
45
45
  end
@@ -4,4 +4,15 @@ class TestRooExcelxCellEmpty < Minitest::Test
4
4
  def empty
5
5
  Roo::Excelx::Cell::Empty
6
6
  end
7
+
8
+ def test_empty?
9
+ cell = empty.new(nil)
10
+ assert_same true, cell.empty?
11
+ end
12
+
13
+ def test_nil_presence
14
+ cell = empty.new(nil)
15
+ assert_nil cell.presence
16
+ end
17
+
7
18
  end
@@ -25,6 +25,11 @@ class TestRooExcelxCellNumber < Minitest::Test
25
25
  assert_kind_of(Float, cell.value)
26
26
  end
27
27
 
28
+ def test_very_simple_scientific_notation
29
+ cell = Roo::Excelx::Cell::Number.new '1e6', nil, ['0'], nil, nil, nil
30
+ assert_kind_of(Float, cell.value)
31
+ end
32
+
28
33
  def test_percent
29
34
  cell = Roo::Excelx::Cell::Number.new '42.1', nil, ['0.00%'], nil, nil, nil
30
35
  assert_kind_of(Float, cell.value)
@@ -53,8 +58,12 @@ class TestRooExcelxCellNumber < Minitest::Test
53
58
  def test_formats
54
59
  [
55
60
  ['General', '1042'],
61
+ ['GENERAL', '1042'],
56
62
  ['0', '1042'],
63
+ ['000000', '001042'],
57
64
  ['0.00', '1042.00'],
65
+ ['0.0000', '1042.0000'],
66
+ ['0.000000000', '1042.000000000'],
58
67
  ['#,##0', '1,042'],
59
68
  ['#,##0.00', '1,042.00'],
60
69
  ['0%', '104200%'],
@@ -25,4 +25,24 @@ class TestRooExcelxCellString < Minitest::Test
25
25
  cell = string.new '0', nil, nil, nil, nil
26
26
  assert_equal '0', cell.value
27
27
  end
28
+
29
+ def test_not_empty?
30
+ cell = string.new '1', nil, nil, nil, nil
31
+ assert_equal false, cell.empty?
32
+ end
33
+
34
+ def test_empty?
35
+ cell = string.new '', nil, nil, nil, nil
36
+ assert_equal true, cell.empty?
37
+ end
38
+
39
+ def test_presence
40
+ cell = string.new '1', nil, nil, nil, nil
41
+ assert_equal cell, cell.presence
42
+ end
43
+
44
+ def test_nil_presence
45
+ cell = string.new '', nil, nil, nil, nil
46
+ assert_nil cell.presence
47
+ end
28
48
  end
@@ -5,8 +5,8 @@ class TestRooExcelxCellTime < Minitest::Test
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 = roo_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 = roo_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
@@ -44,7 +44,7 @@ class TestRooFormatterCSV < Minitest::Test
44
44
  # wird.
45
45
  # Besser: Methode um temporaeres Dir. portabel zu bestimmen
46
46
  def test_huge_document_to_csv
47
- skip unless ENV["LONG_RUN"]
47
+ skip_long_test
48
48
 
49
49
  original_csv_path = File.join(TESTDIR, "Bibelbund.csv")
50
50
  with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
@@ -71,7 +71,7 @@ class TestRooFormatterCSV < Minitest::Test
71
71
  end
72
72
 
73
73
  def test_bug_quotes_excelx
74
- skip unless ENV["LONG_RUN"]
74
+ skip_long_test
75
75
  # TODO: run this test with a much smaller document
76
76
  with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
77
77
  workbook.default_sheet = workbook.sheets.first
@@ -100,6 +100,23 @@ class TestRooFormatterCSV < Minitest::Test
100
100
  end
101
101
  end
102
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
+
103
120
  def test_true_class
104
121
  assert_equal "true", cell_to_csv(1, 1)
105
122
  end
@@ -3,20 +3,23 @@ require "test_helper"
3
3
  class TestRooFormatterXML < Minitest::Test
4
4
  def test_to_xml
5
5
  expected_sheet_count = 5
6
- with_each_spreadsheet(name: "numbers1", encoding: "utf8") do |workbook|
6
+ options = { name: "numbers1", encoding: "utf8" }
7
+ with_each_spreadsheet(options) do |workbook|
7
8
  skip if defined? JRUBY_VERSION
8
9
  workbook.to_xml
9
10
  sheetname = workbook.sheets.first
10
11
  doc = Nokogiri::XML(workbook.to_xml)
11
- all_cells = init_all_cells(workbook, sheetname)
12
12
 
13
13
  assert_equal expected_sheet_count, doc.xpath("//spreadsheet/sheet").count
14
14
 
15
15
  doc.xpath("//spreadsheet/sheet").each do |xml_sheet|
16
- assert_equal sheetname, xml_sheet.attributes["name"].value
17
- xml_sheet.children.each_with_index do |cell, i|
18
- next unless cell.attributes["name"]
16
+ all_cells = init_all_cells(workbook, sheetname)
17
+ cells = xml_sheet.children.reject(&:text?)
19
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|
20
23
  expected = [
21
24
  all_cells[i][:row],
22
25
  all_cells[i][:column],
@@ -24,11 +27,12 @@ class TestRooFormatterXML < Minitest::Test
24
27
  all_cells[i][:type],
25
28
  ]
26
29
  result = [
27
- cell.attributes["row"],
28
- cell.attributes["column"],
29
- cell.content,
30
- cell.attributes["type"],
30
+ cell["row"],
31
+ cell["column"],
32
+ cell.text,
33
+ cell["type"],
31
34
  ]
35
+
32
36
  assert_equal expected, result
33
37
  end # end of sheet
34
38
  sheetname = workbook.sheets[workbook.sheets.index(sheetname) + 1]
@@ -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