Skip to content

Commit

Permalink
improve style writing coverage. i think that's enough for today
Browse files Browse the repository at this point in the history
  • Loading branch information
tfussell committed Jul 9, 2016
1 parent 24ac76c commit 8970bfa
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -56,4 +56,4 @@ after_success:
- if [ "$COMPILER" = "g++-4.9" ]; then cd .. && ./build/bin/xlnt.test ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then export OLDWD=$(pwd) ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then cd "build/CMakeFiles/xlnt.static.dir$(pwd)" ; pwd ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then coveralls --root $OLDWD --verbose -x ".cpp" --gcov-options '\-p' --exclude include --exclude third-party --exclude tests --exclude samples --exclude benchmarks -E ".*/source/.*/tests/.*" ; fi
- if [ "$COMPILER" = "g++-4.9" ]; then coveralls --root $OLDWD --verbose -x ".cpp" --gcov-options '\-p' --exclude include --exclude third-party --exclude tests --exclude samples --exclude benchmarks -E ".*/source/.*/tests/.*" -E ".*/build/tests/runner-autogen.cpp.*" -E ".*CompilerIdCXX/.*" ; fi
6 changes: 4 additions & 2 deletions include/xlnt/styles/font.hpp
Expand Up @@ -80,16 +80,18 @@ class XLNT_CLASS font : public hashable

void set_family(std::size_t family);

bool has_scheme() const;

void set_scheme(const std::string &scheme);

std::string get_scheme() const;

color get_color() const;

bool has_family() const;

std::size_t get_family() const;

bool has_scheme() const;

protected:
std::string to_hash_string() const override;

Expand Down
2 changes: 1 addition & 1 deletion source/detail/style_serializer.cpp
Expand Up @@ -878,7 +878,7 @@ bool write_fonts(const std::vector<xlnt::font> &fonts, pugi::xml_node &fonts_nod
if (f.has_scheme())
{
auto scheme_node = font_node.append_child("scheme");
scheme_node.append_attribute("val").set_value("minor");
scheme_node.append_attribute("val").set_value(f.get_scheme().c_str());
}
}

Expand Down
5 changes: 5 additions & 0 deletions source/styles/font.cpp
Expand Up @@ -145,6 +145,11 @@ bool font::has_scheme() const
return has_scheme_;
}

std::string font::get_scheme() const
{
return scheme_;
}

