#pragma once #include #include #include #include #include #include class test_write_workbook : public CxxTest::TestSuite { public: void test_write_auto_filter() { xlnt::workbook wb; auto ws = wb.get_active_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); TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory("/writer/expected/workbook_auto_filter.xml"), xml)); } void test_write_hidden_worksheet() { xlnt::workbook wb; auto ws = wb.get_active_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()); TS_ASSERT(xml_helper::compare_xml(expected.child("workbook").child("sheets"), xml.child("workbook").child("sheets"))); } 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::no_visible_worksheets); } void test_write_empty_workbook() { xlnt::workbook wb; temporary_file file; xlnt::excel_serializer serializer(wb); serializer.save_workbook(file.get_filename()); TS_ASSERT(path_helper::file_exists(file.get_filename())); } 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); } void test_write_workbook_rels() { xlnt::workbook wb; auto ws = wb.get_active_sheet(); ws.get_cell("A1").set_value("string"); 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()); TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory("/writer/expected/workbook.xml.rels"), observed)); } void test_write_workbook_part() { xlnt::workbook wb; xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; serializer.write_workbook(xml); auto filename = path_helper::get_data_directory("/writer/expected/workbook.xml"); TS_ASSERT(xml_helper::compare_xml(filename, xml)); } void test_write_named_range() { xlnt::workbook wb; auto ws = wb.get_active_sheet(); wb.create_named_range("test_range", ws, "$A$1:$B$5"); xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; auto root = xml.root().append_child("root"); serializer.write_named_ranges(root); std::string expected = "" "'Sheet'!$A$1:$B$5" ""; TS_ASSERT(xml_helper::compare_xml(expected, xml)); } 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 = "" " " " " " " " " " " " " " " " " " " ""; pugi::xml_document expected_xml; expected_xml.load(expected.c_str()); TS_ASSERT(xml_helper::compare_xml(expected_xml.child("workbook").child("workbookPr"), xml.child("workbook").child("workbookPr"))); } 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 = "" " " " " " " ""; TS_ASSERT(xml_helper::compare_xml(expected, observed)); } void test_write_shared_strings_with_runs() { xlnt::workbook wb; auto ws = wb.get_active_sheet(); auto cell = ws.get_cell("A1"); xlnt::text_run run; run.set_color("color"); run.set_family(12); run.set_font("font"); run.set_scheme("scheme"); run.set_size(13); run.set_string("string"); xlnt::text text; text.add_run(run); cell.set_value(text); pugi::xml_document xml; xlnt::shared_strings_serializer::write_shared_strings(wb.get_shared_strings(), xml); std::string expected = "" " " " " " " " " " " " " " " " " " " " string" " " " " ""; TS_ASSERT(xml_helper::compare_xml(expected, xml)); } void test_write_worksheet_order() { auto path = path_helper::get_data_directory("/genuine/tab_order.xlsx"); // Load an original workbook produced by Excel xlnt::workbook wb_src; { xlnt::excel_serializer serializer(wb_src); serializer.load_workbook(path); } // Save it to a new file, unmodified temporary_file file; { xlnt::excel_serializer serializer(wb_src); serializer.save_workbook(file.get_filename()); TS_ASSERT(path_helper::file_exists(file.get_filename())); } // Load it again xlnt::workbook wb_dst; { xlnt::excel_serializer serializer(wb_dst); serializer.load_workbook(file.get_filename()); } // Make sure the number of worksheets is the same auto count_src = std::distance(wb_src.begin(), wb_src.end()); auto count_dst = std::distance(wb_dst.begin(), wb_dst.end()); TS_ASSERT(count_src == count_dst); // Make sure the title of the first sheet matches auto ws1title_src = wb_src[0].get_title(); auto ws1title_dst = wb_dst[0].get_title(); TS_ASSERT(ws1title_src.compare(ws1title_dst) == 0); } private: xlnt::workbook wb_; };