1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/xml/xmltest.cpp
3 // Purpose: XML classes unit test
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2008 Vaclav Slavik
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/xml/xml.h"
25 #include "wx/sstream.h"
29 // ----------------------------------------------------------------------------
30 // helpers for testing XML tree
31 // ----------------------------------------------------------------------------
36 void CheckXml(wxXmlNode
*n
, ...)
41 wxXmlNode
*child
= n
->GetChildren();
45 const char *childName
= va_arg(args
, char*);
46 if ( childName
== NULL
)
49 CPPUNIT_ASSERT( child
);
50 CPPUNIT_ASSERT_EQUAL( childName
, child
->GetName() );
51 CPPUNIT_ASSERT( child
->GetChildren() == NULL
);
52 CPPUNIT_ASSERT( child
->GetParent() == n
);
54 child
= child
->GetNext();
59 CPPUNIT_ASSERT( child
== NULL
); // no more children
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 class XmlTestCase
: public CppUnit::TestCase
74 CPPUNIT_TEST_SUITE( XmlTestCase
);
75 CPPUNIT_TEST( InsertChild
);
76 CPPUNIT_TEST( InsertChildAfter
);
77 CPPUNIT_TEST( LoadSave
);
78 CPPUNIT_TEST( CDATA
);
79 CPPUNIT_TEST( Escaping
);
80 CPPUNIT_TEST_SUITE_END();
83 void InsertChildAfter();
88 DECLARE_NO_COPY_CLASS(XmlTestCase
)
91 // register in the unnamed registry so that these tests are run by default
92 CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase
);
94 // also include in it's own registry so that these tests can be run alone
95 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase
, "XmlTestCase" );
97 void XmlTestCase::InsertChild()
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");
103 root
->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "3"));
104 CheckXml(root
, "1", "2", "3", NULL
);
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
);
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
);
117 void XmlTestCase::InsertChildAfter()
119 wxXmlNode
*root
= new wxXmlNode(wxXML_ELEMENT_NODE
, "root");
121 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "1"), NULL
);
122 CheckXml(root
, "1", NULL
);
124 wxXmlNode
*two
= new wxXmlNode(wxXML_ELEMENT_NODE
, "2");
126 wxXmlNode
*three
= new wxXmlNode(wxXML_ELEMENT_NODE
, "3");
127 root
->AddChild(three
);
128 CheckXml(root
, "1", "2", "3", NULL
);
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
);
137 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "C"), three
);
138 CheckXml(root
, "1", "A", "2", "B", "3", "C", NULL
);
141 void XmlTestCase::LoadSave()
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
147 // Also note that there should be no blank lines here as they disappear
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"
154 " <grandchild id=\"1\"/>\n"
161 wxStringInputStream
sis(xmlText
);
164 CPPUNIT_ASSERT( doc
.Load(sis
) );
166 wxStringOutputStream sos
;
167 CPPUNIT_ASSERT( doc
.Save(sos
) );
169 CPPUNIT_ASSERT_EQUAL( xmlText
, sos
.GetString() );
173 const char *utf8xmlText
=
174 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
176 " <lang name=\"fr\">\xc3\xa9t\xc3\xa9</lang>\n"
177 " <lang name=\"ru\">\xd0\xbb\xd0\xb5\xd1\x82\xd0\xbe</lang>\n"
181 wxStringInputStream
sis8(wxString::FromUTF8(utf8xmlText
));
182 CPPUNIT_ASSERT( doc
.Load(sis8
) );
184 // this contents can't be represented in Latin-1 as it contains Cyrillic
186 doc
.SetFileEncoding("ISO-8859-1");
187 CPPUNIT_ASSERT( !doc
.Save(sos
) );
189 // but it should work in UTF-8
190 wxStringOutputStream sos8
;
191 doc
.SetFileEncoding("UTF-8");
192 CPPUNIT_ASSERT( doc
.Save(sos8
) );
193 CPPUNIT_ASSERT_EQUAL( wxString(utf8xmlText
),
194 wxString(sos8
.GetString().ToUTF8()) );
195 #endif // wxUSE_UNICODE
198 void XmlTestCase::CDATA()
200 const char *xmlText
=
201 "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"
203 " <![CDATA[Giovanni Mittone]]>\n"
207 wxStringInputStream
sis(xmlText
);
209 CPPUNIT_ASSERT( doc
.Load(sis
) );
211 wxXmlNode
*n
= doc
.GetRoot();
214 n
= n
->GetChildren();
217 // check that both leading (" ") and trailing white space is not part of
218 // the node contents when CDATA is used and wxXMLDOC_KEEP_WHITESPACE_NODES
220 CPPUNIT_ASSERT_EQUAL( "Giovanni Mittone", n
->GetContent() );
223 void XmlTestCase::Escaping()
225 // Verify that attribute values are escaped correctly, see
226 // http://trac.wxwidgets.org/ticket/12275
228 const char *xmlText
=
229 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
230 "<root text=\"hello
this is a new line\">\n"
235 wxStringInputStream
sis(xmlText
);
238 CPPUNIT_ASSERT( doc
.Load(sis
) );
240 wxStringOutputStream sos
;
241 CPPUNIT_ASSERT( doc
.Save(sos
) );
243 CPPUNIT_ASSERT_EQUAL( xmlText
, sos
.GetString() );