xlnt/source/serialization/tests/test_write_workbook.hpp

188 lines
6.5 KiB
C++
Raw Normal View History

2015-10-14 12:03:48 +08:00
#pragma once
#include <iostream>
#include <cxxtest/TestSuite.h>
2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/workbook_serializer.hpp>
#include <xlnt/utils/exceptions.hpp>
2015-10-14 12:03:48 +08:00
#include <helpers/path_helper.hpp>
2015-10-14 12:03:48 +08:00
class test_write_workbook : public CxxTest::TestSuite
{
public:
void test_write_auto_filter()
{
xlnt::workbook wb;
auto ws = wb.create_sheet();
ws.get_cell("F42").set_value("hello");
2015-11-05 11:03:18 +08:00
ws.auto_filter("A1:F1");
2015-10-14 12:03:48 +08:00
2015-10-31 06:54:04 +08:00
xlnt::workbook_serializer serializer(wb);
auto observed = serializer.write_workbook();
auto diff = Helper::compare_xml(PathHelper::read_file("workbook_auto_filter.xml"), observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
void test_write_hidden_worksheet()
{
xlnt::workbook wb;
auto ws = wb.create_sheet();
ws.set_sheet_state(xlnt::page_setup::sheet_state::hidden);
wb.create_sheet();
2015-10-31 06:54:04 +08:00
xlnt::workbook_serializer serializer(wb);
auto observed = serializer.write_workbook();
xlnt::string expected_string =
2015-10-14 12:03:48 +08:00
"<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <workbookPr/>"
" <bookViews>"
" <workbookView activeTab=\"0\"/>"
" </bookViews>"
" <sheets>"
" <sheet name=\"Sheet\" sheetId=\"1\" state=\"hidden\" r:id=\"rId1\"/>"
" <sheet name=\"Sheet1\" sheetId=\"2\" r:id=\"rId2\"/>"
" </sheets>"
" <definedNames/>"
" <calcPr calcId=\"124519\" fullCalcOnLoad=\"1\"/>"
"</workbook>";
2015-10-31 06:54:04 +08:00
xlnt::xml_document expected;
expected.from_string(expected_string);
auto diff = Helper::compare_xml(expected, observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
void test_write_hidden_single_worksheet()
{
xlnt::workbook wb;
auto ws = wb.get_active_sheet();
ws.set_sheet_state(xlnt::page_setup::sheet_state::hidden);
2015-10-31 06:54:04 +08:00
xlnt::workbook_serializer serializer(wb);
TS_ASSERT_THROWS(serializer.write_workbook(), xlnt::value_error);
2015-10-14 12:03:48 +08:00
}
void test_write_empty_workbook()
{
xlnt::workbook wb;
TemporaryFile file;
2015-10-31 06:54:04 +08:00
xlnt::excel_serializer serializer(wb);
serializer.save_workbook(file.GetFilename());
TS_ASSERT(PathHelper::FileExists(file.GetFilename()));
2015-10-14 12:03:48 +08:00
}
void test_write_virtual_workbook()
{
2015-10-31 06:54:04 +08:00
xlnt::workbook old_wb, new_wb;
xlnt::excel_serializer serializer(old_wb);
std::vector<std::uint8_t> wb_bytes;
serializer.save_virtual_workbook(wb_bytes);
xlnt::excel_serializer deserializer(new_wb);
deserializer.load_virtual_workbook(wb_bytes);
2015-10-14 12:03:48 +08:00
TS_ASSERT(new_wb != nullptr);
}
void test_write_workbook_rels()
{
xlnt::workbook wb;
2015-10-31 06:54:04 +08:00
xlnt::zip_file archive;
2015-11-04 07:26:33 +08:00
xlnt::relationship_serializer serializer(archive);
serializer.write_relationships(wb.get_relationships(), "xl/workbook.xml");
2015-10-31 06:54:04 +08:00
xlnt::xml_document observed;
2015-11-02 12:52:19 +08:00
observed.from_string(archive.read("xl/_rels/workbook.xml.rels"));
2015-10-14 12:03:48 +08:00
auto filename = "workbook.xml.rels";
2015-10-31 06:54:04 +08:00
auto diff = Helper::compare_xml(PathHelper::read_file(filename), observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
void test_write_workbook_()
{
xlnt::workbook wb;
2015-10-31 06:54:04 +08:00
xlnt::workbook_serializer serializer(wb);
auto observed = serializer.write_workbook();
2015-10-15 06:05:13 +08:00
auto filename = PathHelper::GetDataDirectory("/workbook.xml");
2015-10-31 06:54:04 +08:00
auto diff = Helper::compare_xml(PathHelper::read_file(filename), observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
void test_write_named_range()
{
xlnt::workbook wb;
auto ws = wb.create_sheet();
2015-10-15 06:05:13 +08:00
wb.create_named_range("test_range", ws, "A1:B5");
2015-10-31 06:54:04 +08:00
xlnt::workbook_serializer serializer(wb);
auto observed_node = serializer.write_named_ranges();
xlnt::xml_document observed;
observed.add_child(observed_node);
xlnt::string expected =
2015-10-14 12:03:48 +08:00
"<root>"
"<s:definedName xmlns:s=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" name=\"test_range\">'Sheet'!$A$1:$B$5</s:definedName>"
"</root>";
2015-10-31 06:54:04 +08:00
auto diff = Helper::compare_xml(expected, observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
void test_read_workbook_code_name()
{
// with open(tmpl, "rb") as expected:
// TS_ASSERT(read_workbook_code_name(expected.read()) == code_name
}
void test_write_workbook_code_name()
{
xlnt::workbook wb;
wb.set_code_name("MyWB");
2015-10-31 06:54:04 +08:00
xlnt::workbook_serializer serializer(wb);
auto observed = serializer.write_workbook();
xlnt::string expected =
2015-10-14 12:03:48 +08:00
"<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
" <workbookPr codeName=\"MyWB\"/>"
" <bookViews>"
" <workbookView activeTab=\"0\"/>"
" </bookViews>"
" <sheets>"
" <sheet name=\"Sheet\" sheetId=\"1\" r:id=\"rId1\"/>"
" </sheets>"
" <definedNames/>"
" <calcPr calcId=\"124519\" fullCalcOnLoad=\"1\"/>"
"</workbook>";
2015-10-31 06:54:04 +08:00
auto diff = Helper::compare_xml(expected, observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
void test_write_root_rels()
{
xlnt::workbook wb;
2015-10-31 06:54:04 +08:00
xlnt::zip_file archive;
2015-11-04 07:26:33 +08:00
xlnt::relationship_serializer serializer(archive);
serializer.write_relationships(wb.get_root_relationships(), "");
2015-10-31 06:54:04 +08:00
xlnt::xml_document observed;
observed.from_string(archive.read("_rels/.rels"));
xlnt::string expected =
2015-10-14 12:03:48 +08:00
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"
" <Relationship Id=\"rId1\" Target=\"xl/workbook.xml\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\"/>"
" <Relationship Id=\"rId2\" Target=\"docProps/core.xml\" Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\"/>"
" <Relationship Id=\"rId3\" Target=\"docProps/app.xml\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\"/>"
"</Relationships>";
2015-10-31 06:54:04 +08:00
auto diff = Helper::compare_xml(expected, observed);
2015-10-14 12:03:48 +08:00
TS_ASSERT(!diff);
}
private:
xlnt::workbook wb_;
};