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