| 1 |
require "test_helper" |
| 2 |
|
| 3 |
class TestRooFormatterXML < Minitest::Test |
| 4 |
def test_to_xml |
| 5 |
expected_sheet_count = 5 |
| 6 |
options = { name: "numbers1", encoding: "utf8" } |
| 7 |
with_each_spreadsheet(options) do |workbook| |
| 8 |
skip if defined? JRUBY_VERSION |
| 9 |
workbook.to_xml |
| 10 |
sheetname = workbook.sheets.first |
| 11 |
doc = Nokogiri::XML(workbook.to_xml) |
| 12 |
|
| 13 |
assert_equal expected_sheet_count, doc.xpath("//spreadsheet/sheet").count |
| 14 |
|
| 15 |
doc.xpath("//spreadsheet/sheet").each do |xml_sheet| |
| 16 |
all_cells = init_all_cells(workbook, sheetname) |
| 17 |
cells = xml_sheet.children.reject(&:text?) |
| 18 |
|
| 19 |
assert_equal sheetname, xml_sheet["name"] |
| 20 |
assert_equal all_cells.size, cells.size |
| 21 |
|
| 22 |
cells.each_with_index do |cell, i| |
| 23 |
expected = [ |
| 24 |
all_cells[i][:row], |
| 25 |
all_cells[i][:column], |
| 26 |
all_cells[i][:content], |
| 27 |
all_cells[i][:type], |
| 28 |
] |
| 29 |
result = [ |
| 30 |
cell["row"], |
| 31 |
cell["column"], |
| 32 |
cell.text, |
| 33 |
cell["type"], |
| 34 |
] |
| 35 |
|
| 36 |
assert_equal expected, result |
| 37 |
end # end of sheet |
| 38 |
sheetname = workbook.sheets[workbook.sheets.index(sheetname) + 1] |
| 39 |
end |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
def test_bug_to_xml_with_empty_sheets |
| 44 |
with_each_spreadsheet(name: "emptysheets", format: [:openoffice, :excelx]) do |workbook| |
| 45 |
workbook.sheets.each do |sheet| |
| 46 |
assert_nil workbook.first_row, "first_row not nil in sheet #{sheet}" |
| 47 |
assert_nil workbook.last_row, "last_row not nil in sheet #{sheet}" |
| 48 |
assert_nil workbook.first_column, "first_column not nil in sheet #{sheet}" |
| 49 |
assert_nil workbook.last_column, "last_column not nil in sheet #{sheet}" |
| 50 |
assert_nil workbook.first_row(sheet), "first_row not nil in sheet #{sheet}" |
| 51 |
assert_nil workbook.last_row(sheet), "last_row not nil in sheet #{sheet}" |
| 52 |
assert_nil workbook.first_column(sheet), "first_column not nil in sheet #{sheet}" |
| 53 |
assert_nil workbook.last_column(sheet), "last_column not nil in sheet #{sheet}" |
| 54 |
end |
| 55 |
workbook.to_xml |
| 56 |
end |
| 57 |
end |
| 58 |
|
| 59 |
# Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher |
| 60 |
# Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute |
| 61 |
# nicht immer in der gleichen Reihenfolge erzeugt. |
| 62 |
def init_all_cells(workbook, sheet) |
| 63 |
all = [] |
| 64 |
workbook.first_row(sheet).upto(workbook.last_row(sheet)) do |row| |
| 65 |
workbook.first_column(sheet).upto(workbook.last_column(sheet)) do |col| |
| 66 |
next if workbook.empty?(row, col, sheet) |
| 67 |
|
| 68 |
all << { |
| 69 |
row: row.to_s, |
| 70 |
column: col.to_s, |
| 71 |
content: workbook.cell(row, col, sheet).to_s, |
| 72 |
type: workbook.celltype(row, col, sheet).to_s, |
| 73 |
} |
| 74 |
end |
| 75 |
end |
| 76 |
all |
| 77 |
end |
| 78 |
end |