-
Notifications
You must be signed in to change notification settings - Fork 502
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 #367 from roo-rb/feature/roo_formatters
Extracted testing classes for formatters
- Loading branch information
Showing
12 changed files
with
371 additions
and
357 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
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,26 @@ | ||
--- | ||
cell_1_1: | ||
row: 1 | ||
col: 1 | ||
celltype: string | ||
value: Mittags: | ||
cell_1_2: | ||
row: 1 | ||
col: 2 | ||
celltype: time | ||
value: 12:13:14 | ||
cell_1_3: | ||
row: 1 | ||
col: 3 | ||
celltype: time | ||
value: 15:16:00 | ||
cell_1_4: | ||
row: 1 | ||
col: 4 | ||
celltype: time | ||
value: 23:00:00 | ||
cell_2_1: | ||
row: 2 | ||
col: 1 | ||
celltype: date | ||
value: 2007-11-21 |
Binary file not shown.
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,119 @@ | ||
require "test_helper" | ||
|
||
class TestRooFormatterCSV < Minitest::Test | ||
def test_date_time_to_csv | ||
with_each_spreadsheet(name: "time-test") do |workbook| | ||
Dir.mktmpdir do |tempdir| | ||
csv_output = File.join(tempdir, "time_test.csv") | ||
assert workbook.to_csv(csv_output) | ||
assert File.exist?(csv_output) | ||
assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}` | ||
# --strip-trailing-cr is needed because the test-file use 0A and | ||
# the test on an windows box generates 0D 0A as line endings | ||
end | ||
end | ||
end | ||
|
||
def test_boolean_to_csv | ||
with_each_spreadsheet(name: "boolean") do |workbook| | ||
Dir.mktmpdir do |tempdir| | ||
csv_output = File.join(tempdir,"boolean.csv") | ||
assert workbook.to_csv(csv_output) | ||
assert File.exist?(csv_output) | ||
assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}` | ||
# --strip-trailing-cr is needed because the test-file use 0A and | ||
# the test on an windows box generates 0D 0A as line endings | ||
end | ||
end | ||
end | ||
|
||
def test_link_to_csv | ||
with_each_spreadsheet(name: "link", format: :excelx) do |workbook| | ||
Dir.mktmpdir do |tempdir| | ||
csv_output = File.join(tempdir, "link.csv") | ||
assert workbook.to_csv(csv_output) | ||
assert File.exist?(csv_output) | ||
assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}` | ||
# --strip-trailing-cr is needed because the test-file use 0A and | ||
# the test on an windows box generates 0D 0A as line endings | ||
end | ||
end | ||
end | ||
|
||
# "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt | ||
# wird. | ||
# Besser: Methode um temporaeres Dir. portabel zu bestimmen | ||
def test_huge_document_to_csv | ||
skip unless ENV["LONG_RUN"] | ||
|
||
original_csv_path = File.join(TESTDIR, "Bibelbund.csv") | ||
with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook| | ||
Dir.mktmpdir do |tempdir| | ||
new_csv_path = File.join(tempdir, "Bibelbund.csv") | ||
assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", workbook.cell(45, "A") | ||
assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", workbook.cell(46, "A") | ||
assert_equal "Tagebuch aus Chile Juli 1977", workbook.cell(55, "A") | ||
assert workbook.to_csv(new_csv_path) | ||
assert File.exist?(new_csv_path) | ||
assert FileUtils.identical?(original_csv_path, new_csv_path), "error in class #{workbook.class}" | ||
end | ||
end | ||
end | ||
|
||
def test_bug_empty_sheet | ||
with_each_spreadsheet(name: "formula", format: [:openoffice, :excelx]) do |workbook| | ||
workbook.default_sheet = "Sheet3" # is an empty sheet | ||
Dir.mktmpdir do |tempdir| | ||
workbook.to_csv(File.join(tempdir, "emptysheet.csv")) | ||
assert_equal "", `cat #{File.join(tempdir, "emptysheet.csv")}` | ||
end | ||
end | ||
end | ||
|
||
def test_bug_quotes_excelx | ||
skip unless ENV["LONG_RUN"] | ||
# 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 | ||
assert_equal( | ||
'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"', | ||
workbook.cell("A", 76) | ||
) | ||
workbook.to_csv("csv#{$$}") | ||
assert_equal( | ||
'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"', | ||
workbook.cell("A", 78) | ||
) | ||
File.delete_if_exist("csv#{$$}") | ||
end | ||
end | ||
|
||
def test_bug_datetime_to_csv | ||
with_each_spreadsheet(name: "datetime") do |workbook| | ||
Dir.mktmpdir do |tempdir| | ||
datetime_csv_file = File.join(tempdir, "datetime.csv") | ||
|
||
assert workbook.to_csv(datetime_csv_file) | ||
assert File.exist?(datetime_csv_file) | ||
assert_equal "", file_diff("#{TESTDIR}/so_datetime.csv", datetime_csv_file) | ||
end | ||
end | ||
end | ||
|
||
def test_true_class | ||
assert_equal "true", cell_to_csv(1, 1) | ||
end | ||
|
||
def test_false_class | ||
assert_equal "false", cell_to_csv(2, 1) | ||
end | ||
|
||
def test_date_class | ||
assert_equal "2017-01-01", cell_to_csv(3, 1) | ||
end | ||
|
||
def cell_to_csv(row, col) | ||
filename = File.join(TESTDIR, "formula_cell_types.xlsx") | ||
Roo::Spreadsheet.open(filename).send("cell_to_csv", row, col, "Sheet1") | ||
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,76 @@ | ||
require "test_helper" | ||
require "matrix" | ||
|
||
class TestRooFormatterMatrix < Minitest::Test | ||
def test_matrix | ||
expected_result = Matrix[ | ||
[1.0, 2.0, 3.0], | ||
[4.0, 5.0, 6.0], | ||
[7.0, 8.0, 9.0] | ||
] | ||
with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook| | ||
workbook.default_sheet = workbook.sheets.first | ||
assert_equal expected_result, workbook.to_matrix | ||
end | ||
end | ||
|
||
def test_matrix_selected_range | ||
expected_result = Matrix[ | ||
[1.0, 2.0, 3.0], | ||
[4.0, 5.0, 6.0], | ||
[7.0, 8.0, 9.0] | ||
] | ||
with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook| | ||
workbook.default_sheet = "Sheet2" | ||
assert_equal expected_result, workbook.to_matrix(3, 4, 5, 6) | ||
end | ||
end | ||
|
||
def test_matrix_all_nil | ||
expected_result = Matrix[ | ||
[nil, nil, nil], | ||
[nil, nil, nil], | ||
[nil, nil, nil] | ||
] | ||
with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook| | ||
workbook.default_sheet = "Sheet2" | ||
assert_equal expected_result, workbook.to_matrix(10, 10, 12, 12) | ||
end | ||
end | ||
|
||
def test_matrix_values_and_nil | ||
expected_result = Matrix[ | ||
[1.0, nil, 3.0], | ||
[4.0, 5.0, 6.0], | ||
[7.0, 8.0, nil] | ||
] | ||
with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook| | ||
workbook.default_sheet = "Sheet3" | ||
assert_equal expected_result, workbook.to_matrix(1, 1, 3, 3) | ||
end | ||
end | ||
|
||
def test_matrix_specifying_sheet | ||
expected_result = Matrix[ | ||
[1.0, nil, 3.0], | ||
[4.0, 5.0, 6.0], | ||
[7.0, 8.0, nil] | ||
] | ||
with_each_spreadsheet(name: "matrix", format: :openoffice) do |workbook| | ||
workbook.default_sheet = workbook.sheets.first | ||
assert_equal expected_result, workbook.to_matrix(nil, nil, nil, nil, "Sheet3") | ||
end | ||
end | ||
|
||
# #to_matrix of an empty sheet should return an empty matrix and not result in | ||
# an error message | ||
# 2011-06-25 | ||
def test_bug_to_matrix_empty_sheet | ||
options = { name: "emptysheets", format: [:openoffice, :excelx] } | ||
with_each_spreadsheet(options) do |workbook| | ||
workbook.default_sheet = workbook.sheets.first | ||
workbook.to_matrix | ||
assert_equal(Matrix.empty(0, 0), workbook.to_matrix) | ||
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,74 @@ | ||
require "test_helper" | ||
|
||
class TestRooFormatterXML < Minitest::Test | ||
def test_to_xml | ||
expected_sheet_count = 5 | ||
with_each_spreadsheet(name: "numbers1", encoding: "utf8") 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"] | ||
|
||
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"], | ||
] | ||
assert_equal expected, result | ||
end # end of sheet | ||
sheetname = workbook.sheets[workbook.sheets.index(sheetname) + 1] | ||
end | ||
end | ||
end | ||
|
||
def test_bug_to_xml_with_empty_sheets | ||
with_each_spreadsheet(name: "emptysheets", format: [:openoffice, :excelx]) do |workbook| | ||
workbook.sheets.each do |sheet| | ||
assert_nil workbook.first_row, "first_row not nil in sheet #{sheet}" | ||
assert_nil workbook.last_row, "last_row not nil in sheet #{sheet}" | ||
assert_nil workbook.first_column, "first_column not nil in sheet #{sheet}" | ||
assert_nil workbook.last_column, "last_column not nil in sheet #{sheet}" | ||
assert_nil workbook.first_row(sheet), "first_row not nil in sheet #{sheet}" | ||
assert_nil workbook.last_row(sheet), "last_row not nil in sheet #{sheet}" | ||
assert_nil workbook.first_column(sheet), "first_column not nil in sheet #{sheet}" | ||
assert_nil workbook.last_column(sheet), "last_column not nil in sheet #{sheet}" | ||
end | ||
workbook.to_xml | ||
end | ||
end | ||
|
||
# Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher | ||
# Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute | ||
# nicht immer in der gleichen Reihenfolge erzeugt. | ||
def init_all_cells(workbook, sheet) | ||
all = [] | ||
workbook.first_row(sheet).upto(workbook.last_row(sheet)) do |row| | ||
workbook.first_column(sheet).upto(workbook.last_column(sheet)) do |col| | ||
next if workbook.empty?(row, col, sheet) | ||
|
||
all << { | ||
row: row.to_s, | ||
column: col.to_s, | ||
content: workbook.cell(row, col, sheet).to_s, | ||
type: workbook.celltype(row, col, sheet).to_s, | ||
} | ||
end | ||
end | ||
all | ||
end | ||
end |
Oops, something went wrong.