2009-10-12 16:26:18 +00:00
|
|
|
#include "common.hpp"
|
|
|
|
|
2010-05-06 20:28:36 +00:00
|
|
|
#include "writer_string.hpp"
|
|
|
|
|
2009-10-12 16:26:18 +00:00
|
|
|
#include <string>
|
2009-10-29 07:17:30 +00:00
|
|
|
#include <sstream>
|
2009-10-12 16:26:18 +00:00
|
|
|
|
|
|
|
TEST_XML(write_simple, "<node attr='1'><child>text</child></node>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n<child>text</child>\n</node>\n"), STR(""), 0);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_XML(write_raw, "<node attr='1'><child>text</child></node>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE_EX(doc, STR("<node attr=\"1\"><child>text</child></node>"), STR(""), format_raw);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_XML(write_indent, "<node attr='1'><child><sub>text</sub></child></node>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub>text</sub>\n\t</child>\n</node>\n"), STR("\t"), format_indent);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_XML(write_pcdata, "<node attr='1'><child><sub/>text</child></node>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub />\n\t\ttext\n\t</child>\n</node>\n"), STR("\t"), format_indent);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_XML(write_cdata, "<![CDATA[value]]>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE(doc, STR("<![CDATA[value]]>"));
|
|
|
|
CHECK_NODE_EX(doc, STR("<![CDATA[value]]>\n"), STR(""), 0);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 20:25:35 +00:00
|
|
|
TEST_XML_FLAGS(write_comment, "<!--text-->", parse_default | parse_comments)
|
2009-10-12 16:26:18 +00:00
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE(doc, STR("<!--text-->"));
|
|
|
|
CHECK_NODE_EX(doc, STR("<!--text-->\n"), STR(""), 0);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 20:25:35 +00:00
|
|
|
TEST_XML_FLAGS(write_pi, "<?name value?>", parse_default | parse_pi)
|
2009-10-12 16:26:18 +00:00
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE(doc, STR("<?name value?>"));
|
|
|
|
CHECK_NODE_EX(doc, STR("<?name value?>\n"), STR(""), 0);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 20:25:35 +00:00
|
|
|
TEST_XML_FLAGS(write_declaration, "<?xml version='2.0'?>", parse_default | parse_declaration)
|
2009-10-12 16:26:18 +00:00
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK_NODE(doc, STR("<?xml version=\"2.0\"?>"));
|
|
|
|
CHECK_NODE_EX(doc, STR("<?xml version=\"2.0\"?>\n"), STR(""), 0);
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_XML(write_escape, "<node attr=''>text</node>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
doc.child(STR("node")).attribute(STR("attr")) = STR("<>'\"&\x04\r\n\t");
|
|
|
|
doc.child(STR("node")).first_child().set_value(STR("<>'\"&\x04\r\n\t"));
|
|
|
|
|
|
|
|
CHECK_NODE(doc, STR("<node attr=\"<>'"& \t\"><>'\"&\r\n\t</node>"));
|
|
|
|
}
|
2009-10-12 16:26:18 +00:00
|
|
|
|
2010-05-06 20:28:36 +00:00
|
|
|
TEST_XML(write_escape_unicode, "<node attr='㰀'/>")
|
|
|
|
{
|
|
|
|
#ifdef PUGIXML_WCHAR_MODE
|
|
|
|
#ifdef U_LITERALS
|
|
|
|
CHECK_NODE(doc, STR("<node attr=\"\u3c00\" />"));
|
|
|
|
#else
|
|
|
|
CHECK_NODE(doc, STR("<node attr=\"\x3c00\" />"));
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
CHECK_NODE(doc, STR("<node attr=\"\xe3\xb0\x80\" />"));
|
|
|
|
#endif
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct test_writer: xml_writer
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
std::basic_string<pugi::char_t> contents;
|
2009-10-12 16:26:18 +00:00
|
|
|
|
|
|
|
virtual void write(const void* data, size_t size)
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK(size % sizeof(pugi::char_t) == 0);
|
|
|
|
contents += std::basic_string<pugi::char_t>(static_cast<const pugi::char_t*>(data), static_cast<const pugi::char_t*>(data) + size / sizeof(pugi::char_t));
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_XML(write_print_writer, "<node/>")
|
|
|
|
{
|
|
|
|
test_writer writer;
|
2010-05-06 20:28:36 +00:00
|
|
|
doc.print(writer, STR(""), format_default, get_native_encoding());
|
2009-10-12 16:26:18 +00:00
|
|
|
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK(writer.contents == STR("<node />\n"));
|
2009-10-12 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
2009-10-29 07:17:30 +00:00
|
|
|
#ifndef PUGIXML_NO_STL
|
2009-10-12 16:26:18 +00:00
|
|
|
TEST_XML(write_print_stream, "<node/>")
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
2010-05-06 20:28:36 +00:00
|
|
|
doc.print(oss, STR(""), format_default, encoding_utf8);
|
2009-10-12 16:26:18 +00:00
|
|
|
|
|
|
|
CHECK(oss.str() == "<node />\n");
|
|
|
|
}
|
2010-05-06 20:28:36 +00:00
|
|
|
|
|
|
|
TEST_XML(write_print_stream_encode, "<n/>")
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
doc.print(oss, STR(""), format_default, encoding_utf16_be);
|
|
|
|
|
|
|
|
CHECK(oss.str() == std::string("\x00<\x00n\x00 \x00/\x00>\x00\n", 12));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_XML(write_print_stream_wide, "<node/>")
|
|
|
|
{
|
|
|
|
std::basic_ostringstream<wchar_t> oss;
|
|
|
|
doc.print(oss, STR(""), format_default, encoding_utf8);
|
|
|
|
|
|
|
|
CHECK(oss.str() == L"<node />\n");
|
|
|
|
}
|
2009-10-29 07:17:30 +00:00
|
|
|
#endif
|
2009-10-20 20:25:35 +00:00
|
|
|
|
|
|
|
TEST_XML(write_huge_chunk, "<node/>")
|
|
|
|
{
|
2010-05-06 20:28:36 +00:00
|
|
|
std::basic_string<pugi::char_t> name(10000, STR('n'));
|
|
|
|
doc.child(STR("node")).set_name(name.c_str());
|
2009-10-20 20:25:35 +00:00
|
|
|
|
2009-10-29 07:17:30 +00:00
|
|
|
test_writer writer;
|
2010-05-06 20:28:36 +00:00
|
|
|
doc.print(writer, STR(""), format_default, get_native_encoding());
|
2009-10-20 20:25:35 +00:00
|
|
|
|
2010-05-06 20:28:36 +00:00
|
|
|
CHECK(writer.contents == STR("<") + name + STR(" />\n"));
|
2009-10-20 20:25:35 +00:00
|
|
|
}
|
2010-05-06 20:28:36 +00:00
|
|
|
|
|
|
|
TEST(write_encodings)
|
|
|
|
{
|
|
|
|
static char s_utf8[] = "<\x54\xC2\xA2\xE2\x82\xAC\xF0\xA4\xAD\xA2/>";
|
|
|
|
|
|
|
|
xml_document doc;
|
|
|
|
CHECK(doc.load_buffer(s_utf8, sizeof(s_utf8), parse_default, encoding_utf8));
|
|
|
|
|
|
|
|
CHECK(write_narrow(doc, format_default, encoding_utf8) == "<\x54\xC2\xA2\xE2\x82\xAC\xF0\xA4\xAD\xA2 />\n");
|
|
|
|
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf32_le, "<\x00\x00\x00\x54\x00\x00\x00\xA2\x00\x00\x00\xAC\x20\x00\x00\x62\x4B\x02\x00 \x00\x00\x00/\x00\x00\x00>\x00\x00\x00\n\x00\x00\x00", 36));
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf32_be, "\x00\x00\x00<\x00\x00\x00\x54\x00\x00\x00\xA2\x00\x00\x20\xAC\x00\x02\x4B\x62\x00\x00\x00 \x00\x00\x00/\x00\x00\x00>\x00\x00\x00\n", 36));
|
|
|
|
CHECK(write_narrow(doc, format_default, encoding_utf32) == write_narrow(doc, format_default, is_little_endian() ? encoding_utf32_le : encoding_utf32_be));
|
|
|
|
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf16_le, "<\x00\x54\x00\xA2\x00\xAC\x20\x52\xd8\x62\xdf \x00/\x00>\x00\n\x00", 20));
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf16_be, "\x00<\x00\x54\x00\xA2\x20\xAC\xd8\x52\xdf\x62\x00 \x00/\x00>\x00\n", 20));
|
|
|
|
CHECK(write_narrow(doc, format_default, encoding_utf16) == write_narrow(doc, format_default, is_little_endian() ? encoding_utf16_le : encoding_utf16_be));
|
|
|
|
|
|
|
|
size_t wcharsize = sizeof(wchar_t);
|
|
|
|
std::wstring v = write_wide(doc, format_default, encoding_wchar);
|
|
|
|
|
|
|
|
if (wcharsize == 4)
|
|
|
|
{
|
|
|
|
CHECK(v.size() == 9 && v[0] == '<' && v[1] == 0x54 && v[2] == 0xA2 && v[3] == 0x20AC && v[4] == wchar_cast(0x24B62) && v[5] == ' ' && v[6] == '/' && v[7] == '>' && v[8] == '\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CHECK(v.size() == 10 && v[0] == '<' && v[1] == 0x54 && v[2] == 0xA2 && v[3] == 0x20AC && v[4] == 0xd852 && v[5] == 0xdf62 && v[6] == ' ' && v[7] == '/' && v[8] == '>' && v[9] == '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PUGIXML_WCHAR_MODE
|
|
|
|
TEST(write_encoding_huge)
|
|
|
|
{
|
|
|
|
const unsigned int N = 16000;
|
|
|
|
|
|
|
|
// make a large utf16 name consisting of 6-byte char pairs (6 does not divide internal buffer size, so will need split correction)
|
|
|
|
std::string s_utf16 = std::string("\x00<", 2);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < N; ++i) s_utf16 += "\x20\xAC\xd8\x52\xdf\x62";
|
|
|
|
|
|
|
|
s_utf16 += std::string("\x00/\x00>", 4);
|
|
|
|
|
|
|
|
xml_document doc;
|
|
|
|
CHECK(doc.load_buffer(&s_utf16[0], s_utf16.length(), parse_default, encoding_utf16_be));
|
|
|
|
|
|
|
|
std::string s_utf8 = "<";
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < N; ++j) s_utf8 += "\xE2\x82\xAC\xF0\xA4\xAD\xA2";
|
|
|
|
|
|
|
|
s_utf8 += " />\n";
|
|
|
|
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf8, s_utf8.c_str(), s_utf8.length()));
|
|
|
|
}
|
2010-05-30 20:17:05 +00:00
|
|
|
|
|
|
|
TEST(write_encoding_huge_invalid)
|
|
|
|
{
|
|
|
|
size_t wcharsize = sizeof(wchar_t);
|
|
|
|
|
|
|
|
if (wcharsize == 2)
|
|
|
|
{
|
|
|
|
const unsigned int N = 16000;
|
|
|
|
|
|
|
|
// make a large utf16 name consisting of leading surrogate chars
|
|
|
|
std::basic_string<wchar_t> s_utf16;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < N; ++i) s_utf16 += static_cast<wchar_t>(0xd852);
|
|
|
|
|
|
|
|
xml_document doc;
|
|
|
|
doc.append_child().set_name(s_utf16.c_str());
|
|
|
|
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf8, "< />\n", 5));
|
|
|
|
}
|
|
|
|
}
|
2010-05-06 20:28:36 +00:00
|
|
|
#else
|
|
|
|
TEST(write_encoding_huge)
|
|
|
|
{
|
|
|
|
const unsigned int N = 16000;
|
|
|
|
|
|
|
|
// make a large utf8 name consisting of 3-byte chars (3 does not divide internal buffer size, so will need split correction)
|
|
|
|
std::string s_utf8 = "<";
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < N; ++i) s_utf8 += "\xE2\x82\xAC";
|
|
|
|
|
|
|
|
s_utf8 += "/>";
|
|
|
|
|
|
|
|
xml_document doc;
|
|
|
|
CHECK(doc.load_buffer(&s_utf8[0], s_utf8.length(), parse_default, encoding_utf8));
|
|
|
|
|
|
|
|
std::string s_utf16 = std::string("\x00<", 2);
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < N; ++j) s_utf16 += "\x20\xAC";
|
|
|
|
|
|
|
|
s_utf16 += std::string("\x00 \x00/\x00>\x00\n", 8);
|
|
|
|
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf16_be, s_utf16.c_str(), s_utf16.length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(write_encoding_huge_invalid)
|
|
|
|
{
|
|
|
|
const unsigned int N = 16000;
|
|
|
|
|
|
|
|
// make a large utf8 name consisting of non-leading chars
|
2010-05-30 20:17:05 +00:00
|
|
|
std::string s_utf8;
|
2010-05-06 20:28:36 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < N; ++i) s_utf8 += "\x82";
|
|
|
|
|
|
|
|
xml_document doc;
|
2010-05-30 20:17:05 +00:00
|
|
|
doc.append_child().set_name(s_utf8.c_str());
|
2010-05-06 20:28:36 +00:00
|
|
|
|
|
|
|
std::string s_utf16 = std::string("\x00<\x00 \x00/\x00>\x00\n", 10);
|
|
|
|
|
|
|
|
CHECK(test_write_narrow(doc, format_default, encoding_utf16_be, s_utf16.c_str(), s_utf16.length()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(write_unicode_escape)
|
|
|
|
{
|
|
|
|
char s_utf8[] = "<\xE2\x82\xAC \xC2\xA2='\"\xF0\xA4\xAD\xA2
\"'>&\x14\xF0\xA4\xAD\xA2<</\xE2\x82\xAC>";
|
|
|
|
|
|
|
|
xml_document doc;
|
|
|
|
CHECK(doc.load_buffer(s_utf8, sizeof(s_utf8), parse_default, encoding_utf8));
|
|
|
|
|
|
|
|
CHECK(write_narrow(doc, format_default, encoding_utf8) == "<\xE2\x82\xAC \xC2\xA2=\""\xF0\xA4\xAD\xA2 "\">&\xF0\xA4\xAD\xA2<</\xE2\x82\xAC>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PUGIXML_WCHAR_MODE
|
|
|
|
static bool test_write_unicode_invalid(const wchar_t* name, const char* expected)
|
|
|
|
{
|
|
|
|
xml_document doc;
|
|
|
|
doc.append_child(node_pcdata).set_value(name);
|
|
|
|
|
|
|
|
return write_narrow(doc, format_raw, encoding_utf8) == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(write_unicode_invalid_utf16)
|
|
|
|
{
|
|
|
|
size_t wcharsize = sizeof(wchar_t);
|
|
|
|
|
|
|
|
if (wcharsize == 2)
|
|
|
|
{
|
|
|
|
// check non-terminated degenerate handling
|
|
|
|
#ifdef U_LITERALS
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\uda1d", "a"));
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\uda1d_", "a_"));
|
|
|
|
#else
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\xda1d", "a"));
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\xda1d_", "a_"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// check incorrect leading code
|
|
|
|
#ifdef U_LITERALS
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\ude24", "a"));
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\ude24_", "a_"));
|
|
|
|
#else
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\xde24", "a"));
|
|
|
|
CHECK(test_write_unicode_invalid(L"a\xde24_", "a_"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static bool test_write_unicode_invalid(const char* name, const wchar_t* expected)
|
|
|
|
{
|
|
|
|
xml_document doc;
|
|
|
|
doc.append_child(node_pcdata).set_value(name);
|
|
|
|
|
|
|
|
return write_wide(doc, format_raw, encoding_wchar) == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(write_unicode_invalid_utf8)
|
|
|
|
{
|
|
|
|
// invalid 1-byte input
|
|
|
|
CHECK(test_write_unicode_invalid("a\xb0", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xb0_", L"a_"));
|
|
|
|
|
|
|
|
// invalid 2-byte input
|
|
|
|
CHECK(test_write_unicode_invalid("a\xc0", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xd0", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xc0_", L"a_"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xd0_", L"a_"));
|
|
|
|
|
|
|
|
// invalid 3-byte input
|
|
|
|
CHECK(test_write_unicode_invalid("a\xe2\x80", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xe2", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xe2\x80_", L"a_"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xe2_", L"a_"));
|
|
|
|
|
|
|
|
// invalid 4-byte input
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf2\x97\x98", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf2\x97", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf2", L"a"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf2\x97\x98_", L"a_"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf2\x97_", L"a_"));
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf2_", L"a_"));
|
|
|
|
|
|
|
|
// invalid 5-byte input
|
|
|
|
CHECK(test_write_unicode_invalid("a\xf8_", L"a_"));
|
|
|
|
}
|
2010-05-30 20:17:05 +00:00
|
|
|
#endif
|
2010-05-06 20:28:36 +00:00
|
|
|
|
2010-05-21 05:50:53 +00:00
|
|
|
TEST(write_no_name_element)
|
|
|
|
{
|
|
|
|
xml_document doc;
|
|
|
|
xml_node root = doc.append_child();
|
|
|
|
root.append_child();
|
2010-05-30 20:17:05 +00:00
|
|
|
root.append_child().append_child(node_pcdata).set_value(STR("text"));
|
2010-05-21 05:50:53 +00:00
|
|
|
|
2010-05-30 20:17:05 +00:00
|
|
|
CHECK_NODE(doc, STR("<:anonymous><:anonymous /><:anonymous>text</:anonymous></:anonymous>"));
|
|
|
|
CHECK_NODE_EX(doc, STR("<:anonymous>\n\t<:anonymous />\n\t<:anonymous>text</:anonymous>\n</:anonymous>\n"), STR("\t"), format_default);
|
2010-05-21 05:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(write_no_name_pi)
|
|
|
|
{
|
|
|
|
xml_document doc;
|
|
|
|
doc.append_child(node_pi);
|
|
|
|
|
2010-05-30 20:17:05 +00:00
|
|
|
CHECK_NODE(doc, STR("<?:anonymous?>"));
|
2010-05-21 05:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(write_no_name_attribute)
|
|
|
|
{
|
|
|
|
xml_document doc;
|
2010-05-30 20:17:05 +00:00
|
|
|
doc.append_child().set_name(STR("root"));
|
|
|
|
doc.child(STR("root")).append_attribute(STR(""));
|
2010-05-21 05:50:53 +00:00
|
|
|
|
2010-05-30 20:17:05 +00:00
|
|
|
CHECK_NODE(doc, STR("<root :anonymous=\"\" />"));
|
2010-05-21 05:50:53 +00:00
|
|
|
}
|