Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 1 | #if defined( _MSC_VER )
|
Serhat Eser Erdem | ca5d684 | 2014-04-17 14:06:15 +0200 | [diff] [blame] | 2 | #if !defined( _CRT_SECURE_NO_WARNINGS )
|
| 3 | #define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
|
| 4 | #endif
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 5 | #endif
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 6 |
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 7 | #include "tinyxml2.h"
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 8 | #include <cstdlib>
|
| 9 | #include <cstring>
|
| 10 | #include <ctime>
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 11 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 12 | #if defined( _MSC_VER )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 13 | #include <crtdbg.h>
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 14 | #define WIN32_LEAN_AND_MEAN
|
| 15 | #include <windows.h>
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 16 | _CrtMemState startMemState;
|
Lee Thomason | 53858b4 | 2017-06-01 19:09:16 -0700 | [diff] [blame^] | 17 | _CrtMemState endMemState;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 18 | #endif
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 19 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 20 | using namespace tinyxml2;
|
Anton Indrawan | 8a0006c | 2014-11-20 18:27:07 +0100 | [diff] [blame] | 21 | using namespace std;
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 22 | int gPass = 0;
|
| 23 | int gFail = 0;
|
| 24 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 25 |
|
Lee Thomason (grinliz) | d0a38c3 | 2013-04-29 09:15:37 -0700 | [diff] [blame] | 26 | bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 27 | {
|
Sarat Addepalli | 13b2d73 | 2015-05-19 12:44:57 +0530 | [diff] [blame] | 28 | bool pass;
|
| 29 | if ( !expected && !found )
|
| 30 | pass = true;
|
| 31 | else if ( !expected || !found )
|
| 32 | pass = false;
|
Sarat Addepalli | d608c56 | 2015-05-20 10:19:00 +0530 | [diff] [blame] | 33 | else
|
| 34 | pass = !strcmp( expected, found );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 35 | if ( pass )
|
| 36 | printf ("[pass]");
|
| 37 | else
|
| 38 | printf ("[fail]");
|
| 39 |
|
Lee Thomason (grinliz) | d0a38c3 | 2013-04-29 09:15:37 -0700 | [diff] [blame] | 40 | if ( !echo ) {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 41 | printf (" %s\n", testString);
|
Lee Thomason (grinliz) | d0a38c3 | 2013-04-29 09:15:37 -0700 | [diff] [blame] | 42 | }
|
| 43 | else {
|
| 44 | if ( extraNL ) {
|
| 45 | printf( " %s\n", testString );
|
| 46 | printf( "%s\n", expected );
|
| 47 | printf( "%s\n", found );
|
| 48 | }
|
| 49 | else {
|
| 50 | printf (" %s [%s][%s]\n", testString, expected, found);
|
| 51 | }
|
| 52 | }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 53 |
|
| 54 | if ( pass )
|
| 55 | ++gPass;
|
| 56 | else
|
| 57 | ++gFail;
|
| 58 | return pass;
|
| 59 | }
|
| 60 |
|
kezenator | 5a70071 | 2016-11-26 13:54:42 +1000 | [diff] [blame] | 61 | bool XMLTest(const char* testString, XMLError expected, XMLError found, bool echo = true, bool extraNL = false)
|
| 62 | {
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 63 | return XMLTest(testString, XMLDocument::ErrorIDToName(expected), XMLDocument::ErrorIDToName(found), echo, extraNL);
|
kezenator | 5a70071 | 2016-11-26 13:54:42 +1000 | [diff] [blame] | 64 | }
|
| 65 |
|
| 66 | bool XMLTest(const char* testString, bool expected, bool found, bool echo = true, bool extraNL = false)
|
| 67 | {
|
| 68 | return XMLTest(testString, expected ? "true" : "false", found ? "true" : "false", echo, extraNL);
|
| 69 | }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 70 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 71 | template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 72 | {
|
| 73 | bool pass = ( expected == found );
|
| 74 | if ( pass )
|
| 75 | printf ("[pass]");
|
| 76 | else
|
| 77 | printf ("[fail]");
|
| 78 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 79 | if ( !echo )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 80 | printf (" %s\n", testString);
|
| 81 | else
|
Lee Thomason | c831279 | 2012-07-16 12:44:41 -0700 | [diff] [blame] | 82 | printf (" %s [%d][%d]\n", testString, static_cast<int>(expected), static_cast<int>(found) );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 83 |
|
| 84 | if ( pass )
|
| 85 | ++gPass;
|
| 86 | else
|
| 87 | ++gFail;
|
| 88 | return pass;
|
| 89 | }
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 90 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 91 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 92 | void NullLineEndings( char* p )
|
| 93 | {
|
| 94 | while( p && *p ) {
|
| 95 | if ( *p == '\n' || *p == '\r' ) {
|
| 96 | *p = 0;
|
| 97 | return;
|
| 98 | }
|
| 99 | ++p;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 104 | int example_1()
|
| 105 | {
|
| 106 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 107 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 108 |
|
| 109 | return doc.ErrorID();
|
| 110 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 111 | /** @page Example-1 Load an XML File
|
| 112 | * @dontinclude ./xmltest.cpp
|
| 113 | * Basic XML file loading.
|
| 114 | * The basic syntax to load an XML file from
|
| 115 | * disk and check for an error. (ErrorID()
|
| 116 | * will return 0 for no error.)
|
| 117 | * @skip example_1()
|
| 118 | * @until }
|
| 119 | */
|
| 120 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 121 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 122 | int example_2()
|
| 123 | {
|
| 124 | static const char* xml = "<element/>";
|
| 125 | XMLDocument doc;
|
| 126 | doc.Parse( xml );
|
| 127 |
|
| 128 | return doc.ErrorID();
|
| 129 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 130 | /** @page Example-2 Parse an XML from char buffer
|
| 131 | * @dontinclude ./xmltest.cpp
|
| 132 | * Basic XML string parsing.
|
| 133 | * The basic syntax to parse an XML for
|
| 134 | * a char* and check for an error. (ErrorID()
|
| 135 | * will return 0 for no error.)
|
| 136 | * @skip example_2()
|
| 137 | * @until }
|
| 138 | */
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 139 |
|
| 140 |
|
| 141 | int example_3()
|
| 142 | {
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 143 | static const char* xml =
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 144 | "<?xml version=\"1.0\"?>"
|
| 145 | "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
|
| 146 | "<PLAY>"
|
| 147 | "<TITLE>A Midsummer Night's Dream</TITLE>"
|
| 148 | "</PLAY>";
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 149 |
|
| 150 | XMLDocument doc;
|
| 151 | doc.Parse( xml );
|
| 152 |
|
| 153 | XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
|
| 154 | const char* title = titleElement->GetText();
|
| 155 | printf( "Name of play (1): %s\n", title );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 156 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 157 | XMLText* textNode = titleElement->FirstChild()->ToText();
|
| 158 | title = textNode->Value();
|
| 159 | printf( "Name of play (2): %s\n", title );
|
| 160 |
|
| 161 | return doc.ErrorID();
|
| 162 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 163 | /** @page Example-3 Get information out of XML
|
| 164 | @dontinclude ./xmltest.cpp
|
| 165 | In this example, we navigate a simple XML
|
| 166 | file, and read some interesting text. Note
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 167 | that this example doesn't use error
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 168 | checking; working code should check for null
|
| 169 | pointers when walking an XML tree, or use
|
| 170 | XMLHandle.
|
| 171 |
|
| 172 | (The XML is an excerpt from "dream.xml").
|
| 173 |
|
| 174 | @skip example_3()
|
| 175 | @until </PLAY>";
|
| 176 |
|
| 177 | The structure of the XML file is:
|
| 178 |
|
| 179 | <ul>
|
| 180 | <li>(declaration)</li>
|
| 181 | <li>(dtd stuff)</li>
|
| 182 | <li>Element "PLAY"</li>
|
| 183 | <ul>
|
| 184 | <li>Element "TITLE"</li>
|
| 185 | <ul>
|
| 186 | <li>Text "A Midsummer Night's Dream"</li>
|
| 187 | </ul>
|
| 188 | </ul>
|
| 189 | </ul>
|
| 190 |
|
| 191 | For this example, we want to print out the
|
| 192 | title of the play. The text of the title (what
|
| 193 | we want) is child of the "TITLE" element which
|
| 194 | is a child of the "PLAY" element.
|
| 195 |
|
| 196 | We want to skip the declaration and dtd, so the
|
| 197 | method FirstChildElement() is a good choice. The
|
| 198 | FirstChildElement() of the Document is the "PLAY"
|
| 199 | Element, the FirstChildElement() of the "PLAY" Element
|
| 200 | is the "TITLE" Element.
|
| 201 |
|
| 202 | @until ( "TITLE" );
|
| 203 |
|
| 204 | We can then use the convenience function GetText()
|
| 205 | to get the title of the play.
|
| 206 |
|
| 207 | @until title );
|
| 208 |
|
| 209 | Text is just another Node in the XML DOM. And in
|
| 210 | fact you should be a little cautious with it, as
|
| 211 | text nodes can contain elements.
|
| 212 |
|
| 213 | @verbatim
|
| 214 | Consider: A Midsummer Night's <b>Dream</b>
|
| 215 | @endverbatim
|
| 216 |
|
| 217 | It is more correct to actually query the Text Node
|
| 218 | if in doubt:
|
| 219 |
|
| 220 | @until title );
|
| 221 |
|
| 222 | Noting that here we use FirstChild() since we are
|
| 223 | looking for XMLText, not an element, and ToText()
|
| 224 | is a cast from a Node to a XMLText.
|
| 225 | */
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 226 |
|
| 227 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 228 | bool example_4()
|
| 229 | {
|
| 230 | static const char* xml =
|
| 231 | "<information>"
|
| 232 | " <attributeApproach v='2' />"
|
| 233 | " <textApproach>"
|
| 234 | " <v>2</v>"
|
| 235 | " </textApproach>"
|
| 236 | "</information>";
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 237 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 238 | XMLDocument doc;
|
| 239 | doc.Parse( xml );
|
| 240 |
|
| 241 | int v0 = 0;
|
| 242 | int v1 = 0;
|
| 243 |
|
| 244 | XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
|
| 245 | attributeApproachElement->QueryIntAttribute( "v", &v0 );
|
| 246 |
|
| 247 | XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
|
| 248 | textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
|
| 249 |
|
| 250 | printf( "Both values are the same: %d and %d\n", v0, v1 );
|
| 251 |
|
| 252 | return !doc.Error() && ( v0 == v1 );
|
| 253 | }
|
Martinsh Shaiters | c9c8b77 | 2013-01-16 02:08:19 +0200 | [diff] [blame] | 254 | /** @page Example-4 Read attributes and text information.
|
| 255 | @dontinclude ./xmltest.cpp
|
| 256 |
|
| 257 | There are fundamentally 2 ways of writing a key-value
|
| 258 | pair into an XML file. (Something that's always annoyed
|
| 259 | me about XML.) Either by using attributes, or by writing
|
| 260 | the key name into an element and the value into
|
| 261 | the text node wrapped by the element. Both approaches
|
| 262 | are illustrated in this example, which shows two ways
|
| 263 | to encode the value "2" into the key "v":
|
| 264 |
|
| 265 | @skip example_4()
|
| 266 | @until "</information>";
|
| 267 |
|
| 268 | TinyXML-2 has accessors for both approaches.
|
| 269 |
|
| 270 | When using an attribute, you navigate to the XMLElement
|
| 271 | with that attribute and use the QueryIntAttribute()
|
| 272 | group of methods. (Also QueryFloatAttribute(), etc.)
|
| 273 |
|
| 274 | @skip XMLElement* attributeApproachElement
|
| 275 | @until &v0 );
|
| 276 |
|
| 277 | When using the text approach, you need to navigate
|
| 278 | down one more step to the XMLElement that contains
|
| 279 | the text. Note the extra FirstChildElement( "v" )
|
| 280 | in the code below. The value of the text can then
|
| 281 | be safely queried with the QueryIntText() group
|
| 282 | of methods. (Also QueryFloatText(), etc.)
|
| 283 |
|
| 284 | @skip XMLElement* textApproachElement
|
| 285 | @until &v1 );
|
| 286 | */
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 287 |
|
| 288 |
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 289 | int main( int argc, const char ** argv )
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 290 | {
|
Lee Thomason (grinliz) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 291 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 292 | _CrtMemCheckpoint( &startMemState );
|
Dmitry-Me | 9991659 | 2014-10-23 11:37:03 +0400 | [diff] [blame] | 293 | // Enable MS Visual C++ debug heap memory leaks dump on exit
|
| 294 | _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 295 | #endif
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 296 |
|
Dmitry-Me | 4bcbf14 | 2014-12-25 19:05:18 +0300 | [diff] [blame] | 297 | {
|
| 298 | TIXMLASSERT( true );
|
| 299 | }
|
| 300 |
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 301 | if ( argc > 1 ) {
|
| 302 | XMLDocument* doc = new XMLDocument();
|
| 303 | clock_t startTime = clock();
|
| 304 | doc->LoadFile( argv[1] );
|
Anton Indrawan | 8a0006c | 2014-11-20 18:27:07 +0100 | [diff] [blame] | 305 | clock_t loadTime = clock();
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 306 | int errorID = doc->ErrorID();
|
| 307 | delete doc; doc = 0;
|
Anton Indrawan | 8a0006c | 2014-11-20 18:27:07 +0100 | [diff] [blame] | 308 | clock_t deleteTime = clock();
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 309 |
|
| 310 | printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
|
| 311 | if ( !errorID ) {
|
Lee Thomason (grinliz) | d6bd736 | 2013-05-11 20:23:13 -0700 | [diff] [blame] | 312 | printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
|
| 313 | printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
|
| 314 | printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
|
Lee Thomason | 178e4cc | 2013-01-25 16:19:05 -0800 | [diff] [blame] | 315 | }
|
| 316 | exit(0);
|
| 317 | }
|
| 318 |
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 319 | FILE* fp = fopen( "resources/dream.xml", "r" );
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 320 | if ( !fp ) {
|
| 321 | printf( "Error opening test file 'dream.xml'.\n"
|
| 322 | "Is your working directory the same as where \n"
|
| 323 | "the xmltest.cpp and dream.xml file are?\n\n"
|
| 324 | #if defined( _MSC_VER )
|
| 325 | "In windows Visual Studio you may need to set\n"
|
| 326 | "Properties->Debugging->Working Directory to '..'\n"
|
| 327 | #endif
|
| 328 | );
|
| 329 | exit( 1 );
|
| 330 | }
|
| 331 | fclose( fp );
|
| 332 |
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 333 | XMLTest( "Example-1", 0, example_1() );
|
| 334 | XMLTest( "Example-2", 0, example_2() );
|
| 335 | XMLTest( "Example-3", 0, example_3() );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 336 | XMLTest( "Example-4", true, example_4() );
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 337 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 338 | /* ------ Example 2: Lookup information. ---- */
|
Lee Thomason | 87e475a | 2012-03-20 11:55:29 -0700 | [diff] [blame] | 339 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 340 | {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 341 | static const char* test[] = { "<element />",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 342 | "<element></element>",
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 343 | "<element><subelement/></element>",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 344 | "<element><subelement></subelement></element>",
|
| 345 | "<element><subelement><subsub/></subelement></element>",
|
| 346 | "<!--comment beside elements--><element><subelement></subelement></element>",
|
| 347 | "<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
|
| 348 | "<element attrib1='foo' attrib2=\"bar\" ></element>",
|
| 349 | "<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 350 | "<element>Text inside element.</element>",
|
| 351 | "<element><b></b></element>",
|
| 352 | "<element>Text inside and <b>bolded</b> in the element.</element>",
|
| 353 | "<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 354 | "<element>This & That.</element>",
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 355 | "<element attrib='This<That' />",
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 356 | 0
|
| 357 | };
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 358 | for( int i=0; test[i]; ++i ) {
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 359 | XMLDocument doc;
|
Lee Thomason | 6ee99fc | 2012-01-21 18:45:16 -0800 | [diff] [blame] | 360 | doc.Parse( test[i] );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 361 | doc.Print();
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 362 | printf( "----------------------------------------------\n" );
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 363 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 364 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 365 | #if 1
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 366 | {
|
| 367 | static const char* test = "<!--hello world\n"
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 368 | " line 2\r"
|
| 369 | " line 3\r\n"
|
| 370 | " line 4\n\r"
|
| 371 | " line 5\r-->";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 372 |
|
| 373 | XMLDocument doc;
|
| 374 | doc.Parse( test );
|
| 375 | doc.Print();
|
| 376 | }
|
| 377 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 378 | {
|
| 379 | static const char* test = "<element>Text before.</element>";
|
| 380 | XMLDocument doc;
|
| 381 | doc.Parse( test );
|
| 382 | XMLElement* root = doc.FirstChildElement();
|
| 383 | XMLElement* newElement = doc.NewElement( "Subelement" );
|
| 384 | root->InsertEndChild( newElement );
|
| 385 | doc.Print();
|
| 386 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 387 | {
|
| 388 | XMLDocument* doc = new XMLDocument();
|
| 389 | static const char* test = "<element><sub/></element>";
|
| 390 | doc->Parse( test );
|
| 391 | delete doc;
|
| 392 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 393 | {
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 394 | // Test: Programmatic DOM
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 395 | // Build:
|
| 396 | // <element>
|
| 397 | // <!--comment-->
|
| 398 | // <sub attrib="1" />
|
| 399 | // <sub attrib="2" />
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 400 | // <sub attrib="3" >& Text!</sub>
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 401 | // <element>
|
| 402 |
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 403 | XMLDocument* doc = new XMLDocument();
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 404 | XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
|
| 405 |
|
| 406 | XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
|
| 407 | for( int i=0; i<3; ++i ) {
|
| 408 | sub[i]->SetAttribute( "attrib", i );
|
| 409 | }
|
| 410 | element->InsertEndChild( sub[2] );
|
| 411 | XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
|
Lee Thomason | af9bce1 | 2016-07-17 22:35:52 -0700 | [diff] [blame] | 412 | comment->SetUserData((void*)2);
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 413 | element->InsertAfterChild( comment, sub[0] );
|
| 414 | element->InsertAfterChild( sub[0], sub[1] );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 415 | sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 416 | doc->Print();
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 417 | XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
|
| 418 | XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
|
| 419 | XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 420 | XMLTest( "Programmatic DOM", "& Text!",
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 421 | doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
|
Lee Thomason | c944546 | 2016-07-17 22:53:48 -0700 | [diff] [blame] | 422 | XMLTest("User data", (void*)2 == comment->GetUserData(), true, false);
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 423 |
|
| 424 | // And now deletion:
|
| 425 | element->DeleteChild( sub[2] );
|
| 426 | doc->DeleteNode( comment );
|
| 427 |
|
| 428 | element->FirstChildElement()->SetAttribute( "attrib", true );
|
| 429 | element->LastChildElement()->DeleteAttribute( "attrib" );
|
| 430 |
|
| 431 | XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 432 | int value1 = 10;
|
| 433 | int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute( "attrib", 10 );
|
| 434 | int result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value1 );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 435 | XMLTest( "Programmatic DOM", result, (int)XML_NO_ATTRIBUTE );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 436 | XMLTest( "Programmatic DOM", value1, 10 );
|
| 437 | XMLTest( "Programmatic DOM", value2, 10 );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 438 |
|
| 439 | doc->Print();
|
| 440 |
|
Lee Thomason | 7b1b86a | 2012-06-04 17:01:38 -0700 | [diff] [blame] | 441 | {
|
| 442 | XMLPrinter streamer;
|
| 443 | doc->Print( &streamer );
|
| 444 | printf( "%s", streamer.CStr() );
|
| 445 | }
|
| 446 | {
|
| 447 | XMLPrinter streamer( 0, true );
|
| 448 | doc->Print( &streamer );
|
Doruk Turak | 1f212f3 | 2016-08-28 20:54:17 +0200 | [diff] [blame] | 449 | XMLTest( "Compact mode", "<element><sub attrib=\"true\"/><sub/></element>", streamer.CStr(), false );
|
Lee Thomason | 7b1b86a | 2012-06-04 17:01:38 -0700 | [diff] [blame] | 450 | }
|
Lee Thomason (grinliz) | 6b8b012 | 2012-09-08 21:21:00 -0700 | [diff] [blame] | 451 | doc->SaveFile( "./resources/out/pretty.xml" );
|
| 452 | doc->SaveFile( "./resources/out/compact.xml", true );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 453 | delete doc;
|
| 454 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 455 | {
|
| 456 | // Test: Dream
|
| 457 | // XML1 : 1,187,569 bytes in 31,209 allocations
|
| 458 | // XML2 : 469,073 bytes in 323 allocations
|
| 459 | //int newStart = gNew;
|
| 460 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 461 | doc.LoadFile( "resources/dream.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 462 |
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 463 | doc.SaveFile( "resources/out/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 464 | doc.PrintError();
|
| 465 |
|
| 466 | XMLTest( "Dream", "xml version=\"1.0\"",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 467 | doc.FirstChild()->ToDeclaration()->Value() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 468 | XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 469 | XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 470 | doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 471 | XMLTest( "Dream", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 472 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 473 | XMLTest( "Dream", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 474 | doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 475 |
|
| 476 | XMLDocument doc2;
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 477 | doc2.LoadFile( "resources/out/dreamout.xml" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 478 | XMLTest( "Dream-out", "xml version=\"1.0\"",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 479 | doc2.FirstChild()->ToDeclaration()->Value() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 480 | XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() ? true : false );
|
| 481 | XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
|
| 482 | doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
|
| 483 | XMLTest( "Dream-out", "And Robin shall restore amends.",
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 484 | doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 485 |
|
| 486 | //gNewTotal = gNew - newStart;
|
| 487 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 488 |
|
| 489 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 490 | {
|
| 491 | const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
|
| 492 | "<passages count=\"006\" formatversion=\"20020620\">\n"
|
| 493 | " <wrong error>\n"
|
| 494 | "</passages>";
|
| 495 |
|
| 496 | XMLDocument doc;
|
| 497 | doc.Parse( error );
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 498 | XMLTest( "Bad XML", doc.ErrorID(), XML_ERROR_PARSING_ATTRIBUTE );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 499 | }
|
| 500 |
|
| 501 | {
|
| 502 | const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
|
| 503 |
|
| 504 | XMLDocument doc;
|
| 505 | doc.Parse( str );
|
| 506 |
|
| 507 | XMLElement* ele = doc.FirstChildElement();
|
| 508 |
|
| 509 | int iVal, result;
|
| 510 | double dVal;
|
| 511 |
|
| 512 | result = ele->QueryDoubleAttribute( "attr0", &dVal );
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 513 | XMLTest( "Query attribute: int as double", result, (int)XML_SUCCESS);
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 514 | XMLTest( "Query attribute: int as double", (int)dVal, 1 );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 515 | XMLTest( "Query attribute: int as double", (int)ele->DoubleAttribute("attr0"), 1);
|
| 516 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 517 | result = ele->QueryDoubleAttribute( "attr1", &dVal );
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 518 | XMLTest( "Query attribute: double as double", result, (int)XML_SUCCESS);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 519 | XMLTest( "Query attribute: double as double", dVal, 2.0 );
|
| 520 | XMLTest( "Query attribute: double as double", ele->DoubleAttribute("attr1"), 2.0 );
|
| 521 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 522 | result = ele->QueryIntAttribute( "attr1", &iVal );
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 523 | XMLTest( "Query attribute: double as int", result, (int)XML_SUCCESS);
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 524 | XMLTest( "Query attribute: double as int", iVal, 2 );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 525 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 526 | result = ele->QueryIntAttribute( "attr2", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 527 | XMLTest( "Query attribute: not a number", result, (int)XML_WRONG_ATTRIBUTE_TYPE );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 528 | XMLTest( "Query attribute: not a number", ele->DoubleAttribute("attr2", 4.0), 4.0 );
|
| 529 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 530 | result = ele->QueryIntAttribute( "bar", &iVal );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 531 | XMLTest( "Query attribute: does not exist", result, (int)XML_NO_ATTRIBUTE );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 532 | XMLTest( "Query attribute: does not exist", ele->BoolAttribute("bar", true), true );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 533 | }
|
| 534 |
|
| 535 | {
|
| 536 | const char* str = "<doc/>";
|
| 537 |
|
| 538 | XMLDocument doc;
|
| 539 | doc.Parse( str );
|
| 540 |
|
| 541 | XMLElement* ele = doc.FirstChildElement();
|
| 542 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 543 | int iVal, iVal2;
|
| 544 | double dVal, dVal2;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 545 |
|
| 546 | ele->SetAttribute( "str", "strValue" );
|
| 547 | ele->SetAttribute( "int", 1 );
|
| 548 | ele->SetAttribute( "double", -1.0 );
|
| 549 |
|
| 550 | const char* cStr = ele->Attribute( "str" );
|
| 551 | ele->QueryIntAttribute( "int", &iVal );
|
| 552 | ele->QueryDoubleAttribute( "double", &dVal );
|
| 553 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 554 | ele->QueryAttribute( "int", &iVal2 );
|
| 555 | ele->QueryAttribute( "double", &dVal2 );
|
| 556 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 557 | XMLTest( "Attribute match test", ele->Attribute( "str", "strValue" ), "strValue" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 558 | XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
|
| 559 | XMLTest( "Attribute round trip. int.", 1, iVal );
|
| 560 | XMLTest( "Attribute round trip. double.", -1, (int)dVal );
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 561 | XMLTest( "Alternate query", true, iVal == iVal2 );
|
| 562 | XMLTest( "Alternate query", true, dVal == dVal2 );
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 563 | XMLTest( "Alternate query", true, iVal == ele->IntAttribute("int") );
|
| 564 | XMLTest( "Alternate query", true, dVal == ele->DoubleAttribute("double") );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 565 | }
|
| 566 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 567 | {
|
| 568 | XMLDocument doc;
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 569 | doc.LoadFile( "resources/utf8test.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 570 |
|
| 571 | // Get the attribute "value" from the "Russian" element and check it.
|
| 572 | XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 573 | const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 574 | 0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
|
| 575 |
|
| 576 | XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
|
| 577 |
|
| 578 | const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
|
| 579 | 0xd1U, 0x81U, 0xd1U, 0x81U,
|
| 580 | 0xd0U, 0xbaU, 0xd0U, 0xb8U,
|
| 581 | 0xd0U, 0xb9U, 0 };
|
| 582 | const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
|
| 583 |
|
| 584 | XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
|
| 585 | XMLTest( "UTF-8: Browsing russian element name.",
|
| 586 | russianText,
|
| 587 | text->Value() );
|
| 588 |
|
| 589 | // Now try for a round trip.
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 590 | doc.SaveFile( "resources/out/utf8testout.xml" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 591 |
|
| 592 | // Check the round trip.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 593 | int okay = 0;
|
| 594 |
|
Thomas Roß | a6dd8c6 | 2012-07-26 20:42:18 +0200 | [diff] [blame] | 595 | FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
|
Bruno Dias | a2d4e6e | 2012-05-07 04:58:11 -0300 | [diff] [blame] | 596 | FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 597 |
|
| 598 | if ( saved && verify )
|
| 599 | {
|
| 600 | okay = 1;
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 601 | char verifyBuf[256];
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 602 | while ( fgets( verifyBuf, 256, verify ) )
|
| 603 | {
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 604 | char savedBuf[256];
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 605 | fgets( savedBuf, 256, saved );
|
| 606 | NullLineEndings( verifyBuf );
|
| 607 | NullLineEndings( savedBuf );
|
| 608 |
|
| 609 | if ( strcmp( verifyBuf, savedBuf ) )
|
| 610 | {
|
| 611 | printf( "verify:%s<\n", verifyBuf );
|
| 612 | printf( "saved :%s<\n", savedBuf );
|
| 613 | okay = 0;
|
| 614 | break;
|
| 615 | }
|
| 616 | }
|
| 617 | }
|
| 618 | if ( saved )
|
| 619 | fclose( saved );
|
| 620 | if ( verify )
|
| 621 | fclose( verify );
|
| 622 | XMLTest( "UTF-8: Verified multi-language round trip.", 1, okay );
|
| 623 | }
|
| 624 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 625 | // --------GetText()-----------
|
| 626 | {
|
| 627 | const char* str = "<foo>This is text</foo>";
|
| 628 | XMLDocument doc;
|
| 629 | doc.Parse( str );
|
| 630 | const XMLElement* element = doc.RootElement();
|
| 631 |
|
| 632 | XMLTest( "GetText() normal use.", "This is text", element->GetText() );
|
| 633 |
|
| 634 | str = "<foo><b>This is text</b></foo>";
|
| 635 | doc.Parse( str );
|
| 636 | element = doc.RootElement();
|
| 637 |
|
| 638 | XMLTest( "GetText() contained element.", element->GetText() == 0, true );
|
| 639 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 640 |
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 641 |
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 642 | // --------SetText()-----------
|
| 643 | {
|
| 644 | const char* str = "<foo></foo>";
|
| 645 | XMLDocument doc;
|
| 646 | doc.Parse( str );
|
| 647 | XMLElement* element = doc.RootElement();
|
| 648 |
|
Lee Thomason | 9c0678a | 2014-01-24 10:18:27 -0800 | [diff] [blame] | 649 | element->SetText("darkness.");
|
| 650 | XMLTest( "SetText() normal use (open/close).", "darkness.", element->GetText() );
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 651 |
|
Lee Thomason | 9c0678a | 2014-01-24 10:18:27 -0800 | [diff] [blame] | 652 | element->SetText("blue flame.");
|
| 653 | XMLTest( "SetText() replace.", "blue flame.", element->GetText() );
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 654 |
|
| 655 | str = "<foo/>";
|
| 656 | doc.Parse( str );
|
| 657 | element = doc.RootElement();
|
| 658 |
|
Lee Thomason | 9c0678a | 2014-01-24 10:18:27 -0800 | [diff] [blame] | 659 | element->SetText("The driver");
|
| 660 | XMLTest( "SetText() normal use. (self-closing)", "The driver", element->GetText() );
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 661 |
|
Lee Thomason | 9c0678a | 2014-01-24 10:18:27 -0800 | [diff] [blame] | 662 | element->SetText("<b>horses</b>");
|
| 663 | XMLTest( "SetText() replace with tag-like text.", "<b>horses</b>", element->GetText() );
|
| 664 | //doc.Print();
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 665 |
|
| 666 | str = "<foo><bar>Text in nested element</bar></foo>";
|
| 667 | doc.Parse( str );
|
| 668 | element = doc.RootElement();
|
| 669 |
|
Lee Thomason | 9c0678a | 2014-01-24 10:18:27 -0800 | [diff] [blame] | 670 | element->SetText("wolves");
|
| 671 | XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() );
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 672 |
|
| 673 | str = "<foo/>";
|
| 674 | doc.Parse( str );
|
| 675 | element = doc.RootElement();
|
| 676 |
|
| 677 | element->SetText( "str" );
|
| 678 | XMLTest( "SetText types", "str", element->GetText() );
|
| 679 |
|
| 680 | element->SetText( 1 );
|
| 681 | XMLTest( "SetText types", "1", element->GetText() );
|
| 682 |
|
| 683 | element->SetText( 1U );
|
| 684 | XMLTest( "SetText types", "1", element->GetText() );
|
| 685 |
|
| 686 | element->SetText( true );
|
Doruk Turak | 1f212f3 | 2016-08-28 20:54:17 +0200 | [diff] [blame] | 687 | XMLTest( "SetText types", "true", element->GetText() );
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 688 |
|
| 689 | element->SetText( 1.5f );
|
| 690 | XMLTest( "SetText types", "1.5", element->GetText() );
|
| 691 |
|
| 692 | element->SetText( 1.5 );
|
| 693 | XMLTest( "SetText types", "1.5", element->GetText() );
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 694 | }
|
| 695 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 696 | // ---------- Attributes ---------
|
| 697 | {
|
| 698 | static const int64_t BIG = -123456789012345678;
|
| 699 | XMLDocument doc;
|
| 700 | XMLElement* element = doc.NewElement("element");
|
| 701 | doc.InsertFirstChild(element);
|
| 702 |
|
| 703 | {
|
| 704 | element->SetAttribute("attrib", int(-100));
|
| 705 | int v = 0;
|
| 706 | element->QueryIntAttribute("attrib", &v);
|
| 707 | XMLTest("Attribute: int", -100, v, true);
|
| 708 | element->QueryAttribute("attrib", &v);
|
| 709 | XMLTest("Attribute: int", -100, v, true);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 710 | XMLTest("Attribute: int", -100, element->IntAttribute("attrib"), true);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 711 | }
|
| 712 | {
|
| 713 | element->SetAttribute("attrib", unsigned(100));
|
| 714 | unsigned v = 0;
|
| 715 | element->QueryUnsignedAttribute("attrib", &v);
|
| 716 | XMLTest("Attribute: unsigned", unsigned(100), v, true);
|
| 717 | element->QueryAttribute("attrib", &v);
|
| 718 | XMLTest("Attribute: unsigned", unsigned(100), v, true);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 719 | XMLTest("Attribute: unsigned", unsigned(100), element->UnsignedAttribute("attrib"), true);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 720 | }
|
| 721 | {
|
| 722 | element->SetAttribute("attrib", BIG);
|
| 723 | int64_t v = 0;
|
| 724 | element->QueryInt64Attribute("attrib", &v);
|
| 725 | XMLTest("Attribute: int64_t", BIG, v, true);
|
| 726 | element->QueryAttribute("attrib", &v);
|
| 727 | XMLTest("Attribute: int64_t", BIG, v, true);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 728 | XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 729 | }
|
| 730 | {
|
| 731 | element->SetAttribute("attrib", true);
|
| 732 | bool v = false;
|
| 733 | element->QueryBoolAttribute("attrib", &v);
|
| 734 | XMLTest("Attribute: bool", true, v, true);
|
| 735 | element->QueryAttribute("attrib", &v);
|
| 736 | XMLTest("Attribute: bool", true, v, true);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 737 | XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 738 | }
|
| 739 | {
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 740 | element->SetAttribute("attrib", true);
|
| 741 | const char* result = element->Attribute("attrib");
|
| 742 | XMLTest("Bool true is 'true'", "true", result);
|
| 743 |
|
Lee Thomason | c5c99c2 | 2016-12-29 11:19:17 -0800 | [diff] [blame] | 744 | XMLUtil::SetBoolSerialization("1", "0");
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 745 | element->SetAttribute("attrib", true);
|
| 746 | result = element->Attribute("attrib");
|
| 747 | XMLTest("Bool true is '1'", "1", result);
|
| 748 |
|
Lee Thomason | c5c99c2 | 2016-12-29 11:19:17 -0800 | [diff] [blame] | 749 | XMLUtil::SetBoolSerialization(0, 0);
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 750 | }
|
| 751 | {
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 752 | element->SetAttribute("attrib", 100.0);
|
| 753 | double v = 0;
|
| 754 | element->QueryDoubleAttribute("attrib", &v);
|
| 755 | XMLTest("Attribute: double", 100.0, v, true);
|
| 756 | element->QueryAttribute("attrib", &v);
|
| 757 | XMLTest("Attribute: double", 100.0, v, true);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 758 | XMLTest("Attribute: double", 100.0, element->DoubleAttribute("attrib"), true);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 759 | }
|
| 760 | {
|
| 761 | element->SetAttribute("attrib", 100.0f);
|
| 762 | float v = 0;
|
| 763 | element->QueryFloatAttribute("attrib", &v);
|
| 764 | XMLTest("Attribute: float", 100.0f, v, true);
|
| 765 | element->QueryAttribute("attrib", &v);
|
| 766 | XMLTest("Attribute: float", 100.0f, v, true);
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 767 | XMLTest("Attribute: float", 100.0f, element->FloatAttribute("attrib"), true);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 768 | }
|
| 769 | {
|
| 770 | element->SetText(BIG);
|
| 771 | int64_t v = 0;
|
| 772 | element->QueryInt64Text(&v);
|
| 773 | XMLTest("Element: int64_t", BIG, v, true);
|
| 774 | }
|
| 775 | }
|
| 776 |
|
| 777 | // ---------- XMLPrinter stream mode ------
|
| 778 | {
|
| 779 | {
|
| 780 | FILE* printerfp = fopen("resources/printer.xml", "w");
|
| 781 | XMLPrinter printer(printerfp);
|
| 782 | printer.OpenElement("foo");
|
| 783 | printer.PushAttribute("attrib-text", "text");
|
| 784 | printer.PushAttribute("attrib-int", int(1));
|
| 785 | printer.PushAttribute("attrib-unsigned", unsigned(2));
|
| 786 | printer.PushAttribute("attrib-int64", int64_t(3));
|
| 787 | printer.PushAttribute("attrib-bool", true);
|
| 788 | printer.PushAttribute("attrib-double", 4.0);
|
| 789 | printer.CloseElement();
|
| 790 | fclose(printerfp);
|
| 791 | }
|
| 792 | {
|
| 793 | XMLDocument doc;
|
| 794 | doc.LoadFile("resources/printer.xml");
|
| 795 | XMLTest("XMLPrinter Stream mode: load", doc.ErrorID(), XML_SUCCESS, true);
|
| 796 |
|
| 797 | const XMLDocument& cdoc = doc;
|
| 798 |
|
| 799 | const XMLAttribute* attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-text");
|
| 800 | XMLTest("attrib-text", "text", attrib->Value(), true);
|
| 801 | attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int");
|
| 802 | XMLTest("attrib-int", int(1), attrib->IntValue(), true);
|
| 803 | attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-unsigned");
|
| 804 | XMLTest("attrib-unsigned", unsigned(2), attrib->UnsignedValue(), true);
|
| 805 | attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int64");
|
| 806 | XMLTest("attrib-int64", int64_t(3), attrib->Int64Value(), true);
|
| 807 | attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool");
|
| 808 | XMLTest("attrib-bool", true, attrib->BoolValue(), true);
|
| 809 | attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-double");
|
| 810 | XMLTest("attrib-double", 4.0, attrib->DoubleValue(), true);
|
| 811 | }
|
| 812 |
|
| 813 | }
|
| 814 |
|
Uli Kusterer | 321072e | 2014-01-21 01:57:38 +0100 | [diff] [blame] | 815 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 816 | // ---------- CDATA ---------------
|
| 817 | {
|
| 818 | const char* str = "<xmlElement>"
|
| 819 | "<![CDATA["
|
| 820 | "I am > the rules!\n"
|
| 821 | "...since I make symbolic puns"
|
| 822 | "]]>"
|
| 823 | "</xmlElement>";
|
| 824 | XMLDocument doc;
|
| 825 | doc.Parse( str );
|
| 826 | doc.Print();
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 827 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 828 | XMLTest( "CDATA parse.", doc.FirstChildElement()->FirstChild()->Value(),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 829 | "I am > the rules!\n...since I make symbolic puns",
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 830 | false );
|
| 831 | }
|
| 832 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 833 | // ----------- CDATA -------------
|
| 834 | {
|
| 835 | const char* str = "<xmlElement>"
|
| 836 | "<![CDATA["
|
| 837 | "<b>I am > the rules!</b>\n"
|
| 838 | "...since I make symbolic puns"
|
| 839 | "]]>"
|
| 840 | "</xmlElement>";
|
| 841 | XMLDocument doc;
|
| 842 | doc.Parse( str );
|
| 843 | doc.Print();
|
| 844 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 845 | XMLTest( "CDATA parse. [ tixml1:1480107 ]", doc.FirstChildElement()->FirstChild()->Value(),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 846 | "<b>I am > the rules!</b>\n...since I make symbolic puns",
|
| 847 | false );
|
| 848 | }
|
| 849 |
|
| 850 | // InsertAfterChild causes crash.
|
| 851 | {
|
| 852 | // InsertBeforeChild and InsertAfterChild causes crash.
|
| 853 | XMLDocument doc;
|
| 854 | XMLElement* parent = doc.NewElement( "Parent" );
|
| 855 | doc.InsertFirstChild( parent );
|
| 856 |
|
| 857 | XMLElement* childText0 = doc.NewElement( "childText0" );
|
| 858 | XMLElement* childText1 = doc.NewElement( "childText1" );
|
| 859 |
|
| 860 | XMLNode* childNode0 = parent->InsertEndChild( childText0 );
|
| 861 | XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
|
| 862 |
|
| 863 | XMLTest( "Test InsertAfterChild on empty node. ", ( childNode1 == parent->LastChild() ), true );
|
| 864 | }
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 865 |
|
| 866 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 867 | // Entities not being written correctly.
|
| 868 | // From Lynn Allen
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 869 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 870 | const char* passages =
|
| 871 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 872 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 873 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 874 | " It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
|
| 875 | "</passages>";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 876 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 877 | XMLDocument doc;
|
| 878 | doc.Parse( passages );
|
| 879 | XMLElement* psg = doc.RootElement()->FirstChildElement();
|
| 880 | const char* context = psg->Attribute( "context" );
|
| 881 | const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 882 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 883 | XMLTest( "Entity transformation: read. ", expected, context, true );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 884 |
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 885 | FILE* textfile = fopen( "resources/out/textfile.txt", "w" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 886 | if ( textfile )
|
| 887 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 888 | XMLPrinter streamer( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 889 | psg->Accept( &streamer );
|
| 890 | fclose( textfile );
|
| 891 | }
|
Thomas Roß | 0922b73 | 2012-09-23 16:31:22 +0200 | [diff] [blame] | 892 |
|
| 893 | textfile = fopen( "resources/out/textfile.txt", "r" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 894 | TIXMLASSERT( textfile );
|
| 895 | if ( textfile )
|
| 896 | {
|
| 897 | char buf[ 1024 ];
|
| 898 | fgets( buf, 1024, textfile );
|
| 899 | XMLTest( "Entity transformation: write. ",
|
| 900 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
|
| 901 | " It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
|
| 902 | buf, false );
|
PKEuS | c28ba3a | 2012-07-16 03:08:47 -0700 | [diff] [blame] | 903 | fclose( textfile );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 904 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 905 | }
|
| 906 |
|
| 907 | {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 908 | // Suppress entities.
|
| 909 | const char* passages =
|
| 910 | "<?xml version=\"1.0\" standalone=\"no\" ?>"
|
| 911 | "<passages count=\"006\" formatversion=\"20020620\">"
|
| 912 | "<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
|
| 913 | "</passages>";
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 914 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 915 | XMLDocument doc( false );
|
| 916 | doc.Parse( passages );
|
| 917 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 918 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ),
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 919 | "Line 5 has "quotation marks" and 'apostrophe marks'." );
|
| 920 | XMLTest( "No entity parsing.", doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value(),
|
| 921 | "Crazy &ttk;" );
|
| 922 | doc.Print();
|
| 923 | }
|
| 924 |
|
| 925 | {
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 926 | const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 927 |
|
| 928 | XMLDocument doc;
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 929 | doc.Parse( test );
|
| 930 | XMLTest( "dot in names", doc.Error(), false );
|
| 931 | XMLTest( "dot in names", doc.FirstChildElement()->Name(), "a.elem" );
|
| 932 | XMLTest( "dot in names", doc.FirstChildElement()->Attribute( "xmi.version" ), "2.0" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 933 | }
|
| 934 |
|
| 935 | {
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 936 | const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 937 |
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 938 | XMLDocument doc;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 939 | doc.Parse( test );
|
| 940 |
|
| 941 | XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
|
| 942 | XMLTest( "Entity with one digit.",
|
| 943 | text->Value(), "1.1 Start easy ignore fin thickness\n",
|
| 944 | false );
|
Arkadiy Shapkin | ef1c69c | 2012-07-25 22:10:39 +0400 | [diff] [blame] | 945 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 946 |
|
| 947 | {
|
| 948 | // DOCTYPE not preserved (950171)
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 949 | //
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 950 | const char* doctype =
|
| 951 | "<?xml version=\"1.0\" ?>"
|
| 952 | "<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
|
| 953 | "<!ELEMENT title (#PCDATA)>"
|
| 954 | "<!ELEMENT books (title,authors)>"
|
| 955 | "<element />";
|
| 956 |
|
| 957 | XMLDocument doc;
|
| 958 | doc.Parse( doctype );
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 959 | doc.SaveFile( "resources/out/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 960 | doc.DeleteChild( doc.RootElement() );
|
Arkadiy Shapkin | ff72d1f | 2012-07-24 00:24:07 +0400 | [diff] [blame] | 961 | doc.LoadFile( "resources/out/test7.xml" );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 962 | doc.Print();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 963 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 964 | const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
|
| 965 | XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
|
| 966 |
|
| 967 | }
|
| 968 |
|
| 969 | {
|
| 970 | // Comments do not stream out correctly.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 971 | const char* doctype =
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 972 | "<!-- Somewhat<evil> -->";
|
| 973 | XMLDocument doc;
|
| 974 | doc.Parse( doctype );
|
| 975 |
|
| 976 | XMLComment* comment = doc.FirstChild()->ToComment();
|
| 977 |
|
| 978 | XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
|
| 979 | }
|
| 980 | {
|
| 981 | // Double attributes
|
| 982 | const char* doctype = "<element attr='red' attr='blue' />";
|
| 983 |
|
| 984 | XMLDocument doc;
|
| 985 | doc.Parse( doctype );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 986 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 987 | XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
|
Lee Thomason (grinliz) | 0a4df40 | 2012-02-27 20:50:52 -0800 | [diff] [blame] | 988 | doc.PrintError();
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 989 | }
|
| 990 |
|
| 991 | {
|
| 992 | // Embedded null in stream.
|
| 993 | const char* doctype = "<element att\0r='red' attr='blue' />";
|
| 994 |
|
| 995 | XMLDocument doc;
|
| 996 | doc.Parse( doctype );
|
| 997 | XMLTest( "Embedded null throws error.", true, doc.Error() );
|
| 998 | }
|
| 999 |
|
| 1000 | {
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 1001 | // Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
|
Dmitry-Me | 588bb8d | 2014-12-25 18:59:18 +0300 | [diff] [blame] | 1002 | const char* str = "";
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1003 | XMLDocument doc;
|
| 1004 | doc.Parse( str );
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1005 | XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1006 | }
|
| 1007 |
|
| 1008 | {
|
Dmitry-Me | 588bb8d | 2014-12-25 18:59:18 +0300 | [diff] [blame] | 1009 | // Documents with all whitespaces should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
|
| 1010 | const char* str = " ";
|
| 1011 | XMLDocument doc;
|
| 1012 | doc.Parse( str );
|
| 1013 | XMLTest( "All whitespaces document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
|
| 1014 | }
|
| 1015 |
|
| 1016 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1017 | // Low entities
|
| 1018 | XMLDocument doc;
|
| 1019 | doc.Parse( "<test></test>" );
|
| 1020 | const char result[] = { 0x0e, 0 };
|
| 1021 | XMLTest( "Low entities.", doc.FirstChildElement()->GetText(), result );
|
| 1022 | doc.Print();
|
| 1023 | }
|
| 1024 |
|
| 1025 | {
|
| 1026 | // Attribute values with trailing quotes not handled correctly
|
| 1027 | XMLDocument doc;
|
| 1028 | doc.Parse( "<foo attribute=bar\" />" );
|
| 1029 | XMLTest( "Throw error with bad end quotes.", doc.Error(), true );
|
| 1030 | }
|
| 1031 |
|
| 1032 | {
|
| 1033 | // [ 1663758 ] Failure to report error on bad XML
|
| 1034 | XMLDocument xml;
|
| 1035 | xml.Parse("<x>");
|
| 1036 | XMLTest("Missing end tag at end of input", xml.Error(), true);
|
| 1037 | xml.Parse("<x> ");
|
| 1038 | XMLTest("Missing end tag with trailing whitespace", xml.Error(), true);
|
| 1039 | xml.Parse("<x></y>");
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1040 | XMLTest("Mismatched tags", xml.ErrorID(), XML_ERROR_MISMATCHED_ELEMENT);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1041 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1042 |
|
| 1043 |
|
| 1044 | {
|
| 1045 | // [ 1475201 ] TinyXML parses entities in comments
|
| 1046 | XMLDocument xml;
|
| 1047 | xml.Parse("<!-- declarations for <head> & <body> -->"
|
| 1048 | "<!-- far & away -->" );
|
| 1049 |
|
| 1050 | XMLNode* e0 = xml.FirstChild();
|
| 1051 | XMLNode* e1 = e0->NextSibling();
|
| 1052 | XMLComment* c0 = e0->ToComment();
|
| 1053 | XMLComment* c1 = e1->ToComment();
|
| 1054 |
|
| 1055 | XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
|
| 1056 | XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
|
| 1057 | }
|
| 1058 |
|
| 1059 | {
|
| 1060 | XMLDocument xml;
|
| 1061 | xml.Parse( "<Parent>"
|
| 1062 | "<child1 att=''/>"
|
| 1063 | "<!-- With this comment, child2 will not be parsed! -->"
|
| 1064 | "<child2 att=''/>"
|
| 1065 | "</Parent>" );
|
| 1066 | xml.Print();
|
| 1067 |
|
| 1068 | int count = 0;
|
| 1069 |
|
| 1070 | for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
|
| 1071 | ele;
|
| 1072 | ele = ele->NextSibling() )
|
| 1073 | {
|
| 1074 | ++count;
|
| 1075 | }
|
| 1076 |
|
| 1077 | XMLTest( "Comments iterate correctly.", 3, count );
|
| 1078 | }
|
| 1079 |
|
| 1080 | {
|
| 1081 | // trying to repro ]1874301]. If it doesn't go into an infinite loop, all is well.
|
| 1082 | unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
|
| 1083 | buf[60] = 239;
|
| 1084 | buf[61] = 0;
|
| 1085 |
|
| 1086 | XMLDocument doc;
|
| 1087 | doc.Parse( (const char*)buf);
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1088 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1089 |
|
| 1090 |
|
| 1091 | {
|
| 1092 | // bug 1827248 Error while parsing a little bit malformed file
|
| 1093 | // Actually not malformed - should work.
|
| 1094 | XMLDocument xml;
|
| 1095 | xml.Parse( "<attributelist> </attributelist >" );
|
| 1096 | XMLTest( "Handle end tag whitespace", false, xml.Error() );
|
| 1097 | }
|
| 1098 |
|
| 1099 | {
|
| 1100 | // This one must not result in an infinite loop
|
| 1101 | XMLDocument xml;
|
| 1102 | xml.Parse( "<infinite>loop" );
|
| 1103 | XMLTest( "Infinite loop test.", true, true );
|
| 1104 | }
|
| 1105 | #endif
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1106 | {
|
| 1107 | const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
|
| 1108 | XMLDocument doc;
|
| 1109 | doc.Parse( pub );
|
| 1110 |
|
| 1111 | XMLDocument clone;
|
| 1112 | for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
|
| 1113 | XMLNode* copy = node->ShallowClone( &clone );
|
| 1114 | clone.InsertEndChild( copy );
|
| 1115 | }
|
| 1116 |
|
| 1117 | clone.Print();
|
| 1118 |
|
| 1119 | int count=0;
|
| 1120 | const XMLNode* a=clone.FirstChild();
|
| 1121 | const XMLNode* b=doc.FirstChild();
|
| 1122 | for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
|
| 1123 | ++count;
|
| 1124 | XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
|
| 1125 | }
|
| 1126 | XMLTest( "Clone and Equal", 4, count );
|
| 1127 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1128 |
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 1129 | {
|
| 1130 | // This shouldn't crash.
|
| 1131 | XMLDocument doc;
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1132 | if(XML_SUCCESS != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 1133 | {
|
| 1134 | doc.PrintError();
|
| 1135 | }
|
| 1136 | XMLTest( "Error in snprinf handling.", true, doc.Error() );
|
| 1137 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1138 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 1139 | {
|
| 1140 | // Attribute ordering.
|
| 1141 | static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
|
| 1142 | XMLDocument doc;
|
| 1143 | doc.Parse( xml );
|
| 1144 | XMLElement* ele = doc.FirstChildElement();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1145 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 1146 | const XMLAttribute* a = ele->FirstAttribute();
|
| 1147 | XMLTest( "Attribute order", "1", a->Value() );
|
| 1148 | a = a->Next();
|
| 1149 | XMLTest( "Attribute order", "2", a->Value() );
|
| 1150 | a = a->Next();
|
| 1151 | XMLTest( "Attribute order", "3", a->Value() );
|
| 1152 | XMLTest( "Attribute order", "attrib3", a->Name() );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1153 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 1154 | ele->DeleteAttribute( "attrib2" );
|
| 1155 | a = ele->FirstAttribute();
|
| 1156 | XMLTest( "Attribute order", "1", a->Value() );
|
| 1157 | a = a->Next();
|
| 1158 | XMLTest( "Attribute order", "3", a->Value() );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1159 |
|
Lee Thomason | 5e3803c | 2012-04-16 08:57:05 -0700 | [diff] [blame] | 1160 | ele->DeleteAttribute( "attrib1" );
|
| 1161 | ele->DeleteAttribute( "attrib3" );
|
| 1162 | XMLTest( "Attribute order (empty)", false, ele->FirstAttribute() ? true : false );
|
| 1163 | }
|
Lee Thomason (grinliz) | a4a36ba | 2012-04-06 21:24:29 -0700 | [diff] [blame] | 1164 |
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 1165 | {
|
| 1166 | // Make sure an attribute with a space in it succeeds.
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1167 | static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
|
| 1168 | static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
|
| 1169 | static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
|
| 1170 | XMLDocument doc0;
|
| 1171 | doc0.Parse( xml0 );
|
| 1172 | XMLDocument doc1;
|
| 1173 | doc1.Parse( xml1 );
|
| 1174 | XMLDocument doc2;
|
| 1175 | doc2.Parse( xml2 );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 1176 |
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1177 | XMLElement* ele = 0;
|
| 1178 | ele = doc0.FirstChildElement();
|
| 1179 | XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 1180 | ele = doc1.FirstChildElement();
|
| 1181 | XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
|
| 1182 | ele = doc2.FirstChildElement();
|
| 1183 | XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
|
Lee Thomason (grinliz) | 390e978 | 2012-07-01 21:22:53 -0700 | [diff] [blame] | 1184 | }
|
| 1185 |
|
| 1186 | {
|
| 1187 | // Make sure we don't go into an infinite loop.
|
| 1188 | static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
|
| 1189 | XMLDocument doc;
|
| 1190 | doc.Parse( xml );
|
| 1191 | XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
|
| 1192 | XMLElement* ele1 = ele0->NextSiblingElement();
|
| 1193 | bool equal = ele0->ShallowEqual( ele1 );
|
| 1194 |
|
| 1195 | XMLTest( "Infinite loop in shallow equal.", true, equal );
|
| 1196 | }
|
| 1197 |
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 1198 | // -------- Handles ------------
|
| 1199 | {
|
| 1200 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 1201 | XMLDocument doc;
|
| 1202 | doc.Parse( xml );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 1203 |
|
| 1204 | XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
|
| 1205 | XMLTest( "Handle, success, mutable", ele->Value(), "sub" );
|
| 1206 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1207 | XMLHandle docH( doc );
|
| 1208 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 1209 | XMLTest( "Handle, dne, mutable", false, ele != 0 );
|
Lee Thomason | 5708f81 | 2012-03-28 17:46:41 -0700 | [diff] [blame] | 1210 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1211 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1212 | {
|
| 1213 | static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
|
| 1214 | XMLDocument doc;
|
| 1215 | doc.Parse( xml );
|
| 1216 | XMLConstHandle docH( doc );
|
| 1217 |
|
| 1218 | const XMLElement* ele = docH.FirstChildElement( "element" ).FirstChild().ToElement();
|
| 1219 | XMLTest( "Handle, success, const", ele->Value(), "sub" );
|
| 1220 |
|
| 1221 | ele = docH.FirstChildElement( "none" ).FirstChildElement( "element" ).ToElement();
|
Lee Thomason | d0b19df | 2012-04-12 08:41:37 -0700 | [diff] [blame] | 1222 | XMLTest( "Handle, dne, const", false, ele != 0 );
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1223 | }
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1224 | {
|
| 1225 | // Default Declaration & BOM
|
| 1226 | XMLDocument doc;
|
| 1227 | doc.InsertEndChild( doc.NewDeclaration() );
|
| 1228 | doc.SetBOM( true );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1229 |
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1230 | XMLPrinter printer;
|
| 1231 | doc.Print( &printer );
|
| 1232 |
|
| 1233 | static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
| 1234 | XMLTest( "BOM and default declaration", printer.CStr(), result, false );
|
Lee Thomason (grinliz) | 48ea0bc | 2012-05-26 14:41:14 -0700 | [diff] [blame] | 1235 | XMLTest( "CStrSize", printer.CStrSize(), 42, false );
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1236 | }
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1237 | {
|
| 1238 | const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
|
| 1239 | XMLDocument doc;
|
| 1240 | doc.Parse( xml );
|
| 1241 | XMLTest( "Ill formed XML", true, doc.Error() );
|
| 1242 | }
|
| 1243 |
|
| 1244 | // QueryXYZText
|
| 1245 | {
|
| 1246 | const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
|
| 1247 | XMLDocument doc;
|
| 1248 | doc.Parse( xml );
|
| 1249 |
|
| 1250 | const XMLElement* pointElement = doc.RootElement();
|
| 1251 |
|
| 1252 | int intValue = 0;
|
| 1253 | unsigned unsignedValue = 0;
|
| 1254 | float floatValue = 0;
|
| 1255 | double doubleValue = 0;
|
| 1256 | bool boolValue = false;
|
| 1257 |
|
| 1258 | pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
|
| 1259 | pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
|
| 1260 | pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
|
| 1261 | pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
|
| 1262 | pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
|
| 1263 |
|
| 1264 |
|
| 1265 | XMLTest( "QueryIntText", intValue, 1, false );
|
| 1266 | XMLTest( "QueryUnsignedText", unsignedValue, (unsigned)1, false );
|
| 1267 | XMLTest( "QueryFloatText", floatValue, 1.2f, false );
|
| 1268 | XMLTest( "QueryDoubleText", doubleValue, 1.2, false );
|
| 1269 | XMLTest( "QueryBoolText", boolValue, true, false );
|
| 1270 | }
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1271 |
|
Lee Thomason (grinliz) | 5fbacbe | 2012-09-08 21:40:53 -0700 | [diff] [blame] | 1272 | {
|
| 1273 | const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
|
| 1274 | XMLDocument doc;
|
| 1275 | doc.Parse( xml );
|
| 1276 | XMLTest( "Non-alpha element lead letter parses.", doc.Error(), false );
|
| 1277 | }
|
Martinsh Shaiters | 23e7ae6 | 2013-01-26 20:15:44 +0200 | [diff] [blame] | 1278 |
|
| 1279 | {
|
| 1280 | const char* xml = "<element _attr1=\"foo\" :attr2=\"bar\"></element>";
|
| 1281 | XMLDocument doc;
|
Martinsh Shaiters | 95b3e65 | 2013-01-26 23:08:10 +0200 | [diff] [blame] | 1282 | doc.Parse( xml );
|
Martinsh Shaiters | 23e7ae6 | 2013-01-26 20:15:44 +0200 | [diff] [blame] | 1283 | XMLTest("Non-alpha attribute lead character parses.", doc.Error(), false);
|
| 1284 | }
|
Martinsh Shaiters | 95b3e65 | 2013-01-26 23:08:10 +0200 | [diff] [blame] | 1285 |
|
| 1286 | {
|
| 1287 | const char* xml = "<3lement></3lement>";
|
| 1288 | XMLDocument doc;
|
| 1289 | doc.Parse( xml );
|
| 1290 | XMLTest("Element names with lead digit fail to parse.", doc.Error(), true);
|
| 1291 | }
|
Lee Thomason (grinliz) | 62d1c5a | 2012-09-08 21:44:12 -0700 | [diff] [blame] | 1292 |
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1293 | {
|
| 1294 | const char* xml = "<element/>WOA THIS ISN'T GOING TO PARSE";
|
| 1295 | XMLDocument doc;
|
| 1296 | doc.Parse( xml, 10 );
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1297 | XMLTest( "Set length of incoming data", doc.Error(), false );
|
| 1298 | }
|
| 1299 |
|
Martinsh Shaiters | 53ab79a | 2013-01-30 11:21:36 +0200 | [diff] [blame] | 1300 | {
|
| 1301 | XMLDocument doc;
|
Dmitry-Me | 48b5df0 | 2015-04-06 18:20:25 +0300 | [diff] [blame] | 1302 | XMLTest( "Document is initially empty", doc.NoChildren(), true );
|
| 1303 | doc.Clear();
|
| 1304 | XMLTest( "Empty is empty after Clear()", doc.NoChildren(), true );
|
Martinsh Shaiters | 53ab79a | 2013-01-30 11:21:36 +0200 | [diff] [blame] | 1305 | doc.LoadFile( "resources/dream.xml" );
|
Dmitry-Me | aaa4cea | 2015-02-06 16:00:46 +0300 | [diff] [blame] | 1306 | XMLTest( "Document has something to Clear()", doc.NoChildren(), false );
|
Martinsh Shaiters | 53ab79a | 2013-01-30 11:21:36 +0200 | [diff] [blame] | 1307 | doc.Clear();
|
| 1308 | XMLTest( "Document Clear()'s", doc.NoChildren(), true );
|
| 1309 | }
|
| 1310 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1311 | // ----------- Whitespace ------------
|
| 1312 | {
|
| 1313 | const char* xml = "<element>"
|
| 1314 | "<a> This \nis ' text ' </a>"
|
| 1315 | "<b> This is ' text ' \n</b>"
|
| 1316 | "<c>This is ' \n\n text '</c>"
|
| 1317 | "</element>";
|
| 1318 | XMLDocument doc( true, COLLAPSE_WHITESPACE );
|
| 1319 | doc.Parse( xml );
|
| 1320 |
|
| 1321 | const XMLElement* element = doc.FirstChildElement();
|
| 1322 | for( const XMLElement* parent = element->FirstChildElement();
|
| 1323 | parent;
|
| 1324 | parent = parent->NextSiblingElement() )
|
| 1325 | {
|
| 1326 | XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
|
| 1327 | }
|
| 1328 | }
|
Lee Thomason (grinliz) | 0fa8299 | 2012-09-08 21:53:47 -0700 | [diff] [blame] | 1329 |
|
Lee Thomason | ae9ab07 | 2012-10-24 10:17:53 -0700 | [diff] [blame] | 1330 | #if 0
|
| 1331 | {
|
| 1332 | // Passes if assert doesn't fire.
|
| 1333 | XMLDocument xmlDoc;
|
| 1334 |
|
| 1335 | xmlDoc.NewDeclaration();
|
| 1336 | xmlDoc.NewComment("Configuration file");
|
| 1337 |
|
| 1338 | XMLElement *root = xmlDoc.NewElement("settings");
|
| 1339 | root->SetAttribute("version", 2);
|
| 1340 | }
|
| 1341 | #endif
|
| 1342 |
|
Lee Thomason (grinliz) | 0fa8299 | 2012-09-08 21:53:47 -0700 | [diff] [blame] | 1343 | {
|
| 1344 | const char* xml = "<element> </element>";
|
| 1345 | XMLDocument doc( true, COLLAPSE_WHITESPACE );
|
| 1346 | doc.Parse( xml );
|
| 1347 | XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
|
| 1348 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1349 |
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 1350 | {
|
| 1351 | // An assert should not fire.
|
| 1352 | const char* xml = "<element/>";
|
| 1353 | XMLDocument doc;
|
| 1354 | doc.Parse( xml );
|
| 1355 | XMLElement* ele = doc.NewElement( "unused" ); // This will get cleaned up with the 'doc' going out of scope.
|
| 1356 | XMLTest( "Tracking unused elements", true, ele != 0, false );
|
| 1357 | }
|
| 1358 |
|
Lee Thomason | a6412ac | 2012-12-13 15:39:11 -0800 | [diff] [blame] | 1359 |
|
| 1360 | {
|
| 1361 | const char* xml = "<parent><child>abc</child></parent>";
|
| 1362 | XMLDocument doc;
|
| 1363 | doc.Parse( xml );
|
| 1364 | XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child");
|
| 1365 |
|
| 1366 | XMLPrinter printer;
|
| 1367 | ele->Accept( &printer );
|
| 1368 | XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false );
|
| 1369 | }
|
| 1370 |
|
| 1371 |
|
Vasily Biryukov | 1cfafd0 | 2013-04-20 14:12:33 +0600 | [diff] [blame] | 1372 | {
|
| 1373 | XMLDocument doc;
|
| 1374 | XMLError error = doc.LoadFile( "resources/empty.xml" );
|
| 1375 | XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error );
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 1376 | XMLTest( "Loading an empty file and ErrorName as string", "XML_ERROR_EMPTY_DOCUMENT", doc.ErrorName() );
|
Lee Thomason | c755667 | 2014-09-14 12:39:42 -0700 | [diff] [blame] | 1377 | doc.PrintError();
|
Vasily Biryukov | 1cfafd0 | 2013-04-20 14:12:33 +0600 | [diff] [blame] | 1378 | }
|
| 1379 |
|
Lee Thomason (grinliz) | d0a38c3 | 2013-04-29 09:15:37 -0700 | [diff] [blame] | 1380 | {
|
| 1381 | // BOM preservation
|
| 1382 | static const char* xml_bom_preservation = "\xef\xbb\xbf<element/>\n";
|
| 1383 | {
|
| 1384 | XMLDocument doc;
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1385 | XMLTest( "BOM preservation (parse)", XML_SUCCESS, doc.Parse( xml_bom_preservation ), false );
|
Lee Thomason (grinliz) | d0a38c3 | 2013-04-29 09:15:37 -0700 | [diff] [blame] | 1386 | XMLPrinter printer;
|
| 1387 | doc.Print( &printer );
|
| 1388 |
|
| 1389 | XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
|
| 1390 | doc.SaveFile( "resources/bomtest.xml" );
|
| 1391 | }
|
| 1392 | {
|
| 1393 | XMLDocument doc;
|
| 1394 | doc.LoadFile( "resources/bomtest.xml" );
|
| 1395 | XMLTest( "BOM preservation (load)", true, doc.HasBOM(), false );
|
| 1396 |
|
| 1397 | XMLPrinter printer;
|
| 1398 | doc.Print( &printer );
|
| 1399 | XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
|
| 1400 | }
|
| 1401 | }
|
Vasily Biryukov | 1cfafd0 | 2013-04-20 14:12:33 +0600 | [diff] [blame] | 1402 |
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1403 | {
|
| 1404 | // Insertion with Removal
|
| 1405 | const char* xml = "<?xml version=\"1.0\" ?>"
|
| 1406 | "<root>"
|
| 1407 | "<one>"
|
| 1408 | "<subtree>"
|
| 1409 | "<elem>element 1</elem>text<!-- comment -->"
|
| 1410 | "</subtree>"
|
| 1411 | "</one>"
|
| 1412 | "<two/>"
|
| 1413 | "</root>";
|
| 1414 | const char* xmlInsideTwo = "<?xml version=\"1.0\" ?>"
|
| 1415 | "<root>"
|
| 1416 | "<one/>"
|
| 1417 | "<two>"
|
| 1418 | "<subtree>"
|
| 1419 | "<elem>element 1</elem>text<!-- comment -->"
|
| 1420 | "</subtree>"
|
| 1421 | "</two>"
|
| 1422 | "</root>";
|
| 1423 | const char* xmlAfterOne = "<?xml version=\"1.0\" ?>"
|
| 1424 | "<root>"
|
| 1425 | "<one/>"
|
| 1426 | "<subtree>"
|
| 1427 | "<elem>element 1</elem>text<!-- comment -->"
|
| 1428 | "</subtree>"
|
| 1429 | "<two/>"
|
| 1430 | "</root>";
|
| 1431 | const char* xmlAfterTwo = "<?xml version=\"1.0\" ?>"
|
| 1432 | "<root>"
|
| 1433 | "<one/>"
|
| 1434 | "<two/>"
|
| 1435 | "<subtree>"
|
| 1436 | "<elem>element 1</elem>text<!-- comment -->"
|
| 1437 | "</subtree>"
|
| 1438 | "</root>";
|
| 1439 |
|
| 1440 | XMLDocument doc;
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1441 | doc.Parse(xml);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1442 | XMLElement* subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
|
| 1443 | XMLElement* two = doc.RootElement()->FirstChildElement("two");
|
| 1444 | two->InsertFirstChild(subtree);
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1445 | XMLPrinter printer1(0, true);
|
| 1446 | doc.Accept(&printer1);
|
| 1447 | XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1448 |
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1449 | doc.Parse(xml);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1450 | subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
|
| 1451 | two = doc.RootElement()->FirstChildElement("two");
|
| 1452 | doc.RootElement()->InsertAfterChild(two, subtree);
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1453 | XMLPrinter printer2(0, true);
|
| 1454 | doc.Accept(&printer2);
|
| 1455 | XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1456 |
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1457 | doc.Parse(xml);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1458 | XMLNode* one = doc.RootElement()->FirstChildElement("one");
|
| 1459 | subtree = one->FirstChildElement("subtree");
|
| 1460 | doc.RootElement()->InsertAfterChild(one, subtree);
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1461 | XMLPrinter printer3(0, true);
|
| 1462 | doc.Accept(&printer3);
|
| 1463 | XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1464 |
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1465 | doc.Parse(xml);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1466 | subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
|
| 1467 | two = doc.RootElement()->FirstChildElement("two");
|
| 1468 | doc.RootElement()->InsertEndChild(subtree);
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1469 | XMLPrinter printer4(0, true);
|
| 1470 | doc.Accept(&printer4);
|
| 1471 | XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false);
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 1472 | }
|
| 1473 |
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1474 | {
|
| 1475 | const char* xml = "<svg width = \"128\" height = \"128\">"
|
| 1476 | " <text> </text>"
|
| 1477 | "</svg>";
|
| 1478 | XMLDocument doc;
|
| 1479 | doc.Parse(xml);
|
| 1480 | doc.Print();
|
| 1481 | }
|
| 1482 |
|
Lee Thomason | 92e521b | 2014-11-15 17:45:51 -0800 | [diff] [blame] | 1483 | {
|
| 1484 | // Test that it doesn't crash.
|
Lee Thomason | cd011bc | 2014-12-17 10:41:34 -0800 | [diff] [blame] | 1485 | const char* xml = "<?xml version=\"1.0\"?><root><sample><field0><1</field0><field1>2</field1></sample></root>";
|
| 1486 | XMLDocument doc;
|
| 1487 | doc.Parse(xml);
|
| 1488 | doc.PrintError();
|
Lee Thomason | 92e521b | 2014-11-15 17:45:51 -0800 | [diff] [blame] | 1489 | }
|
| 1490 |
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1491 | #if 1
|
| 1492 | // the question being explored is what kind of print to use:
|
| 1493 | // https://github.com/leethomason/tinyxml2/issues/63
|
| 1494 | {
|
| 1495 | //const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9' attrC='1.0e-10' attrD='1001000000.000000' attrE='0.1234567890123456789'/>";
|
| 1496 | const char* xml = "<element/>";
|
| 1497 | XMLDocument doc;
|
| 1498 | doc.Parse( xml );
|
| 1499 | doc.FirstChildElement()->SetAttribute( "attrA-f64", 123456789.123456789 );
|
| 1500 | doc.FirstChildElement()->SetAttribute( "attrB-f64", 1.001e9 );
|
| 1501 | doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e9 );
|
| 1502 | doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e20 );
|
| 1503 | doc.FirstChildElement()->SetAttribute( "attrD-f64", 1.0e-10 );
|
| 1504 | doc.FirstChildElement()->SetAttribute( "attrD-f64", 0.123456789 );
|
| 1505 |
|
| 1506 | doc.FirstChildElement()->SetAttribute( "attrA-f32", 123456789.123456789f );
|
| 1507 | doc.FirstChildElement()->SetAttribute( "attrB-f32", 1.001e9f );
|
| 1508 | doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e9f );
|
| 1509 | doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e20f );
|
| 1510 | doc.FirstChildElement()->SetAttribute( "attrD-f32", 1.0e-10f );
|
| 1511 | doc.FirstChildElement()->SetAttribute( "attrD-f32", 0.123456789f );
|
| 1512 |
|
| 1513 | doc.Print();
|
| 1514 |
|
| 1515 | /* The result of this test is platform, compiler, and library version dependent. :("
|
| 1516 | XMLPrinter printer;
|
| 1517 | doc.Print( &printer );
|
| 1518 | XMLTest( "Float and double formatting.",
|
| 1519 | "<element attrA-f64=\"123456789.12345679\" attrB-f64=\"1001000000\" attrC-f64=\"1e+20\" attrD-f64=\"0.123456789\" attrA-f32=\"1.2345679e+08\" attrB-f32=\"1.001e+09\" attrC-f32=\"1e+20\" attrD-f32=\"0.12345679\"/>\n",
|
| 1520 | printer.CStr(),
|
| 1521 | true );
|
| 1522 | */
|
| 1523 | }
|
| 1524 | #endif
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 1525 |
|
| 1526 | {
|
| 1527 | // Issue #184
|
| 1528 | // If it doesn't assert, it passes. Caused by objects
|
| 1529 | // getting created during parsing which are then
|
| 1530 | // inaccessible in the memory pools.
|
| 1531 | {
|
| 1532 | XMLDocument doc;
|
| 1533 | doc.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>");
|
| 1534 | }
|
| 1535 | {
|
| 1536 | XMLDocument doc;
|
| 1537 | doc.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>");
|
| 1538 | doc.Clear();
|
| 1539 | }
|
| 1540 | }
|
Lee Thomason | cd011bc | 2014-12-17 10:41:34 -0800 | [diff] [blame] | 1541 |
|
| 1542 | {
|
| 1543 | // If this doesn't assert in DEBUG, all is well.
|
| 1544 | tinyxml2::XMLDocument doc;
|
| 1545 | tinyxml2::XMLElement *pRoot = doc.NewElement("Root");
|
| 1546 | doc.DeleteNode(pRoot);
|
| 1547 | }
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1548 |
|
Dmitry-Me | 8b67d74 | 2014-12-22 11:35:12 +0300 | [diff] [blame] | 1549 | {
|
| 1550 | // Should not assert in DEBUG
|
| 1551 | XMLPrinter printer;
|
| 1552 | }
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1553 |
|
Dmitry-Me | 6f51c80 | 2015-03-14 13:25:03 +0300 | [diff] [blame] | 1554 | {
|
| 1555 | // Issue 291. Should not crash
|
| 1556 | const char* xml = "�</a>";
|
| 1557 | XMLDocument doc;
|
| 1558 | doc.Parse( xml );
|
| 1559 |
|
| 1560 | XMLPrinter printer;
|
| 1561 | doc.Print( &printer );
|
| 1562 | }
|
Ant Mitchell | 148cc1a | 2015-03-24 15:12:35 +0000 | [diff] [blame] | 1563 | {
|
| 1564 | // Issue 299. Can print elements that are not linked in.
|
| 1565 | // Will crash if issue not fixed.
|
| 1566 | XMLDocument doc;
|
| 1567 | XMLElement* newElement = doc.NewElement( "printme" );
|
| 1568 | XMLPrinter printer;
|
| 1569 | newElement->Accept( &printer );
|
Dmitry-Me | 5daa54c | 2015-04-08 17:45:07 +0300 | [diff] [blame] | 1570 | // Delete the node to avoid possible memory leak report in debug output
|
| 1571 | doc.DeleteNode( newElement );
|
Ant Mitchell | 148cc1a | 2015-03-24 15:12:35 +0000 | [diff] [blame] | 1572 | }
|
Lee Thomason | f657783 | 2015-03-26 11:18:21 -0700 | [diff] [blame] | 1573 | {
|
Ant Mitchell | 189198f | 2015-03-24 16:20:36 +0000 | [diff] [blame] | 1574 | // Issue 302. Clear errors from LoadFile/SaveFile
|
| 1575 | XMLDocument doc;
|
Dmitry-Me | 32533ca | 2015-04-07 10:37:39 +0300 | [diff] [blame] | 1576 | XMLTest( "Issue 302. Should be no error initially", "XML_SUCCESS", doc.ErrorName() );
|
Ant Mitchell | 189198f | 2015-03-24 16:20:36 +0000 | [diff] [blame] | 1577 | doc.SaveFile( "./no/such/path/pretty.xml" );
|
Dmitry-Me | 32533ca | 2015-04-07 10:37:39 +0300 | [diff] [blame] | 1578 | XMLTest( "Issue 302. Fail to save", "XML_ERROR_FILE_COULD_NOT_BE_OPENED", doc.ErrorName() );
|
Ant Mitchell | 189198f | 2015-03-24 16:20:36 +0000 | [diff] [blame] | 1579 | doc.SaveFile( "./resources/out/compact.xml", true );
|
Dmitry-Me | 32533ca | 2015-04-07 10:37:39 +0300 | [diff] [blame] | 1580 | XMLTest( "Issue 302. Subsequent success in saving", "XML_SUCCESS", doc.ErrorName() );
|
Ant Mitchell | 189198f | 2015-03-24 16:20:36 +0000 | [diff] [blame] | 1581 | }
|
Dmitry-Me | 6f51c80 | 2015-03-14 13:25:03 +0300 | [diff] [blame] | 1582 |
|
Dmitry-Me | d9852a5 | 2015-03-25 10:17:49 +0300 | [diff] [blame] | 1583 | {
|
| 1584 | // If a document fails to load then subsequent
|
| 1585 | // successful loads should clear the error
|
| 1586 | XMLDocument doc;
|
Dmitry-Me | 32533ca | 2015-04-07 10:37:39 +0300 | [diff] [blame] | 1587 | XMLTest( "Should be no error initially", false, doc.Error() );
|
Dmitry-Me | d9852a5 | 2015-03-25 10:17:49 +0300 | [diff] [blame] | 1588 | doc.LoadFile( "resources/no-such-file.xml" );
|
| 1589 | XMLTest( "No such file - should fail", true, doc.Error() );
|
| 1590 |
|
| 1591 | doc.LoadFile( "resources/dream.xml" );
|
| 1592 | XMLTest( "Error should be cleared", false, doc.Error() );
|
| 1593 | }
|
Sarat Addepalli | 8e85afa | 2015-05-19 09:07:03 +0530 | [diff] [blame] | 1594 |
|
Sarat Addepalli | 2bb6bb5 | 2015-05-18 09:16:34 +0530 | [diff] [blame] | 1595 | {
|
Dmitry-Me | 446c3bc | 2016-11-11 10:34:56 +0300 | [diff] [blame] | 1596 | // Check that declarations are allowed only at beginning of document
|
Lee Thomason | 8549202 | 2015-05-22 11:07:45 -0700 | [diff] [blame] | 1597 | const char* xml0 = "<?xml version=\"1.0\" ?>"
|
| 1598 | " <!-- xml version=\"1.1\" -->"
|
| 1599 | "<first />";
|
| 1600 | const char* xml1 = "<?xml version=\"1.0\" ?>"
|
Dmitry-Me | 446c3bc | 2016-11-11 10:34:56 +0300 | [diff] [blame] | 1601 | "<?xml-stylesheet type=\"text/xsl\" href=\"Anything.xsl\"?>"
|
Lee Thomason | 8549202 | 2015-05-22 11:07:45 -0700 | [diff] [blame] | 1602 | "<first />";
|
| 1603 | const char* xml2 = "<first />"
|
| 1604 | "<?xml version=\"1.0\" ?>";
|
Dmitry-Me | 446c3bc | 2016-11-11 10:34:56 +0300 | [diff] [blame] | 1605 | const char* xml3 = "<first></first>"
|
| 1606 | "<?xml version=\"1.0\" ?>";
|
| 1607 |
|
| 1608 | const char* xml4 = "<first><?xml version=\"1.0\" ?></first>";
|
| 1609 |
|
Lee Thomason | 8549202 | 2015-05-22 11:07:45 -0700 | [diff] [blame] | 1610 | XMLDocument doc;
|
| 1611 | doc.Parse(xml0);
|
| 1612 | XMLTest("Test that the code changes do not affect normal parsing", doc.Error(), false);
|
| 1613 | doc.Parse(xml1);
|
Dmitry-Me | 446c3bc | 2016-11-11 10:34:56 +0300 | [diff] [blame] | 1614 | XMLTest("Test that the second declaration is allowed", doc.Error(), false);
|
Lee Thomason | 8549202 | 2015-05-22 11:07:45 -0700 | [diff] [blame] | 1615 | doc.Parse(xml2);
|
Dmitry-Me | 446c3bc | 2016-11-11 10:34:56 +0300 | [diff] [blame] | 1616 | XMLTest("Test that declaration after a child is not allowed", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
|
| 1617 | doc.Parse(xml3);
|
| 1618 | XMLTest("Test that declaration after a child is not allowed", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
|
| 1619 | doc.Parse(xml4);
|
| 1620 | XMLTest("Test that declaration inside a child is not allowed", doc.ErrorID(), XML_ERROR_PARSING_DECLARATION);
|
Sarat Addepalli | 2bb6bb5 | 2015-05-18 09:16:34 +0530 | [diff] [blame] | 1621 | }
|
Dmitry-Me | d9852a5 | 2015-03-25 10:17:49 +0300 | [diff] [blame] | 1622 |
|
Lee Thomason | 8549202 | 2015-05-22 11:07:45 -0700 | [diff] [blame] | 1623 | {
|
| 1624 | // No matter - before or after successfully parsing a text -
|
| 1625 | // calling XMLDocument::Value() causes an assert in debug.
|
| 1626 | const char* validXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
|
| 1627 | "<first />"
|
| 1628 | "<second />";
|
| 1629 | XMLDocument* doc = new XMLDocument();
|
| 1630 | XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
|
| 1631 | doc->Parse( validXml );
|
| 1632 | XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
|
| 1633 | delete doc;
|
| 1634 | }
|
| 1635 |
|
Dmitry-Me | a1beddf | 2015-05-26 16:19:21 +0300 | [diff] [blame] | 1636 | {
|
| 1637 | XMLDocument doc;
|
| 1638 | for( int i = 0; i < XML_ERROR_COUNT; i++ ) {
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1639 | doc.SetError( (XMLError)i, 0, 0, 0 );
|
Dmitry-Me | a1beddf | 2015-05-26 16:19:21 +0300 | [diff] [blame] | 1640 | doc.ErrorName();
|
| 1641 | }
|
| 1642 | }
|
| 1643 |
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1644 | // ----------- Line Number Tracking --------------
|
| 1645 | {
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1646 | struct TestUtil: XMLVisitor
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1647 | {
|
| 1648 | void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine)
|
| 1649 | {
|
| 1650 | XMLDocument doc;
|
| 1651 | XMLError err = doc.Parse(docStr);
|
| 1652 |
|
| 1653 | XMLTest(testString, true, doc.Error());
|
| 1654 | XMLTest(testString, expected_error, err);
|
| 1655 | XMLTest(testString, expectedLine, doc.GetErrorLineNum());
|
| 1656 | };
|
| 1657 |
|
| 1658 | void TestStringLines(const char *testString, const char *docStr, const char *expectedLines)
|
| 1659 | {
|
| 1660 | XMLDocument doc;
|
| 1661 | doc.Parse(docStr);
|
| 1662 | XMLTest(testString, false, doc.Error());
|
| 1663 | TestDocLines(testString, doc, expectedLines);
|
| 1664 | }
|
| 1665 |
|
| 1666 | void TestFileLines(const char *testString, const char *file_name, const char *expectedLines)
|
| 1667 | {
|
| 1668 | XMLDocument doc;
|
| 1669 | doc.LoadFile(file_name);
|
| 1670 | XMLTest(testString, false, doc.Error());
|
| 1671 | TestDocLines(testString, doc, expectedLines);
|
| 1672 | }
|
| 1673 |
|
| 1674 | private:
|
| 1675 | DynArray<char, 10> str;
|
| 1676 |
|
| 1677 | void Push(char type, int lineNum)
|
| 1678 | {
|
| 1679 | str.Push(type);
|
| 1680 | str.Push(char('0' + (lineNum / 10)));
|
| 1681 | str.Push(char('0' + (lineNum % 10)));
|
| 1682 | }
|
| 1683 |
|
| 1684 | bool VisitEnter(const XMLDocument& doc)
|
| 1685 | {
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1686 | Push('D', doc.GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1687 | return true;
|
| 1688 | }
|
| 1689 | bool VisitEnter(const XMLElement& element, const XMLAttribute* firstAttribute)
|
| 1690 | {
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1691 | Push('E', element.GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1692 | for (const XMLAttribute *attr = firstAttribute; attr != 0; attr = attr->Next())
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1693 | Push('A', attr->GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1694 | return true;
|
| 1695 | }
|
| 1696 | bool Visit(const XMLDeclaration& declaration)
|
| 1697 | {
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1698 | Push('L', declaration.GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1699 | return true;
|
| 1700 | }
|
| 1701 | bool Visit(const XMLText& text)
|
| 1702 | {
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1703 | Push('T', text.GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1704 | return true;
|
| 1705 | }
|
| 1706 | bool Visit(const XMLComment& comment)
|
| 1707 | {
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1708 | Push('C', comment.GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1709 | return true;
|
| 1710 | }
|
| 1711 | bool Visit(const XMLUnknown& unknown)
|
| 1712 | {
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1713 | Push('U', unknown.GetLineNum());
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1714 | return true;
|
| 1715 | }
|
| 1716 |
|
| 1717 | void TestDocLines(const char *testString, XMLDocument &doc, const char *expectedLines)
|
| 1718 | {
|
| 1719 | str.Clear();
|
| 1720 | doc.Accept(this);
|
| 1721 | str.Push(0);
|
| 1722 | XMLTest(testString, expectedLines, str.Mem());
|
| 1723 | }
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1724 | } tester;
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1725 |
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1726 | tester.TestParseError("ErrorLine-Parsing", "\n<root>\n foo \n<unclosed/>", XML_ERROR_PARSING, 2);
|
| 1727 | tester.TestParseError("ErrorLine-Declaration", "<root>\n<?xml version=\"1.0\"?>", XML_ERROR_PARSING_DECLARATION, 2);
|
| 1728 | tester.TestParseError("ErrorLine-Mismatch", "\n<root>\n</mismatch>", XML_ERROR_MISMATCHED_ELEMENT, 2);
|
| 1729 | tester.TestParseError("ErrorLine-CData", "\n<root><![CDATA[ \n foo bar \n", XML_ERROR_PARSING_CDATA, 2);
|
| 1730 | tester.TestParseError("ErrorLine-Text", "\n<root>\n foo bar \n", XML_ERROR_PARSING_TEXT, 3);
|
| 1731 | tester.TestParseError("ErrorLine-Comment", "\n<root>\n<!-- >\n", XML_ERROR_PARSING_COMMENT, 3);
|
| 1732 | tester.TestParseError("ErrorLine-Declaration", "\n<root>\n<? >\n", XML_ERROR_PARSING_DECLARATION, 3);
|
| 1733 | tester.TestParseError("ErrorLine-Unknown", "\n<root>\n<! \n", XML_ERROR_PARSING_UNKNOWN, 3);
|
| 1734 | tester.TestParseError("ErrorLine-Element", "\n<root>\n<unclosed \n", XML_ERROR_PARSING_ELEMENT, 3);
|
| 1735 | tester.TestParseError("ErrorLine-Attribute", "\n<root>\n<unclosed \n att\n", XML_ERROR_PARSING_ATTRIBUTE, 4);
|
| 1736 | tester.TestParseError("ErrorLine-ElementClose", "\n<root>\n<unclosed \n/unexpected", XML_ERROR_PARSING_ELEMENT, 3);
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1737 |
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1738 | tester.TestStringLines(
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1739 | "LineNumbers-String",
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1740 |
|
| 1741 | "<?xml version=\"1.0\"?>\n" // 1 Doc, DecL
|
| 1742 | "<root a='b' \n" // 2 Element Attribute
|
| 1743 | "c='d'> d <blah/> \n" // 3 Attribute Text Element
|
| 1744 | "newline in text \n" // 4 Text
|
| 1745 | "and second <zxcv/><![CDATA[\n" // 5 Element Text
|
| 1746 | " cdata test ]]><!-- comment -->\n" // 6 Comment
|
| 1747 | "<! unknown></root>", // 7 Unknown
|
| 1748 |
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1749 | "D01L01E02A02A03T03E03T04E05T05C06U07");
|
| 1750 |
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1751 | tester.TestStringLines(
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1752 | "LineNumbers-CRLF",
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1753 |
|
| 1754 | "\r\n" // 1 Doc (arguably should be line 2)
|
| 1755 | "<?xml version=\"1.0\"?>\n" // 2 DecL
|
| 1756 | "<root>\r\n" // 3 Element
|
| 1757 | "\n" // 4
|
| 1758 | "text contining new line \n" // 5 Text
|
| 1759 | " and also containing crlf \r\n" // 6
|
| 1760 | "<sub><![CDATA[\n" // 7 Element Text
|
| 1761 | "cdata containing new line \n" // 8
|
| 1762 | " and also containing cflr\r\n" // 9
|
| 1763 | "]]></sub><sub2/></root>", // 10 Element
|
| 1764 |
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1765 | "D01L02E03T05E07T07E10");
|
| 1766 |
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1767 | tester.TestFileLines(
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1768 | "LineNumbers-File",
|
| 1769 | "resources/utf8test.xml",
|
| 1770 | "D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");
|
| 1771 | }
|
| 1772 |
|
Lee Thomason | 8549202 | 2015-05-22 11:07:45 -0700 | [diff] [blame] | 1773 | // ----------- Performance tracking --------------
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1774 | {
|
| 1775 | #if defined( _MSC_VER )
|
| 1776 | __int64 start, end, freq;
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1777 | QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1778 | #endif
|
| 1779 |
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1780 | FILE* perfFP = fopen("resources/dream.xml", "r");
|
| 1781 | fseek(perfFP, 0, SEEK_END);
|
Armagetron | 3c21d6f | 2016-10-13 13:31:23 +0200 | [diff] [blame] | 1782 | long size = ftell(perfFP);
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1783 | fseek(perfFP, 0, SEEK_SET);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1784 |
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1785 | char* mem = new char[size + 1];
|
| 1786 | fread(mem, size, 1, perfFP);
|
| 1787 | fclose(perfFP);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1788 | mem[size] = 0;
|
| 1789 |
|
| 1790 | #if defined( _MSC_VER )
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1791 | QueryPerformanceCounter((LARGE_INTEGER*)&start);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1792 | #else
|
| 1793 | clock_t cstart = clock();
|
| 1794 | #endif
|
| 1795 | static const int COUNT = 10;
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1796 | for (int i = 0; i < COUNT; ++i) {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1797 | XMLDocument doc;
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1798 | doc.Parse(mem);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1799 | }
|
| 1800 | #if defined( _MSC_VER )
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1801 | QueryPerformanceCounter((LARGE_INTEGER*)&end);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1802 | #else
|
| 1803 | clock_t cend = clock();
|
| 1804 | #endif
|
| 1805 |
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1806 | delete[] mem;
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1807 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1808 | static const char* note =
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1809 | #ifdef DEBUG
|
| 1810 | "DEBUG";
|
| 1811 | #else
|
| 1812 | "Release";
|
| 1813 | #endif
|
| 1814 |
|
| 1815 | #if defined( _MSC_VER )
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1816 | printf("\nParsing %s of dream.xml: %.3f milli-seconds\n", note, 1000.0 * (double)(end - start) / ((double)freq * (double)COUNT));
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1817 | #else
|
Lee Thomason | df4ffc0 | 2016-06-04 11:32:46 -0700 | [diff] [blame] | 1818 | printf("\nParsing %s of dream.xml: %.3f milli-seconds\n", note, (double)(cend - cstart) / (double)COUNT);
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1819 | #endif
|
| 1820 | }
|
| 1821 |
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame] | 1822 | #if defined( _MSC_VER ) && defined( DEBUG )
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1823 | _CrtMemCheckpoint( &endMemState );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1824 |
|
| 1825 | _CrtMemState diffMemState;
|
| 1826 | _CrtMemDifference( &diffMemState, &startMemState, &endMemState );
|
| 1827 | _CrtMemDumpStatistics( &diffMemState );
|
| 1828 | #endif
|
| 1829 |
|
| 1830 | printf ("\nPass %d, Fail %d\n", gPass, gFail);
|
Lee Thomason (grinliz) | db30425 | 2013-07-31 12:24:52 -0700 | [diff] [blame] | 1831 |
|
| 1832 | return gFail;
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1833 | }
|