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/scopedptr.h"
26 #include "wx/sstream.h"
30 // ----------------------------------------------------------------------------
31 // helpers for testing XML tree
32 // ----------------------------------------------------------------------------
37 void CheckXml(const wxScopedPtr
<wxXmlNode
>& n
, ...)
42 wxXmlNode
*child
= n
->GetChildren();
46 const char *childName
= va_arg(args
, char*);
47 if ( childName
== NULL
)
50 CPPUNIT_ASSERT( child
);
51 CPPUNIT_ASSERT_EQUAL( childName
, child
->GetName() );
52 CPPUNIT_ASSERT( child
->GetChildren() == NULL
);
53 CPPUNIT_ASSERT( child
->GetParent() == n
.get() );
55 child
= child
->GetNext();
60 CPPUNIT_ASSERT( child
== NULL
); // no more children
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 class XmlTestCase
: public CppUnit
::TestCase
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();
84 void InsertChildAfter();
89 DECLARE_NO_COPY_CLASS(XmlTestCase
)
92 // register in the unnamed registry so that these tests are run by default
93 CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase
);
95 // also include in it's own registry so that these tests can be run alone
96 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase
, "XmlTestCase" );
98 void XmlTestCase
::InsertChild()
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");
104 root
->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "3"));
105 CheckXml(root
, "1", "2", "3", NULL
);
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
);
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
);
118 void XmlTestCase
::InsertChildAfter()
120 wxScopedPtr
<wxXmlNode
> root(new wxXmlNode(wxXML_ELEMENT_NODE
, "root"));
122 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "1"), NULL
);
123 CheckXml(root
, "1", NULL
);
125 wxXmlNode
*two
= new wxXmlNode(wxXML_ELEMENT_NODE
, "2");
127 wxXmlNode
*three
= new wxXmlNode(wxXML_ELEMENT_NODE
, "3");
128 root
->AddChild(three
);
129 CheckXml(root
, "1", "2", "3", NULL
);
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
);
138 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "C"), three
);
139 CheckXml(root
, "1", "A", "2", "B", "3", "C", NULL
);
142 void XmlTestCase
::LoadSave()
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
148 // Also note that there should be no blank lines here as they disappear
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"
155 " <grandchild id=\"1\"/>\n"
162 wxStringInputStream
sis(xmlText
);
165 CPPUNIT_ASSERT( doc
.Load(sis
) );
167 wxStringOutputStream sos
;
168 CPPUNIT_ASSERT( doc
.Save(sos
) );
170 CPPUNIT_ASSERT_EQUAL( xmlText
, sos
.GetString() );
174 const char *utf8xmlText
=
175 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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"
182 wxStringInputStream
sis8(wxString
::FromUTF8(utf8xmlText
));
183 CPPUNIT_ASSERT( doc
.Load(sis8
) );
185 // this contents can't be represented in Latin-1 as it contains Cyrillic
187 doc
.SetFileEncoding("ISO-8859-1");
188 CPPUNIT_ASSERT( !doc
.Save(sos
) );
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
199 void XmlTestCase
::CDATA()
201 const char *xmlText
=
202 "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"
204 " <![CDATA[Giovanni Mittone]]>\n"
208 wxStringInputStream
sis(xmlText
);
210 CPPUNIT_ASSERT( doc
.Load(sis
) );
212 wxXmlNode
*n
= doc
.GetRoot();
215 n
= n
->GetChildren();
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
221 CPPUNIT_ASSERT_EQUAL( "Giovanni Mittone", n
->GetContent() );
224 void XmlTestCase
::Escaping()
226 // Verify that attribute values are escaped correctly, see
227 // http://trac.wxwidgets.org/ticket/12275
229 const char *xmlText
=
230 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
231 "<root text=\"hello
this is a new line\">\n"
236 wxStringInputStream
sis(xmlText
);
239 CPPUNIT_ASSERT( doc
.Load(sis
) );
241 wxStringOutputStream sos
;
242 CPPUNIT_ASSERT( doc
.Save(sos
) );
244 CPPUNIT_ASSERT_EQUAL( xmlText
, sos
.GetString() );