#pragma once #include #include #include #include #include class test_style_writer : public CxxTest::TestSuite { public: void test_write_number_formats() { xlnt::workbook wb; wb.get_active_sheet().get_cell("A1").set_number_format(xlnt::number_format("YYYY")); xlnt::excel_serializer excel_serializer(wb); xlnt::style_serializer style_serializer(excel_serializer.get_stylesheet()); xlnt::xml_document observed; style_serializer.write_stylesheet(observed); xlnt::xml_document expected_doc; std::string expected = " " " " " "; expected_doc.from_string(expected); auto diff = Helper::compare_xml(expected_doc.get_child("numFmts"), observed.get_child("styleSheet").get_child("numFmts")); TS_ASSERT(diff); } /* class TestStyleWriter(object): void setup(self): self.workbook = Workbook() self.worksheet = self.workbook.create_sheet() void _test_no_style(self): w = StyleWriter(self.workbook) assert len(w.wb._cell_styles) == 1 # there is always the empty (defaul) style void _test_nb_style(self): for i in range(1, 6): cell = self.worksheet.cell(row=1, column=i) cell.font = Font(size=i) _ = cell.style_id w = StyleWriter(self.workbook) assert len(w.wb._cell_styles) == 6 # 5 + the default cell = self.worksheet.cell('A10') cell.border=Border(top=Side(border_style=borders.BORDER_THIN)) _ = cell.style_id w = StyleWriter(self.workbook) assert len(w.wb._cell_styles) == 7 void _test_default_xfs(self): w = StyleWriter(self.workbook) fonts = nft = borders = fills = DummyElement() w._write_cell_styles() xml = tostring(w._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_xfs_number_format(self): for idx, nf in enumerate(["0.0%", "0.00%", "0.000%"], 1): cell = self.worksheet.cell(row=idx, column=1) cell.number_format = nf _ = cell.style_id # add to workbook styles w = StyleWriter(self.workbook) w._write_cell_styles() expected = """ """ xml = tostring(w._root) diff = compare_xml(xml, expected) assert diff is None, diff void _test_xfs_fonts(self): cell = self.worksheet.cell('A1') cell.font = Font(size=12, bold=True) _ = cell.style_id # update workbook styles w = StyleWriter(self.workbook) w._write_cell_styles() xml = tostring(w._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_xfs_fills(self): cell = self.worksheet.cell('A1') cell.fill = fill=PatternFill(fill_type='solid', start_color=Color(colors.DARKYELLOW)) _ = cell.style_id # update workbook styles w = StyleWriter(self.workbook) w._write_cell_styles() xml = tostring(w._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_xfs_borders(self): cell = self.worksheet.cell('A1') cell.border=Border(top=Side(border_style=borders.BORDER_THIN, color=Color(colors.DARKYELLOW))) _ = cell.style_id # update workbook styles w = StyleWriter(self.workbook) w._write_cell_styles() xml = tostring(w._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_protection(self): cell = self.worksheet.cell('A1') cell.protection = Protection(locked=True, hidden=True) _ = cell.style_id w = StyleWriter(self.workbook) w._write_cell_styles() xml = tostring(w._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_named_styles(self): writer = StyleWriter(self.workbook) writer._write_named_styles() xml = tostring(writer._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_style_names(self): writer = StyleWriter(self.workbook) writer._write_style_names() xml = tostring(writer._root) expected = """ """ diff = compare_xml(xml, expected) assert diff is None, diff void _test_simple_styles(datadir): wb = Workbook(guess_types=True) ws = wb.active now = datetime.datetime.now() 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 writer = StyleWriter(wb) datadir.chdir() with open('simple-styles.xml') as reference_file: expected = reference_file.read() xml = writer.write_table() diff = compare_xml(xml, expected) assert diff is None, diff void _test_empty_workbook(): wb = Workbook() writer = StyleWriter(wb) expected = """ """ xml = writer.write_table() diff = compare_xml(xml, expected) assert diff is None, diff */ };