diff options
| author | 2017-06-12 03:37:11 -0400 | |
|---|---|---|
| committer | 2017-06-12 03:37:11 -0400 | |
| commit | 8280a21a23d44aa90177e2bc041d0b8dc8556f4b (patch) | |
| tree | dadef7ee085c0e990a5070bd41b6a5b98c97f4fd /test/helpers | |
Import Upstream version 2.7.1upstream/2.7.1
Diffstat (limited to 'test/helpers')
| -rw-r--r-- | test/helpers/test_accessing_files.rb | 60 | ||||
| -rw-r--r-- | test/helpers/test_comments.rb | 43 | ||||
| -rw-r--r-- | test/helpers/test_formulas.rb | 9 | ||||
| -rw-r--r-- | test/helpers/test_labels.rb | 103 | ||||
| -rw-r--r-- | test/helpers/test_sheets.rb | 55 | ||||
| -rw-r--r-- | test/helpers/test_styles.rb | 62 |
6 files changed, 332 insertions, 0 deletions
diff --git a/test/helpers/test_accessing_files.rb b/test/helpers/test_accessing_files.rb new file mode 100644 index 0000000..8732bc8 --- /dev/null +++ b/test/helpers/test_accessing_files.rb @@ -0,0 +1,60 @@ +# Tests for "Accessing Files" which includes opening and closing files. +module TestAccesingFiles + def test_close + with_each_spreadsheet(name: "numbers1") do |oo| + next unless (tempdir = oo.instance_variable_get("@tmpdir")) + oo.close + refute File.exist?(tempdir), "Expected #{tempdir} to be cleaned up" + end + end + + # NOTE: Ruby 2.4.0 changed the way GC works. The last Roo object created by + # with_each_spreadsheet wasn't getting GC'd until after the process + # ended. + # + # That behavior change broke this test. In order to fix it, I forked the + # process and passed the temp directories from the forked process in + # order to check if they were removed properly. + def test_finalize + skip if defined? JRUBY_VERSION + + read, write = IO.pipe + pid = Process.fork do + with_each_spreadsheet(name: "numbers1") do |oo| + write.puts oo.instance_variable_get("@tmpdir") + end + end + + Process.wait(pid) + write.close + tempdirs = read.read.split("\n") + read.close + + refute tempdirs.empty? + tempdirs.each do |tempdir| + refute File.exist?(tempdir), "Expected #{tempdir} to be cleaned up" + end + end + + def test_cleanup_on_error + # NOTE: This test was occasionally failing because when it started running + # other tests would have already added folders to the temp directory, + # polluting the directory. You'd end up in a situation where there + # would be less folders AFTER this ran than originally started. + # + # Instead, just use a custom temp directory to test the functionality. + ENV["ROO_TMP"] = Dir.tmpdir + "/test_cleanup_on_error" + Dir.mkdir(ENV["ROO_TMP"]) unless File.exist?(ENV["ROO_TMP"]) + expected_dir_contents = Dir.open(ENV["ROO_TMP"]).to_a + with_each_spreadsheet(name: "non_existent_file", ignore_errors: true) {} + + assert_equal expected_dir_contents, Dir.open(ENV["ROO_TMP"]).to_a + Dir.rmdir ENV["ROO_TMP"] if File.exist?(ENV["ROO_TMP"]) + ENV.delete "ROO_TMP" + end + + def test_name_with_leading_slash + xlsx = Roo::Excelx.new(File.join(TESTDIR, "/name_with_leading_slash.xlsx")) + assert_equal 1, xlsx.sheets.count + end +end diff --git a/test/helpers/test_comments.rb b/test/helpers/test_comments.rb new file mode 100644 index 0000000..2e26786 --- /dev/null +++ b/test/helpers/test_comments.rb @@ -0,0 +1,43 @@ +module TestComments + def test_comment + options = { name: "comments", format: [:openoffice, :libreoffice, :excelx] } + with_each_spreadsheet(options) do |oo| + assert_equal "Kommentar fuer B4", oo.comment("b", 4) + assert_equal "Kommentar fuer B5", oo.comment("b", 5) + end + end + + def test_no_comment + options = { name: "comments", format: [:openoffice, :excelx] } + with_each_spreadsheet(options) do |oo| + # There are no comments at the second sheet. + assert_nil oo.comment("b", 4, oo.sheets[1]) + end + end + + def test_comments + options = { name: "comments", format: [:openoffice, :libreoffice, :excelx] } + expexted_comments = [ + [4, 2, "Kommentar fuer B4"], + [5, 2, "Kommentar fuer B5"], + ] + + with_each_spreadsheet(options) do |oo| + assert_equal expexted_comments, oo.comments(oo.sheets.first), "comments error in class #{oo.class}" + end + end + + def test_empty_sheet_comments + options = { name: "emptysheets", format: [:openoffice, :excelx] } + with_each_spreadsheet(options) do |oo| + assert_equal [], oo.comments, "An empty sheet's formulas should be an empty array" + end + end + + def test_comments_from_google_sheet_exported_as_xlsx + expected_comments = [[1, 1, "this is a comment\n\t-Steven Daniels"]] + with_each_spreadsheet(name: "comments-google", format: [:excelx]) do |oo| + assert_equal expected_comments, oo.comments(oo.sheets.first), "comments error in class #{oo.class}" + end + end +end diff --git a/test/helpers/test_formulas.rb b/test/helpers/test_formulas.rb new file mode 100644 index 0000000..509fba4 --- /dev/null +++ b/test/helpers/test_formulas.rb @@ -0,0 +1,9 @@ +module TestFormulas + def test_empty_sheet_formulas + options = { name: "emptysheets", format: [:openoffice, :excelx] } + with_each_spreadsheet(options) do |oo| + oo.default_sheet = oo.sheets.first + assert_equal [], oo.formulas, "An empty sheet's formulas should be an empty array" + end + end +end diff --git a/test/helpers/test_labels.rb b/test/helpers/test_labels.rb new file mode 100644 index 0000000..1b867b5 --- /dev/null +++ b/test/helpers/test_labels.rb @@ -0,0 +1,103 @@ +module TestLabels + def test_labels + options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] } + expected_labels = [ + ["anton", [5, 3, "Sheet1"]], + ["berta", [4, 2, "Sheet1"]], + ["caesar", [7, 2, "Sheet1"]], + ] + with_each_spreadsheet(options) do |oo| + assert_equal expected_labels, oo.labels, "error with labels array in class #{oo.class}" + end + end + + def test_labeled_cells + options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] } + with_each_spreadsheet(options) do |oo| + oo.default_sheet = oo.sheets.first + begin + row, col = oo.label("anton") + rescue ArgumentError + puts "labels error at #{oo.class}" + raise + end + assert_equal 5, row + assert_equal 3, col + + row, col = oo.label("anton") + assert_equal "Anton", oo.cell(row, col) + + row, col = oo.label("berta") + assert_equal "Bertha", oo.cell(row, col) + + row, col = oo.label("caesar") + assert_equal "Cäsar", oo.cell(row, col) + + row, col = oo.label("never") + assert_nil row + assert_nil col + + row, col, sheet = oo.label("anton") + assert_equal 5, row + assert_equal 3, col + assert_equal "Sheet1", sheet + + assert_equal "Anton", oo.anton + assert_raises(NoMethodError) do + row, col = oo.never + end + + # Reihenfolge row, col,sheet analog zu #label + expected_labels = [ + ["anton", [5, 3, "Sheet1"]], + ["berta", [4, 2, "Sheet1"]], + ["caesar", [7, 2, "Sheet1"]], + ] + assert_equal expected_labels, oo.labels, "error with labels array in class #{oo.class}" + end + end + + def test_label + options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] } + with_each_spreadsheet(options) do |oo| + begin + row, col = oo.label("anton") + rescue ArgumentError + puts "labels error at #{oo.class}" + raise + end + + assert_equal 5, row, "error with label in class #{oo.class}" + assert_equal 3, col, "error with label in class #{oo.class}" + + row, col = oo.label("anton") + assert_equal "Anton", oo.cell(row, col), "error with label in class #{oo.class}" + + row, col = oo.label("berta") + assert_equal "Bertha", oo.cell(row, col), "error with label in class #{oo.class}" + + row, col = oo.label("caesar") + assert_equal "Cäsar", oo.cell(row, col), "error with label in class #{oo.class}" + + row, col = oo.label("never") + assert_nil row + assert_nil col + + row, col, sheet = oo.label("anton") + assert_equal 5, row + assert_equal 3, col + assert_equal "Sheet1", sheet + end + end + + def test_method_missing_anton + options = { name: "named_cells", format: [:openoffice, :excelx, :libreoffice] } + with_each_spreadsheet(options) do |oo| + # oo.default_sheet = oo.sheets.first + assert_equal "Anton", oo.anton + assert_raises(NoMethodError) do + oo.never + end + end + end +end diff --git a/test/helpers/test_sheets.rb b/test/helpers/test_sheets.rb new file mode 100644 index 0000000..aa18f6e --- /dev/null +++ b/test/helpers/test_sheets.rb @@ -0,0 +1,55 @@ +# NOTE: Putting these tests into modules in order to share them across different +# test classes, i.e. both TestRooExcelx and TestRooOpenOffice should run +# sheet related tests. +# +# This will allow me to reuse these test cases when I add new classes for +# Roo's future API. +# Sheet related tests +module TestSheets + def test_sheets + sheet_names = ["Tabelle1", "Name of Sheet 2", "Sheet3", "Sheet4", "Sheet5"] + with_each_spreadsheet(name: "numbers1") do |oo| + assert_equal sheet_names, oo.sheets + assert_raises(RangeError) { oo.default_sheet = "no_sheet" } + assert_raises(TypeError) { oo.default_sheet = [1, 2, 3] } + oo.sheets.each do |sheet_name| + oo.default_sheet = sheet_name + assert_equal sheet_name, oo.default_sheet + end + end + end + + def test_sheetname + bad_sheet_name = "non existing sheet name" + with_each_spreadsheet(name: "numbers1") do |oo| + oo.default_sheet = "Name of Sheet 2" + assert_equal "I am sheet 2", oo.cell("C", 5) + assert_raises(RangeError) { oo.default_sheet = bad_sheet_name } + assert_raises(RangeError) { oo.default_sheet = bad_sheet_name } + assert_raises(RangeError) { oo.cell("C", 5, bad_sheet_name) } + assert_raises(RangeError) { oo.celltype("C", 5, bad_sheet_name) } + assert_raises(RangeError) { oo.empty?("C", 5, bad_sheet_name) } + assert_raises(RangeError) { oo.formula?("C", 5, bad_sheet_name) } + assert_raises(RangeError) { oo.formula("C", 5, bad_sheet_name) } + assert_raises(RangeError) { oo.set("C", 5, 42, bad_sheet_name) } + assert_raises(RangeError) { oo.formulas(bad_sheet_name) } + assert_raises(RangeError) { oo.to_yaml({}, 1, 1, 1, 1, bad_sheet_name) } + end + end + + def test_info_doesnt_set_default_sheet + sheet_name = "Sheet3" + with_each_spreadsheet(name: "numbers1") do |oo| + oo.default_sheet = sheet_name + oo.info + assert_equal sheet_name, oo.default_sheet + end + end + + def test_bug_numbered_sheet_names + options = { name: "bug-numbered-sheet-names", format: :excelx } + with_each_spreadsheet(options) do |oo| + oo.each_with_pagename {} + end + end +end diff --git a/test/helpers/test_styles.rb b/test/helpers/test_styles.rb new file mode 100644 index 0000000..476fe7d --- /dev/null +++ b/test/helpers/test_styles.rb @@ -0,0 +1,62 @@ +module TestStyles + def test_cell_styles + # styles only valid in excel spreadsheets? + # TODO: what todo with other spreadsheet types + with_each_spreadsheet(name: "style", format: [:excelx]) do |oo| + # bold + assert_equal true, oo.font(1, 1).bold? + assert_equal false, oo.font(1, 1).italic? + assert_equal false, oo.font(1, 1).underline? + + # italic + assert_equal false, oo.font(2, 1).bold? + assert_equal true, oo.font(2, 1).italic? + assert_equal false, oo.font(2, 1).underline? + + # normal + assert_equal false, oo.font(3, 1).bold? + assert_equal false, oo.font(3, 1).italic? + assert_equal false, oo.font(3, 1).underline? + + # underline + assert_equal false, oo.font(4, 1).bold? + assert_equal false, oo.font(4, 1).italic? + assert_equal true, oo.font(4, 1).underline? + + # bold italic + assert_equal true, oo.font(5, 1).bold? + assert_equal true, oo.font(5, 1).italic? + assert_equal false, oo.font(5, 1).underline? + + # bold underline + assert_equal true, oo.font(6, 1).bold? + assert_equal false, oo.font(6, 1).italic? + assert_equal true, oo.font(6, 1).underline? + + # italic underline + assert_equal false, oo.font(7, 1).bold? + assert_equal true, oo.font(7, 1).italic? + assert_equal true, oo.font(7, 1).underline? + + # bolded row + assert_equal true, oo.font(8, 1).bold? + assert_equal false, oo.font(8, 1).italic? + assert_equal false, oo.font(8, 1).underline? + + # bolded col + assert_equal true, oo.font(9, 2).bold? + assert_equal false, oo.font(9, 2).italic? + assert_equal false, oo.font(9, 2).underline? + + # bolded row, italic col + assert_equal true, oo.font(10, 3).bold? + assert_equal true, oo.font(10, 3).italic? + assert_equal false, oo.font(10, 3).underline? + + # normal + assert_equal false, oo.font(11, 4).bold? + assert_equal false, oo.font(11, 4).italic? + assert_equal false, oo.font(11, 4).underline? + end + end +end |
