#pragma once #include #include #include #include #include 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"); ws.auto_filter("A1:F1"); xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; serializer.write_workbook(xml); auto diff = Helper::compare_xml(PathHelper::read_file("workbook_auto_filter.xml"), xml); TS_ASSERT(!diff); } void test_write_hidden_worksheet() { xlnt::workbook wb; auto ws = wb.create_sheet(); ws.set_sheet_state(xlnt::sheet_state::hidden); wb.create_sheet(); xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; serializer.write_workbook(xml); std::string expected_string = "" " " " " " " " " " " " " " " " " " " " " ""; pugi::xml_document expected; expected.load(expected_string.c_str()); auto diff = Helper::compare_xml(expected, xml); TS_ASSERT(!diff); } void test_write_hidden_single_worksheet() { xlnt::workbook wb; auto ws = wb.get_active_sheet(); ws.set_sheet_state(xlnt::sheet_state::hidden); xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; TS_ASSERT_THROWS(serializer.write_workbook(xml), xlnt::value_error); } void test_write_empty_workbook() { xlnt::workbook wb; TemporaryFile file; xlnt::excel_serializer serializer(wb); serializer.save_workbook(file.GetFilename()); TS_ASSERT(PathHelper::FileExists(file.GetFilename())); } void test_write_virtual_workbook() { xlnt::workbook old_wb, new_wb; xlnt::excel_serializer serializer(old_wb); std::vector wb_bytes; serializer.save_virtual_workbook(wb_bytes); xlnt::excel_serializer deserializer(new_wb); deserializer.load_virtual_workbook(wb_bytes); TS_ASSERT(new_wb != nullptr); } void test_write_workbook_rels() { xlnt::workbook wb; xlnt::zip_file archive; xlnt::relationship_serializer serializer(archive); serializer.write_relationships(wb.get_relationships(), "xl/workbook.xml"); pugi::xml_document observed; observed.load(archive.read("xl/_rels/workbook.xml.rels").c_str()); auto filename = "workbook.xml.rels"; auto diff = Helper::compare_xml(PathHelper::read_file(filename), observed); TS_ASSERT(!diff); } void test_write_workbook_() { xlnt::workbook wb; xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; serializer.write_workbook(xml); auto filename = PathHelper::GetDataDirectory("/workbook.xml"); auto diff = Helper::compare_xml(PathHelper::read_file(filename), xml); TS_ASSERT(!diff); } void test_write_named_range() { xlnt::workbook wb; auto ws = wb.create_sheet(); wb.create_named_range("test_range", ws, "A1:B5"); xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; serializer.write_named_ranges(xml.root()); std::string expected = "" "'Sheet'!$A$1:$B$5" ""; auto diff = Helper::compare_xml(expected, xml); 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"); xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; serializer.write_workbook(xml); std::string expected = "" " " " " " " " " " " " " " " " " " " ""; auto diff = Helper::compare_xml(expected, xml); TS_ASSERT(!diff); } void test_write_root_rels() { xlnt::workbook wb; xlnt::zip_file archive; xlnt::relationship_serializer serializer(archive); serializer.write_relationships(wb.get_root_relationships(), ""); pugi::xml_document observed; observed.load(archive.read("_rels/.rels").c_str()); std::string expected = "" " " " " " " ""; auto diff = Helper::compare_xml(expected, observed); TS_ASSERT(diff); } private: xlnt::workbook wb_; };