]> git.saurik.com Git - wxWidgets.git/blame - tests/xml/xmltest.cpp
Correct wxConvAuto::ToWChar() behaviour with wxNO_LEN input size.
[wxWidgets.git] / tests / xml / xmltest.cpp
CommitLineData
5e05df3c
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/xml/xmltest.cpp
3// Purpose: XML classes unit test
4// Author: Vaclav Slavik
5// Created: 2008-03-29
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Vaclav Slavik
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif // WX_PRECOMP
23
24#include "wx/xml/xml.h"
4c493e0b 25#include "wx/sstream.h"
5e05df3c
VS
26
27#include <stdarg.h>
28
29// ----------------------------------------------------------------------------
30// helpers for testing XML tree
31// ----------------------------------------------------------------------------
32
33namespace
34{
35
36void CheckXml(wxXmlNode *n, ...)
37{
38 va_list args;
39 va_start(args, n);
40
41 wxXmlNode *child = n->GetChildren();
42
43 for (;;)
44 {
45 const char *childName = va_arg(args, char*);
46 if ( childName == NULL )
47 break;
48
49 CPPUNIT_ASSERT( child );
1de532f5 50 CPPUNIT_ASSERT_EQUAL( childName, child->GetName() );
5e05df3c
VS
51 CPPUNIT_ASSERT( child->GetChildren() == NULL );
52 CPPUNIT_ASSERT( child->GetParent() == n );
53
54 child = child->GetNext();
55 }
56
57 va_end(args);
58
59 CPPUNIT_ASSERT( child == NULL ); // no more children
60}
61
62} // anon namespace
63
64// ----------------------------------------------------------------------------
65// test class
66// ----------------------------------------------------------------------------
67
68class XmlTestCase : public CppUnit::TestCase
69{
70public:
71 XmlTestCase() {}
72
73private:
74 CPPUNIT_TEST_SUITE( XmlTestCase );
75 CPPUNIT_TEST( InsertChild );
43a302f2 76 CPPUNIT_TEST( InsertChildAfter );
4c493e0b 77 CPPUNIT_TEST( LoadSave );
2388960a 78 CPPUNIT_TEST( CDATA );
926649a9 79 CPPUNIT_TEST( Escaping );
5e05df3c
VS
80 CPPUNIT_TEST_SUITE_END();
81
82 void InsertChild();
43a302f2 83 void InsertChildAfter();
4c493e0b 84 void LoadSave();
2388960a 85 void CDATA();
926649a9 86 void Escaping();
5e05df3c
VS
87
88 DECLARE_NO_COPY_CLASS(XmlTestCase)
89};
90
91// register in the unnamed registry so that these tests are run by default
92CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase );
93
94// also include in it's own registry so that these tests can be run alone
95CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase, "XmlTestCase" );
96
97void XmlTestCase::InsertChild()
98{
99 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root");
100 root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "1"));
101 wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2");
102 root->AddChild(two);
103 root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "3"));
104 CheckXml(root, "1", "2", "3", NULL);
105
106 // check inserting in front:
107 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "A"), NULL);
108 CheckXml(root, "A", "1", "2", "3", NULL);
109 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "B"), root->GetChildren());
110 CheckXml(root, "B", "A", "1", "2", "3", NULL);
111
112 // and in the middle:
113 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), two);
114 CheckXml(root, "B", "A", "1", "C", "2", "3", NULL);
115}
43a302f2
VS
116
117void XmlTestCase::InsertChildAfter()
118{
119 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root");
120
121 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "1"), NULL);
122 CheckXml(root, "1", NULL);
123
124 wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2");
125 root->AddChild(two);
126 wxXmlNode *three = new wxXmlNode(wxXML_ELEMENT_NODE, "3");
127 root->AddChild(three);
128 CheckXml(root, "1", "2", "3", NULL);
129
130 // check inserting in the middle:
131 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "A"), root->GetChildren());
132 CheckXml(root, "1", "A", "2", "3", NULL);
133 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "B"), two);
134 CheckXml(root, "1", "A", "2", "B", "3", NULL);
135
136 // and at the end:
137 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), three);
138 CheckXml(root, "1", "A", "2", "B", "3", "C", NULL);
139}
4c493e0b
VZ
140
141void XmlTestCase::LoadSave()
142{
143 // NB: this is not real XRC but rather some XRC-like XML fragment which
144 // exercises different XML constructs to check that they're saved back
145 // correctly
146 //
147 // Also note that there should be no blank lines here as they disappear
148 // after saving.
149 const char *xmlText =
150"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
151"<resource xmlns=\"http://www.wxwidgets.org/wxxrc\" version=\"2.3.0.1\">\n"
152" <object class=\"wxDialog\" name=\"my_dialog\">\n"
153" <children>\n"
154" <grandchild id=\"1\"/>\n"
155" </children>\n"
156" <subobject/>\n"
157" </object>\n"
158"</resource>\n"
159 ;
160
161 wxStringInputStream sis(xmlText);
162
163 wxXmlDocument doc;
164 CPPUNIT_ASSERT( doc.Load(sis) );
165
166 wxStringOutputStream sos;
167 CPPUNIT_ASSERT( doc.Save(sos) );
168
169 CPPUNIT_ASSERT_EQUAL( xmlText, sos.GetString() );
e767076e
VZ
170
171
172 const char *utf8xmlText =
173"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
174"<word>\n"
175" <lang name=\"fr\">\xc3\xa9t\xc3\xa9</lang>\n"
176" <lang name=\"ru\">\xd0\xbb\xd0\xb5\xd1\x82\xd0\xbe</lang>\n"
177"</word>\n"
178 ;
179
180 wxStringInputStream sis8(wxString::FromUTF8(utf8xmlText));
181 CPPUNIT_ASSERT( doc.Load(sis8) );
182
183 // this contents can't be represented in Latin-1 as it contains Cyrillic
184 // letters
185 doc.SetFileEncoding("ISO-8859-1");
186 CPPUNIT_ASSERT( !doc.Save(sos) );
187
188 // but it should work in UTF-8
189 wxStringOutputStream sos8;
190 doc.SetFileEncoding("UTF-8");
191 CPPUNIT_ASSERT( doc.Save(sos8) );
a0871132
VZ
192 CPPUNIT_ASSERT_EQUAL( wxString(utf8xmlText),
193 wxString(sos8.GetString().ToUTF8()) );
4c493e0b
VZ
194}
195
2388960a
VZ
196void XmlTestCase::CDATA()
197{
198 const char *xmlText =
199 "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"
200 "<name>\n"
201 " <![CDATA[Giovanni Mittone]]>\n"
202 "</name>\n"
203 ;
204
205 wxStringInputStream sis(xmlText);
206 wxXmlDocument doc;
207 CPPUNIT_ASSERT( doc.Load(sis) );
208
209 wxXmlNode *n = doc.GetRoot();
210 CPPUNIT_ASSERT( n );
211
212 n = n->GetChildren();
213 CPPUNIT_ASSERT( n );
214
a6cf6bcf
VZ
215 // check that both leading (" ") and trailing white space is not part of
216 // the node contents when CDATA is used and wxXMLDOC_KEEP_WHITESPACE_NODES
217 // is not
218 CPPUNIT_ASSERT_EQUAL( "Giovanni Mittone", n->GetContent() );
2388960a 219}
926649a9
VS
220
221void XmlTestCase::Escaping()
222{
223 // Verify that attribute values are escaped correctly, see
224 // http://trac.wxwidgets.org/ticket/12275
225
226 const char *xmlText =
227"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
228"<root text=\"hello&#xD;&#xA;this is a new line\">\n"
229" <x/>\n"
230"</root>\n"
231 ;
232
233 wxStringInputStream sis(xmlText);
234
235 wxXmlDocument doc;
236 CPPUNIT_ASSERT( doc.Load(sis) );
237
238 wxStringOutputStream sos;
239 CPPUNIT_ASSERT( doc.Save(sos) );
240
241 CPPUNIT_ASSERT_EQUAL( xmlText, sos.GetString() );
242}