Skip to content

Commit

Permalink
Merge pull request #370 from roo-rb/test_refactoring
Browse files Browse the repository at this point in the history
Refactored test cases into separate classes and modules
  • Loading branch information
stevendaniels committed Jan 3, 2017
2 parents a99a179 + fbaa333 commit 3bddcc5
Show file tree
Hide file tree
Showing 18 changed files with 938 additions and 892 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -266,6 +266,9 @@ Roo's public methods have stayed relatively consistent between 1.13.x and 2.0.0,
Roo uses Minitest and RSpec. The best of both worlds! Run `bundle exec rake` to
run the tests/examples.

You can run the tests/examples with Rspec like reporters by running
`USE_REPORTERS=true bundle exec rake`

Roo also has a few tests that take a long time (5+ seconds). To run these, use
`LONG_RUN=true bundle exec rake`

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -5,5 +5,5 @@
RSpec.configure do |c|
c.include Helpers
c.color = true
c.formatter = :documentation
c.formatter = :documentation if ENV["USE_REPORTERS"]
end
12 changes: 12 additions & 0 deletions test/files/expected_results/bbu_info.txt
@@ -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 -
28 changes: 28 additions & 0 deletions test/files/expected_results/numbers_info.yml
@@ -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
4 changes: 2 additions & 2 deletions test/formatters/test_csv.rb
Expand Up @@ -44,7 +44,7 @@ def test_link_to_csv
# wird.
# Besser: Methode um temporaeres Dir. portabel zu bestimmen
def test_huge_document_to_csv
skip unless ENV["LONG_RUN"]
skip_long_test

original_csv_path = File.join(TESTDIR, "Bibelbund.csv")
with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
Expand All @@ -71,7 +71,7 @@ def test_bug_empty_sheet
end

def test_bug_quotes_excelx
skip unless ENV["LONG_RUN"]
skip_long_test
# TODO: run this test with a much smaller document
with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
workbook.default_sheet = workbook.sheets.first
Expand Down
22 changes: 13 additions & 9 deletions test/formatters/test_xml.rb
Expand Up @@ -3,32 +3,36 @@
class TestRooFormatterXML < Minitest::Test
def test_to_xml
expected_sheet_count = 5
with_each_spreadsheet(name: "numbers1", encoding: "utf8") do |workbook|
options = { name: "numbers1", encoding: "utf8" }
with_each_spreadsheet(options) do |workbook|
skip if defined? JRUBY_VERSION
workbook.to_xml
sheetname = workbook.sheets.first
doc = Nokogiri::XML(workbook.to_xml)
all_cells = init_all_cells(workbook, sheetname)

assert_equal expected_sheet_count, doc.xpath("//spreadsheet/sheet").count

doc.xpath("//spreadsheet/sheet").each do |xml_sheet|
assert_equal sheetname, xml_sheet.attributes["name"].value
xml_sheet.children.each_with_index do |cell, i|
next unless cell.attributes["name"]
all_cells = init_all_cells(workbook, sheetname)
cells = xml_sheet.children.reject(&:text?)

assert_equal sheetname, xml_sheet.attribute("name").value
assert_equal all_cells.size, cells.size

cells.each_with_index do |cell, i|
expected = [
all_cells[i][:row],
all_cells[i][:column],
all_cells[i][:content],
all_cells[i][:type],
]
result = [
cell.attributes["row"],
cell.attributes["column"],
cell.content,
cell.attributes["type"],
cell.attribute("row").value,
cell.attribute("column").value,
cell.text,
cell.attribute("type").value,
]

assert_equal expected, result
end # end of sheet
sheetname = workbook.sheets[workbook.sheets.index(sheetname) + 1]
Expand Down
60 changes: 60 additions & 0 deletions 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
43 changes: 43 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions 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
103 changes: 103 additions & 0 deletions 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
55 changes: 55 additions & 0 deletions 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

0 comments on commit 3bddcc5

Please sign in to comment.