diff options
| author | 2017-06-12 03:37:11 -0400 | |
|---|---|---|
| committer | 2017-06-12 03:37:11 -0400 | |
| commit | 8280a21a23d44aa90177e2bc041d0b8dc8556f4b (patch) | |
| tree | dadef7ee085c0e990a5070bd41b6a5b98c97f4fd /test/formatters | |
Import Upstream version 2.7.1upstream/2.7.1
Diffstat (limited to 'test/formatters')
| -rw-r--r-- | test/formatters/test_csv.rb | 119 | ||||
| -rw-r--r-- | test/formatters/test_matrix.rb | 76 | ||||
| -rw-r--r-- | test/formatters/test_xml.rb | 78 | ||||
| -rw-r--r-- | test/formatters/test_yaml.rb | 20 |
4 files changed, 293 insertions, 0 deletions
diff --git a/test/formatters/test_csv.rb b/test/formatters/test_csv.rb new file mode 100644 index 0000000..a4443f4 --- /dev/null +++ b/test/formatters/test_csv.rb @@ -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_long_test + + 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_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 + 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 diff --git a/test/formatters/test_matrix.rb b/test/formatters/test_matrix.rb new file mode 100644 index 0000000..f2cb51f --- /dev/null +++ b/test/formatters/test_matrix.rb @@ -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 diff --git a/test/formatters/test_xml.rb b/test/formatters/test_xml.rb new file mode 100644 index 0000000..c8390b6 --- /dev/null +++ b/test/formatters/test_xml.rb @@ -0,0 +1,78 @@ +require "test_helper" + +class TestRooFormatterXML < Minitest::Test + def test_to_xml + expected_sheet_count = 5 + 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) + + assert_equal expected_sheet_count, doc.xpath("//spreadsheet/sheet").count + + doc.xpath("//spreadsheet/sheet").each do |xml_sheet| + 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.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] + 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 diff --git a/test/formatters/test_yaml.rb b/test/formatters/test_yaml.rb new file mode 100644 index 0000000..dafc03e --- /dev/null +++ b/test/formatters/test_yaml.rb @@ -0,0 +1,20 @@ +require "test_helper" + +class TestRooFormatterYAML < Minitest::Test + def test_date_time_yaml + name = "time-test" + expected = File.open(TESTDIR + "/expected_results/#{name}.yml").read + with_each_spreadsheet(name: name) do |workbook| + assert_equal expected, workbook.to_yaml + end + end + + def test_bug_to_yaml_empty_sheet + formats = [:openoffice, :excelx] + with_each_spreadsheet(name: "emptysheets", format: formats) do |workbook| + workbook.default_sheet = workbook.sheets.first + workbook.to_yaml + assert_equal "", workbook.to_yaml + end + end +end |
