#pragma once #include #include #include #include #include #include class test_style_writer : public CxxTest::TestSuite { public: bool style_xml_matches(const std::string &expected_string, xlnt::workbook &wb) { xlnt::excel_serializer excel_serializer(wb); xlnt::style_serializer style_serializer(excel_serializer.get_stylesheet()); pugi::xml_document observed; style_serializer.write_stylesheet(observed); pugi::xml_document expected; expected.load(expected_string.c_str()); auto comparison = Helper::compare_xml(expected.root(), observed.root()); return (bool)comparison; } void test_write_custom_number_format() { xlnt::workbook wb; wb.get_active_sheet().get_cell("A1").set_number_format(xlnt::number_format("YYYY")); auto expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; TS_ASSERT(style_xml_matches(expected, wb)); } void test_simple_styles() { xlnt::workbook wb; wb.set_guess_types(true); auto ws = wb.get_active_sheet(); ws.get_cell("A1").set_value("12.34%"); auto now = xlnt::date::today(); ws.get_cell("A2").set_value(now); ws.get_cell("A3").set_value("This is a test"); ws.get_cell("A4").set_value("31.31415"); ws.get_cell("A5"); ws.get_cell("D9").set_number_format(xlnt::number_format::number_00()); xlnt::protection locked(true, false); ws.get_cell("D9").set_protection(locked); xlnt::protection hidden(true, true); ws.get_cell("E1").set_protection(hidden); std::string expected = PathHelper::read_file(PathHelper::GetDataDirectory("/writer/expected/simple-styles.xml")); TS_ASSERT(style_xml_matches(expected, wb)); } void test_empty_workbook() { xlnt::workbook wb; std::string expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; TS_ASSERT(style_xml_matches(expected, wb)); } void test_complex_font() { xlnt::font f; f.set_bold(true); f.set_color(xlnt::color::red()); f.set_family(3); f.set_italic(true); f.set_name("Consolas"); f.set_scheme("major"); f.set_size(21); f.set_strikethrough(true); f.set_underline(xlnt::font::underline_style::double_accounting); xlnt::workbook wb; wb.get_active_sheet().get_cell("A1").set_font(f); std::string expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; TS_ASSERT(style_xml_matches(expected, wb)); } };