]> git.saurik.com Git - wxWidgets.git/blob - tests/xml/xmltest.cpp
Added wxAny::HasSameType()
[wxWidgets.git] / tests / xml / xmltest.cpp
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"
25 #include "wx/sstream.h"
26
27 #include <stdarg.h>
28
29 // ----------------------------------------------------------------------------
30 // helpers for testing XML tree
31 // ----------------------------------------------------------------------------
32
33 namespace
34 {
35
36 void 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 );
50 CPPUNIT_ASSERT_EQUAL( childName, child->GetName() );
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
68 class XmlTestCase : public CppUnit::TestCase
69 {
70 public:
71 XmlTestCase() {}
72
73 private:
74 CPPUNIT_TEST_SUITE( XmlTestCase );
75 CPPUNIT_TEST( InsertChild );
76 CPPUNIT_TEST( InsertChildAfter );
77 CPPUNIT_TEST( LoadSave );
78 CPPUNIT_TEST( CDATA );
79 CPPUNIT_TEST_SUITE_END();
80
81 void InsertChild();
82 void InsertChildAfter();
83 void LoadSave();
84 void CDATA();
85
86 DECLARE_NO_COPY_CLASS(XmlTestCase)
87 };
88
89 // register in the unnamed registry so that these tests are run by default
90 CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase );
91
92 // also include in it's own registry so that these tests can be run alone
93 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase, "XmlTestCase" );
94
95 void XmlTestCase::InsertChild()
96 {
97 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root");
98 root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "1"));
99 wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2");
100 root->AddChild(two);
101 root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "3"));
102 CheckXml(root, "1", "2", "3", NULL);
103
104 // check inserting in front:
105 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "A"), NULL);
106 CheckXml(root, "A", "1", "2", "3", NULL);
107 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "B"), root->GetChildren());
108 CheckXml(root, "B", "A", "1", "2", "3", NULL);
109
110 // and in the middle:
111 root->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), two);
112 CheckXml(root, "B", "A", "1", "C", "2", "3", NULL);
113 }
114
115 void XmlTestCase::InsertChildAfter()
116 {
117 wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root");
118
119 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "1"), NULL);
120 CheckXml(root, "1", NULL);
121
122 wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2");
123 root->AddChild(two);
124 wxXmlNode *three = new wxXmlNode(wxXML_ELEMENT_NODE, "3");
125 root->AddChild(three);
126 CheckXml(root, "1", "2", "3", NULL);
127
128 // check inserting in the middle:
129 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "A"), root->GetChildren());
130 CheckXml(root, "1", "A", "2", "3", NULL);
131 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "B"), two);
132 CheckXml(root, "1", "A", "2", "B", "3", NULL);
133
134 // and at the end:
135 root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), three);
136 CheckXml(root, "1", "A", "2", "B", "3", "C", NULL);
137 }
138
139 void XmlTestCase::LoadSave()
140 {
141 // NB: this is not real XRC but rather some XRC-like XML fragment which
142 // exercises different XML constructs to check that they're saved back
143 // correctly
144 //
145 // Also note that there should be no blank lines here as they disappear
146 // after saving.
147 const char *xmlText =
148 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
149 "<resource xmlns=\"http://www.wxwidgets.org/wxxrc\" version=\"2.3.0.1\">\n"
150 " <object class=\"wxDialog\" name=\"my_dialog\">\n"
151 " <children>\n"
152 " <grandchild id=\"1\"/>\n"
153 " </children>\n"
154 " <subobject/>\n"
155 " </object>\n"
156 "</resource>\n"
157 ;
158
159 wxStringInputStream sis(xmlText);
160
161 wxXmlDocument doc;
162 CPPUNIT_ASSERT( doc.Load(sis) );
163
164 wxStringOutputStream sos;
165 CPPUNIT_ASSERT( doc.Save(sos) );
166
167 CPPUNIT_ASSERT_EQUAL( xmlText, sos.GetString() );
168
169
170 const char *utf8xmlText =
171 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
172 "<word>\n"
173 " <lang name=\"fr\">\xc3\xa9t\xc3\xa9</lang>\n"
174 " <lang name=\"ru\">\xd0\xbb\xd0\xb5\xd1\x82\xd0\xbe</lang>\n"
175 "</word>\n"
176 ;
177
178 wxStringInputStream sis8(wxString::FromUTF8(utf8xmlText));
179 CPPUNIT_ASSERT( doc.Load(sis8) );
180
181 // this contents can't be represented in Latin-1 as it contains Cyrillic
182 // letters
183 doc.SetFileEncoding("ISO-8859-1");
184 CPPUNIT_ASSERT( !doc.Save(sos) );
185
186 // but it should work in UTF-8
187 wxStringOutputStream sos8;
188 doc.SetFileEncoding("UTF-8");
189 CPPUNIT_ASSERT( doc.Save(sos8) );
190 CPPUNIT_ASSERT_EQUAL( wxString(utf8xmlText),
191 wxString(sos8.GetString().ToUTF8()) );
192 }
193
194 void XmlTestCase::CDATA()
195 {
196 const char *xmlText =
197 "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"
198 "<name>\n"
199 " <![CDATA[Giovanni Mittone]]>\n"
200 "</name>\n"
201 ;
202
203 wxStringInputStream sis(xmlText);
204 wxXmlDocument doc;
205 CPPUNIT_ASSERT( doc.Load(sis) );
206
207 wxXmlNode *n = doc.GetRoot();
208 CPPUNIT_ASSERT( n );
209
210 n = n->GetChildren();
211 CPPUNIT_ASSERT( n );
212
213 // check that both leading (" ") and trailing white space is not part of
214 // the node contents when CDATA is used and wxXMLDOC_KEEP_WHITESPACE_NODES
215 // is not
216 CPPUNIT_ASSERT_EQUAL( "Giovanni Mittone", n->GetContent() );
217 }