diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test.hpp | 17 | ||||
-rw-r--r-- | tests/test_write.cpp | 77 |
2 files changed, 87 insertions, 7 deletions
diff --git a/tests/test.hpp b/tests/test.hpp index 10e32c7..af3c63b 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -17,10 +17,10 @@ template <typename Node> inline bool test_node_name_value(const Node& node, cons return test_string_equal(node.name(), name) && test_string_equal(node.value(), value);
}
-inline bool test_node(const pugi::xml_node& node, const char* contents)
+inline bool test_node(const pugi::xml_node& node, const char* contents, const char* indent, unsigned int flags)
{
std::ostringstream oss;
- node.print(oss, "", pugi::format_raw);
+ node.print(oss, indent, flags);
return oss.str() == contents;
}
@@ -80,10 +80,13 @@ struct dummy_fixture {}; #define TEST_XML(name, xml) TEST_XML_FLAGS(name, xml, pugi::parse_default)
-#define CHECK(condition) if (condition) ; else throw #condition " is false"
-#define CHECK_STRING(value, expected) if (test_string_equal(value, expected)) ; else throw #value " is not equal to " #expected
-#define CHECK_DOUBLE(value, expected) if (fabs(value - expected) < 1e-6) ; else throw #value " is not equal to " #expected
-#define CHECK_NAME_VALUE(node, name, value) if (test_node_name_value(node, name, value)) ; else throw #node " name/value do not match " #name " and " #value
-#define CHECK_NODE(node, expected) if (test_node(node, expected)) ; else throw #node " contents does not match " #expected
+#define CHECK_TEXT(condition, text) if (condition) ; else throw text
+
+#define CHECK(condition) CHECK_TEXT(condition, #condition " is false")
+#define CHECK_STRING(value, expected) CHECK_TEXT(test_string_equal(value, expected), #value " is not equal to " #expected)
+#define CHECK_DOUBLE(value, expected) CHECK_TEXT(fabs(value - expected) < 1e-6, #value " is not equal to " #expected)
+#define CHECK_NAME_VALUE(node, name, value) CHECK_TEXT(test_node_name_value(node, name, value), #node " name/value do not match " #name " and " #value)
+#define CHECK_NODE_EX(node, expected, indent, flags) CHECK_TEXT(test_node(node, expected, indent, flags), #node " contents does not match " #expected)
+#define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, "", pugi::format_raw)
#endif
diff --git a/tests/test_write.cpp b/tests/test_write.cpp new file mode 100644 index 0000000..78516b2 --- /dev/null +++ b/tests/test_write.cpp @@ -0,0 +1,77 @@ +#include "common.hpp"
+
+#include <string>
+
+TEST_XML(write_simple, "<node attr='1'><child>text</child></node>")
+{
+ CHECK_NODE_EX(doc, "<node attr=\"1\">\n<child>text</child>\n</node>\n", "", 0);
+}
+
+TEST_XML(write_raw, "<node attr='1'><child>text</child></node>")
+{
+ CHECK_NODE_EX(doc, "<node attr=\"1\"><child>text</child></node>", "", pugi::format_raw);
+}
+
+TEST_XML(write_indent, "<node attr='1'><child><sub>text</sub></child></node>")
+{
+ CHECK_NODE_EX(doc, "<node attr=\"1\">\n\t<child>\n\t\t<sub>text</sub>\n\t</child>\n</node>\n", "\t", pugi::format_indent);
+}
+
+TEST_XML(write_pcdata, "<node attr='1'><child><sub/>text</child></node>")
+{
+ CHECK_NODE_EX(doc, "<node attr=\"1\">\n\t<child>\n\t\t<sub />\n\t\ttext\n\t</child>\n</node>\n", "\t", pugi::format_indent);
+}
+
+TEST_XML(write_cdata, "<![CDATA[value]]>")
+{
+ CHECK_NODE(doc, "<![CDATA[value]]>");
+}
+
+TEST_XML_FLAGS(write_comment, "<!--text-->", pugi::parse_default | pugi::parse_comments)
+{
+ CHECK_NODE(doc, "<!--text-->");
+}
+
+TEST_XML_FLAGS(write_pi, "<?name value?>", pugi::parse_default | pugi::parse_pi)
+{
+ CHECK_NODE(doc, "<?name value?>");
+}
+
+TEST_XML_FLAGS(write_declaration, "<?xml version='2.0'?>", pugi::parse_default | pugi::parse_declaration)
+{
+ CHECK_NODE(doc, "<?xml version=\"2.0\"?>");
+}
+
+TEST_XML(write_escape, "<node attr=''>text</node>")
+{
+ doc.child("node").attribute("attr") = "<>'\"&\x04\r\n\t";
+ doc.child("node").first_child().set_value("<>'\"&\x04\r\n\t");
+
+ CHECK_NODE(doc, "<node attr=\"<>'"& \t\"><>'\"&\r\n\t</node>");
+}
+
+struct test_writer: xml_writer
+{
+ std::string contents;
+
+ virtual void write(const void* data, size_t size)
+ {
+ contents += std::string(static_cast<const char*>(data), static_cast<const char*>(data) + size);
+ }
+};
+
+TEST_XML(write_print_writer, "<node/>")
+{
+ test_writer writer;
+ doc.print(writer);
+
+ CHECK(writer.contents == "<node />\n");
+}
+
+TEST_XML(write_print_stream, "<node/>")
+{
+ std::ostringstream oss;
+ doc.print(oss);
+
+ CHECK(oss.str() == "<node />\n");
+}
|