#pragma once #include #include #include class test_style_writer : public CxxTest::TestSuite { public: bool style_xml_matches(const std::string &expected_string, xlnt::workbook &wb) { std::vector bytes; wb.save(bytes); xlnt::zip_file archive; archive.load(bytes); pugi::xml_document observed; observed.load(archive.read(xlnt::constants::part_styles()).c_str()); return xml_helper::string_matches_document(expected_string, observed); } 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); xlnt::border b; xlnt::border::border_property prop; prop.set_style(xlnt::border_style::dashdot); prop.set_color(xlnt::rgb_color("ffff0000")); b.set_side(xlnt::border::side::top, prop); ws.get_cell("D10").set_border(b); auto expected = path_helper::get_data_directory("writer/expected/simple-styles.xml"); TS_ASSERT(style_xml_matches(expected.read_contents(), 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)); } void test_alignment() { xlnt::workbook wb; xlnt::alignment a; a.set_horizontal(xlnt::horizontal_alignment::center_continuous); a.set_vertical(xlnt::vertical_alignment::justify); a.set_wrap_text(true); a.set_shrink_to_fit(true); wb.get_active_sheet().get_cell("A1").set_alignment(a); std::string expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; TS_ASSERT(style_xml_matches(expected, wb)); } void test_fill() { xlnt::workbook wb; xlnt::fill pattern_fill = xlnt::fill::pattern(xlnt::pattern_fill::type::solid); pattern_fill.get_pattern_fill().set_foreground_color(xlnt::color::red()); pattern_fill.get_pattern_fill().set_background_color(xlnt::color::blue()); wb.get_active_sheet().get_cell("A1").set_fill(pattern_fill); xlnt::fill gradient_fill_linear = xlnt::fill::gradient(xlnt::gradient_fill::type::linear); gradient_fill_linear.get_gradient_fill().set_degree(90); wb.get_active_sheet().get_cell("A1").set_fill(gradient_fill_linear); xlnt::fill gradient_fill_path = xlnt::fill::gradient(xlnt::gradient_fill::type::path); gradient_fill_path.get_gradient_fill().set_gradient_left(1); gradient_fill_path.get_gradient_fill().set_gradient_right(2); gradient_fill_path.get_gradient_fill().set_gradient_top(3); gradient_fill_path.get_gradient_fill().set_gradient_bottom(4); gradient_fill_path.get_gradient_fill().add_stop(0, xlnt::color::red()); gradient_fill_path.get_gradient_fill().add_stop(1, xlnt::color::blue()); wb.get_active_sheet().get_cell("A1").set_fill(gradient_fill_path); std::string expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; TS_ASSERT(style_xml_matches(expected, wb)); } };