diff options
author | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2016-11-09 20:06:49 -0800 |
---|---|---|
committer | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2016-11-09 20:12:25 -0800 |
commit | 2d47cde5d6a2cf0aff885b19e5c2100389313481 (patch) | |
tree | b641dad40da38479047f61a101499485d2972c3e /tests | |
parent | c225b722cf154e1697f4ee15e7710f8c53ab5df6 (diff) |
tests: Add a generalized write-roundtrip test
This test tests two important invariants:
- Every combination of write flags has to result in a valid document
- Parsing that document and saving the result has to result in identical output
We don't test all flags since parse_no_escapes can intentionally result in
malformed documents and other flags aren't relevant for node output.
Also note that we test both no-whitespace and whitespace version to make sure
we don't have unnecessary whitespace added during formatting.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_write.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/test_write.cpp b/tests/test_write.cpp index 8fbcc27..de77339 100644 --- a/tests/test_write.cpp +++ b/tests/test_write.cpp @@ -608,6 +608,31 @@ TEST_XML(write_no_empty_element_tags, "<node><child1/><child2>text</child2><chil CHECK_NODE_EX(doc, STR("<node>\n\t<child1></child1>\n\t<child2>text</child2>\n\t<child3></child3>\n</node>\n"), STR("\t"), format_indent | format_no_empty_element_tags); } +TEST_XML_FLAGS(write_roundtrip, "<node><child1 attr1='value1' attr2='value2'/><child2 attr='value'>pre<![CDATA[data]]>mid<text&escape<!--comment--><test/>post<?pi value?>fin</child2><child3/></node>", parse_full) +{ + const unsigned int flagset[] = { format_indent, format_raw, format_no_declaration, format_indent_attributes, format_no_empty_element_tags }; + size_t flagcount = sizeof(flagset) / sizeof(flagset[0]); + + for (size_t i = 0; i < size_t(1 << flagcount); ++i) + { + unsigned int flags = 0; + + for (size_t j = 0; j < flagcount; ++j) + if (i & (1 << j)) + flags |= flagset[j]; + + std::string contents = write_narrow(doc, flags, encoding_utf8); + + pugi::xml_document verify; + CHECK(verify.load_buffer(contents.c_str(), contents.size(), parse_full)); + CHECK(test_write_narrow(verify, flags, encoding_utf8, contents.c_str(), contents.size())); + + pugi::xml_document verifyws; + CHECK(verifyws.load_buffer(contents.c_str(), contents.size(), parse_full | parse_ws_pcdata)); + CHECK(test_write_narrow(verifyws, flags, encoding_utf8, contents.c_str(), contents.size())); + } +} + #ifndef PUGIXML_NO_EXCEPTIONS struct throwing_writer: pugi::xml_writer { |