#pragma once #include #include #include "pugixml.hpp" #include #include #include #include class test_number_style : public CxxTest::TestSuite { public: void test_ctor() { xlnt::workbook wb; xlnt::excel_serializer excel_serializer(wb); xlnt::style_serializer style_serializer(excel_serializer.get_stylesheet()); xlnt::zip_file archive; xlnt::xml_document stylesheet_doc; style_serializer.write_stylesheet(stylesheet_doc); auto stylesheet_xml = stylesheet_doc.to_string(); const std::string expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; TS_ASSERT(Helper::compare_xml(expected, stylesheet_xml)); } void test_from_simple() { xlnt::xml_document doc; auto xml = PathHelper::read_file(PathHelper::GetDataDirectory("/reader/styles/simple-styles.xml")); doc.from_string(xml); xlnt::workbook wb; xlnt::excel_serializer e(wb); xlnt::style_serializer s(e.get_stylesheet()); TS_ASSERT(s.read_stylesheet(doc)); TS_ASSERT_EQUALS(e.get_stylesheet().number_formats.size(), 1); } void test_from_complex() { xlnt::xml_document doc; auto xml = PathHelper::read_file(PathHelper::GetDataDirectory("/reader/styles/complex-styles.xml")); doc.from_string(xml); xlnt::workbook wb; xlnt::excel_serializer e(wb); xlnt::style_serializer s(e.get_stylesheet()); TS_ASSERT(s.read_stylesheet(doc)); TS_ASSERT_EQUALS(e.get_stylesheet().borders.size(), 7); TS_ASSERT_EQUALS(e.get_stylesheet().fills.size(), 6); TS_ASSERT_EQUALS(e.get_stylesheet().fonts.size(), 8); TS_ASSERT_EQUALS(e.get_stylesheet().number_formats.size(), 0); } /* void _test_unprotected_cell() { datadir.chdir(); src = open ("worksheet_unprotected_style.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); styles = stylesheet.cell_styles; assert len(styles) == 3; // default is cells are locked assert styles[1] == StyleArray([4,0,0,0,0,0,0,0,0]); assert styles[2] == StyleArray([3,0,0,0,1,0,0,0,0]); } void _test_read_cell_style() { datadir.chdir(); src = open("empty-workbook-styles.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); styles = stylesheet.cell_styles; assert len(styles) == 2; assert styles[1] == StyleArray([0,0,0,9,0,0,0,0,1]; } void _test_read_xf_no_number_format() { datadir.chdir(); src = open("no_number_format.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); styles = stylesheet.cell_styles; assert len(styles) == 3; assert styles[1] == StyleArray([1,0,1,0,0,0,0,0,0]); assert styles[2] == StyleArray([0,0,0,14,0,0,0,0,0]); } void _test_none_values() { datadir.chdir(); src = open("none_value_styles.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); fonts = stylesheet.fonts; assert fonts[0].scheme is None; assert fonts[0].vertAlign is None; assert fonts[1].u is None; } void _test_alignment() { datadir.chdir(); src = open("alignment_styles.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); styles = stylesheet.cell_styles; assert len(styles) == 3; assert styles[2] == StyleArray([0,0,0,0,0,2,0,0,0]); assert stylesheet.alignments == [ Alignment(), Alignment(textRotation=180), Alignment(vertical='top', textRotation=255), ]; } void _test_rgb_colors() { datadir.chdir(); src = open("rgb_colors.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); assert len(stylesheet.colors.index) == 64; assert stylesheet.colors.index[0] == "00000000"; assert stylesheet.colors.index[-1] == "00333333"; } void _test_custom_number_formats() { datadir.chdir(); src = open("styles_number_formats.xml", "rb"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); assert stylesheet.number_formats == [ '_ * #,##0.00_ ;_ * \-#,##0.00_ ;_ * "-"??_ ;_ @_ ', "#,##0.00_ ", "yyyy/m/d;@", "0.00000_ " ]; } void _test_assign_number_formats() { node = fromstring( "" "" " " "" "" "" " " "" "" ""); stylesheet = Stylesheet.from_tree(node); styles = stylesheet.cell_styles; assert styles[0] == StyleArray([2, 0, 0, 164, 0, 1, 0, 0, 0]); } void _test_named_styles() { datadir.chdir(); src = open("complex-styles.xml"); xml = src.read(); node = fromstring(xml); stylesheet = Stylesheet.from_tree(node); followed = stylesheet.named_styles['Followed Hyperlink']; assert followed.name == "Followed Hyperlink"; assert followed.font == stylesheet.fonts[2]; assert followed.fill == DEFAULT_EMPTY_FILL; assert followed.border == Border(); link = stylesheet.named_styles['Hyperlink']; assert link.name == "Hyperlink"; assert link.font == stylesheet.fonts[1]; assert link.fill == DEFAULT_EMPTY_FILL; assert link.border == Border(); normal = stylesheet.named_styles['Normal']; assert normal.name == "Normal"; assert normal.font == stylesheet.fonts[0]; assert normal.fill == DEFAULT_EMPTY_FILL; assert normal.border == Border(); } void _test_no_styles() { wb1 = wb2 = Workbook(); archive = ZipFile(BytesIO(), "a"); apply_stylesheet(archive, wb1); assert wb1._cell_styles == wb2._cell_styles; assert wb2._named_styles == wb2._named_styles; } void _test_write_worksheet() { wb = Workbook() node = write_stylesheet(wb); xml = tostring(node); const std::string expected = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "" ""; diff = compare_xml(xml, expected); assert diff is None, diff; } void _test_simple_styles() { wb = Workbook(); wb.guess_types = True; ws = wb.active; now = datetime.date.today(); for idx, v in enumerate(['12.34%', now, 'This is a test', '31.31415', None], 1) { ws.append([v]); _ = ws.cell(column=1, row=idx).style_id; } // set explicit formats ws['D9'].number_format = numbers.FORMAT_NUMBER_00; ws['D9'].protection = Protection(locked=True); ws['D9'].style_id; ws['E1'].protection = Protection(hidden=True); ws['E1'].style_id; assert len(wb._cell_styles) == 5; stylesheet = write_stylesheet(wb); datadir.chdir(); reference_file = open('simple-styles.xml'); expected = reference_file.read(); xml = tostring(stylesheet); diff = compare_xml(xml, expected); assert diff is None, diff; } */ };