Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from roo-rb/test_refactoring
Refactored test cases into separate classes and modules
- Loading branch information
Showing
18 changed files
with
938 additions
and
892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
File: bbu%s | ||
Number of sheets: 3 | ||
Sheets: 2007_12, Tabelle2, Tabelle3 | ||
Sheet 1: | ||
First row: 1 | ||
Last row: 4 | ||
First column: A | ||
Last column: F | ||
Sheet 2: | ||
- empty - | ||
Sheet 3: | ||
- empty - |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
File: numbers1%s | ||
Number of sheets: 5 | ||
Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5 | ||
Sheet 1: | ||
First row: 1 | ||
Last row: 18 | ||
First column: A | ||
Last column: G | ||
Sheet 2: | ||
First row: 5 | ||
Last row: 14 | ||
First column: B | ||
Last column: E | ||
Sheet 3: | ||
First row: 1 | ||
Last row: 1 | ||
First column: A | ||
Last column: BA | ||
Sheet 4: | ||
First row: 1 | ||
Last row: 1 | ||
First column: A | ||
Last column: E | ||
Sheet 5: | ||
First row: 1 | ||
Last row: 6 | ||
First column: A | ||
Last column: E |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.