std::string font::to_hash_string() const
{
std::string hash_string = "font";
Expand Down
154 changes: 132 additions & 22 deletions source/workbook/tests/test_style_writer.hpp
Expand Up @@ -11,22 +11,68 @@
class test_style_writer : public CxxTest::TestSuite
{
public:
void test_write_number_formats()
bool style_xml_matches(const std::string &expected_string, xlnt::workbook &wb)
{
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());
pugi::xml_document observed;
style_serializer.write_stylesheet(observed);
pugi::xml_document expected_doc;
std::string expected =
" <numFmts count=\"1\">"
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 =
"<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" mc:Ignorable=\"x14ac\">"
" <numFmts count=\"1\">"
" <numFmt formatCode=\"YYYY\" numFmtId=\"164\"></numFmt>"
" </numFmts>";
expected_doc.load(expected.c_str());
auto diff = Helper::compare_xml(expected_doc.child("numFmts"), observed.child("styleSheet").child("numFmts"));
TS_ASSERT(diff);
" </numFmts>"
" <fonts count=\"1\">"
" <font>"
" <sz val=\"12\"/>"
" <color theme=\"1\"/>"
" <name val=\"Calibri\"/>"
" <family val=\"2\"/>"
" <scheme val=\"minor\"/>"
" </font>"
" </fonts>"
" <fills count=\"2\">"
" <fill>"
" <patternFill patternType=\"none\"/>"
" </fill>"
" <fill>"
" <patternFill patternType=\"gray125\"/>"
" </fill>"
" </fills>"
" <borders count=\"1\">"
" <border>"
" <left/>"
" <right/>"
" <top/>"
" <bottom/>"
" <diagonal/>"
" </border>"
" </borders>"
" <cellStyleXfs count=\"1\">"
" <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\"/>"
" </cellStyleXfs>"
" <cellXfs count=\"2\">"
" <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\" xfId=\"0\"/>"
" <xf numFmtId=\"164\" fontId=\"0\" fillId=\"0\" borderId=\"0\" xfId=\"0\"/>"
" </cellXfs>"
" <cellStyles count=\"1\">"
" <cellStyle name=\"Normal\" xfId=\"0\" builtinId=\"0\"/>"
" </cellStyles>"
" <dxfs count=\"0\"/>"
" <tableStyles count=\"0\" defaultTableStyle=\"TableStyleMedium9\" defaultPivotStyle=\"PivotStyleMedium7\"/>"
"</styleSheet>";
TS_ASSERT(style_xml_matches(expected, wb));
}

void test_simple_styles()
Expand All @@ -47,20 +93,14 @@ class test_style_writer : public CxxTest::TestSuite
xlnt::protection hidden(true, true);
ws.get_cell("E1").set_protection(hidden);

xlnt::excel_serializer e(wb);
xlnt::style_serializer serializer(e.get_stylesheet());
pugi::xml_document xml;
serializer.write_stylesheet(xml);

TS_ASSERT(Helper::compare_xml(PathHelper::GetDataDirectory("/writer/expected/simple-styles.xml"), xml));
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;
xlnt::excel_serializer e(wb);
xlnt::style_serializer serializer(e.get_stylesheet());
auto expected =
std::string expected =
"<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" mc:Ignorable=\"x14ac\">"
" <fonts count=\"1\">"
" <font>"
Expand Down Expand Up @@ -100,8 +140,78 @@ class test_style_writer : public CxxTest::TestSuite
" <dxfs count=\"0\"/>"
" <tableStyles count=\"0\" defaultTableStyle=\"TableStyleMedium9\" defaultPivotStyle=\"PivotStyleMedium7\"/>"
"</styleSheet>";
pugi::xml_document xml;
serializer.write_stylesheet(xml);
TS_ASSERT(Helper::compare_xml(expected, xml));
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 =
"<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" mc:Ignorable=\"x14ac\">"
" <fonts count=\"2\">"
" <font>"
" <sz val=\"12\"/>"
" <color theme=\"1\"/>"
" <name val=\"Calibri\"/>"
" <family val=\"2\"/>"
" <scheme val=\"minor\"/>"
" </font>"
" <font>"
" <b val=\"1\"/>"
" <i val=\"1\"/>"
" <u val=\"double-accounting\"/>"
" <strike val=\"1\"/>"
" <sz val=\"21\"/>"
" <color rgb=\"ffff0000\"/>"
" <name val=\"Consolas\"/>"
" <family val=\"3\"/>"
" <scheme val=\"major\"/>"
" </font>"
" </fonts>"
" <fills count=\"2\">"
" <fill>"
" <patternFill patternType=\"none\"/>"
" </fill>"
" <fill>"
" <patternFill patternType=\"gray125\"/>"
" </fill>"
" </fills>"
" <borders count=\"1\">"
" <border>"
" <left/>"
" <right/>"
" <top/>"
" <bottom/>"
" <diagonal/>"
" </border>"
" </borders>"
" <cellStyleXfs count=\"1\">"
" <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\"/>"
" </cellStyleXfs>"
" <cellXfs count=\"2\">"
" <xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\" borderId=\"0\" xfId=\"0\"/>"
" <xf numFmtId=\"164\" fontId=\"0\" fillId=\"0\" borderId=\"0\" xfId=\"0\"/>"
" </cellXfs>"
" <cellStyles count=\"1\">"
" <cellStyle name=\"Normal\" xfId=\"0\" builtinId=\"0\"/>"
" </cellStyles>"
" <dxfs count=\"0\"/>"
" <tableStyles count=\"0\" defaultTableStyle=\"TableStyleMedium9\" defaultPivotStyle=\"PivotStyleMedium7\"/>"
"</styleSheet>";

TS_ASSERT(style_xml_matches(expected, wb));
}
};

0 comments on commit 8970bfa

Please sign in to comment.