1 /* Copyright (C) 2009 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "lib/self_test.h" 19 20 #include "ps/XML/XMLWriter.h" 21 22 class TestXmlWriter : public CxxTest::TestSuite 23 { 24 public: test1()25 void test1() 26 { 27 XML_Start(); 28 29 { 30 XML_Element("Root"); 31 { 32 XML_Comment("Comment test."); 33 XML_Comment("Comment test again."); 34 { 35 XML_Element("a"); 36 XML_Attribute("one", 1); 37 XML_Attribute("two", "TWO"); 38 XML_Text("b"); 39 XML_Text(" (etc)"); 40 } 41 { 42 XML_Element("c"); 43 XML_Text("d"); 44 } 45 XML_Setting("c2", "d2"); 46 { 47 XML_Element("e"); 48 { 49 { 50 XML_Element("f"); 51 XML_Text("g"); 52 } 53 { 54 XML_Element("h"); 55 } 56 { 57 XML_Element("i"); 58 XML_Attribute("j", 1.23); 59 { 60 XML_Element("k"); 61 XML_Attribute("l", 2.34); 62 XML_Text("m"); 63 } 64 } 65 } 66 } 67 } 68 } 69 70 CStr output = XML_GetOutput(); 71 TS_ASSERT_STR_EQUALS(output, 72 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 73 "\n" 74 "<Root>\n" 75 "\t<!-- Comment test. -->\n" 76 "\t<!-- Comment test again. -->\n" 77 "\t<a one=\"1\" two=\"TWO\">b (etc)</a>\n" 78 "\t<c>d</c>\n" 79 "\t<c2>d2</c2>\n" 80 "\t<e>\n" 81 "\t\t<f>g</f>\n" 82 "\t\t<h/>\n" 83 "\t\t<i j=\"1.23\">\n" 84 "\t\t\t<k l=\"2.34\">m</k>\n" 85 "\t\t</i>\n" 86 "\t</e>\n" 87 "</Root>" 88 ); 89 } 90 test_basic()91 void test_basic() 92 { 93 XML_Start(); 94 95 { 96 XML_Element("Test"); 97 { 98 XML_Element("example"); 99 { 100 XML_Element("content"); 101 XML_Text("text"); 102 } 103 } 104 } 105 106 CStr output = XML_GetOutput(); 107 TS_ASSERT_STR_EQUALS(output, 108 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 109 "\n" 110 "<Test>\n" 111 "\t<example>\n" 112 "\t\t<content>text</content>\n" 113 "\t</example>\n" 114 "</Test>" 115 ); 116 } 117 test_nonpretty()118 void test_nonpretty() 119 { 120 XML_Start(); 121 XML_SetPrettyPrint(false); 122 123 { 124 XML_Element("Test"); 125 { 126 XML_Element("example"); 127 { 128 XML_Element("content"); 129 XML_Text("text"); 130 } 131 } 132 } 133 134 CStr output = XML_GetOutput(); 135 TS_ASSERT_STR_EQUALS(output, 136 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 137 "<Test><example><content>text</content></example></Test>" 138 ); 139 } 140 test_text()141 void test_text() 142 { 143 XML_Start(); 144 145 { 146 XML_Element("Test"); 147 XML_Text("a"); 148 XML_Text("b"); 149 } 150 151 CStr output = XML_GetOutput(); 152 TS_ASSERT_STR_EQUALS(output, 153 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 154 "\n" 155 "<Test>ab</Test>" 156 ); 157 } 158 159 test_utf8()160 void test_utf8() 161 { 162 XML_Start(); 163 164 { 165 XML_Element("Test"); 166 { 167 const wchar_t text[] = { 0x0251, 0 }; 168 XML_Text(text); 169 } 170 } 171 172 CStr output = XML_GetOutput(); 173 TS_ASSERT_STR_EQUALS(output, 174 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" 175 "<Test>\xC9\x91</Test>" 176 ); 177 } 178 test_attr_escape()179 void test_attr_escape() 180 { 181 XML_Start(); 182 183 { 184 XML_Element("Test"); 185 XML_Attribute("example", "abc > ]]> < & \"\" "); 186 } 187 188 CStr output = XML_GetOutput(); 189 TS_ASSERT_STR_EQUALS(output, 190 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" 191 "<Test example=\"abc > ]]> < & "" \"/>" 192 ); 193 } 194 test_chardata_escape()195 void test_chardata_escape() 196 { 197 XML_Start(); 198 199 { 200 XML_Element("Test"); 201 XML_Text("abc > ]]> < & \"\" "); 202 } 203 204 CStr output = XML_GetOutput(); 205 TS_ASSERT_STR_EQUALS(output, 206 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" 207 "<Test>abc > ]]> < & \"\" </Test>" 208 ); 209 } 210 test_cdata_escape()211 void test_cdata_escape() 212 { 213 XML_Start(); 214 215 { 216 XML_Element("Test"); 217 XML_CDATA("abc > ]]> < & \"\" "); 218 } 219 220 CStr output = XML_GetOutput(); 221 TS_ASSERT_STR_EQUALS(output, 222 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" 223 "<Test><![CDATA[abc > ]]>]]><![CDATA[ < & \"\" ]]></Test>" 224 ); 225 } 226 test_comment_escape()227 void test_comment_escape() 228 { 229 XML_Start(); 230 231 { 232 XML_Element("Test"); 233 XML_Comment("test - -- --- ---- test"); 234 } 235 236 CStr output = XML_GetOutput(); 237 TS_ASSERT_STR_EQUALS(output, 238 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" 239 "<Test>\n" 240 "\t<!-- test - \xE2\x80\x90\xE2\x80\x90 \xE2\x80\x90\xE2\x80\x90- \xE2\x80\x90\xE2\x80\x90\xE2\x80\x90\xE2\x80\x90 test -->\n" 241 "</Test>" 242 ); 243 } 244 }; 245