aboutsummaryrefslogtreecommitdiff
path: root/tests/testXML.cpp
blob: dfab9b43472aca57327f72d7ef4266cb34e71893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (C) 2009-2011 Christian Stehno
// No rights reserved: this software is in the public domain.

#include "testUtils.h"

using namespace irr;
using namespace core;

bool simple_xml( irr::io::IFileSystem * fs )
{
	io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/test.xml");
	if (!reader)
	{
		logTestString("Could not create XML reader.\n");
		return false;
	}

	const core::stringc expected[] = {
		"a", "b", "c"
	};

	bool retVal = true;
	u32 i=0;
	while(reader->read())
	{
		if (reader->getNodeType() == io::EXN_ELEMENT)
		{
			if (expected[i++] != reader->getNodeName())
			{
				logTestString("Did not find expected string in XML element name.\n");
				retVal = false;
				break;
			}
		}
	}

	reader->drop();
	return retVal;
}

// CDATA should return everything between "![CDATA[" and "]]>" as it's in the file
bool cdata( irr::io::IFileSystem * fs )
{
	io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/cdata.xml");
	if (!reader)
	{
		logTestString("Could not create XML reader.\n");
		return false;
	}

	const core::stringc textNode("text");
	core::array< core::stringc > compareStrings;
	compareStrings.push_back("simple");
	compareStrings.push_back("");
	compareStrings.push_back("] ]> ");
	compareStrings.push_back("]\n]> ");
	compareStrings.push_back("\nNewlines\n\tand tabs\n\t\tgogogo");
	compareStrings.push_back("&&#@#$%*()@#$%*()#$%*(");
	compareStrings.push_back("& &  && &&& &a &ü &ä &ö &&#");

	bool result = true;
	size_t count = 0;
	while(reader->read())
	{
		if (reader->getNodeType() == io::EXN_ELEMENT)
		{
			if ( core::stringc(reader->getNodeName()) == textNode )
			{
				while(reader->read())
				{
					if (reader->getNodeType() == io::EXN_CDATA)
					{
						core::stringc data = reader->getNodeData();
						core::stringc name = reader->getNodeName();
						if ( count == compareStrings.size() )
						{
							logTestString("too many cdata elements for reading in %s:%d\n", __FILE__, __LINE__);
						}
						else if ( count < compareStrings.size() )
						{
							core::stringc cmpString(compareStrings[count]);
							
							// some (unused) variables to ease debugging
							// const c8* dataRaw = data.c_str();
							// const c8* cmpRaw = cmpString.c_str();
							if ( cmpString != data )
							{
								result = false;
								logTestString("cdata read failed for string %d in %s:%d\n", count, __FILE__, __LINE__);
							}
						}
						++count;
					}
					if ( reader->getNodeType() == io::EXN_ELEMENT_END )
					{
						break;
					}
				}
			}
		}
	}

	reader->drop();
	return result;
}

bool attributeValues(irr::io::IFileSystem * fs)
{
	io::IXMLReaderUTF8* reader = fs->createXMLReaderUTF8("media/attributes.xml");
	if (!reader)
	{
		logTestString("Could not create XML reader.\n");
		return false;
	}

	bool result = true;
	bool hasNode = false;
	while (reader->read())
	{
		if (io::EXN_ELEMENT == reader->getNodeType() )
		{
			if ( core::stringc(reader->getNodeName()) == core::stringc("element_position") )
			{
				hasNode = true;
				int id1 = reader->getAttributeValueAsInt("id1");
				if ( id1 != 152722522 )
				{
					logTestString("id1 is %d in %s:%d\n", id1, __FILE__, __LINE__);
					result = false;
				}
				int id2 = reader->getAttributeValueAsInt("id2");
				result &= id2 == 3;
				int x = reader->getAttributeValueAsInt("x");
				result &= x == 301;
				int y = reader->getAttributeValueAsInt("y");
				result &= y == 118;
			}
		}
	}

	if ( !hasNode )
	{
		logTestString("missing node in xml in %s:%d\n", __FILE__, __LINE__);
		return false;
	}

	reader->drop();
	return result;
}

/** Tests for XML handling */
bool testXML(void)
{
	IrrlichtDevice *device = createDevice(video::EDT_NULL, dimension2du(400, 200));

	bool result = true;

	logTestString("Test simple XML reader features.\n");
	result &= simple_xml(device->getFileSystem());
	logTestString("Test XML reader CDATA support.\n");
	result &= cdata(device->getFileSystem());
	logTestString("Test XML reader attribute support.\n");
	result &= attributeValues(device->getFileSystem());	

	device->closeDevice();
	device->run();
	device->drop();
	return result;
}