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_SUITE_END();
81 void InsertChildAfter();
84 DECLARE_NO_COPY_CLASS(XmlTestCase
)
87 // register in the unnamed registry so that these tests are run by default
88 CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase
);
90 // also include in it's own registry so that these tests can be run alone
91 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase
, "XmlTestCase" );
93 void XmlTestCase::InsertChild()
95 wxXmlNode
*root
= new wxXmlNode(wxXML_ELEMENT_NODE
, "root");
96 root
->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "1"));
97 wxXmlNode
*two
= new wxXmlNode(wxXML_ELEMENT_NODE
, "2");
99 root
->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "3"));
100 CheckXml(root
, "1", "2", "3", NULL
);
102 // check inserting in front:
103 root
->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "A"), NULL
);
104 CheckXml(root
, "A", "1", "2", "3", NULL
);
105 root
->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "B"), root
->GetChildren());
106 CheckXml(root
, "B", "A", "1", "2", "3", NULL
);
108 // and in the middle:
109 root
->InsertChild(new wxXmlNode(wxXML_ELEMENT_NODE
, "C"), two
);
110 CheckXml(root
, "B", "A", "1", "C", "2", "3", NULL
);
113 void XmlTestCase::InsertChildAfter()
115 wxXmlNode
*root
= new wxXmlNode(wxXML_ELEMENT_NODE
, "root");
117 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "1"), NULL
);
118 CheckXml(root
, "1", NULL
);
120 wxXmlNode
*two
= new wxXmlNode(wxXML_ELEMENT_NODE
, "2");
122 wxXmlNode
*three
= new wxXmlNode(wxXML_ELEMENT_NODE
, "3");
123 root
->AddChild(three
);
124 CheckXml(root
, "1", "2", "3", NULL
);
126 // check inserting in the middle:
127 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "A"), root
->GetChildren());
128 CheckXml(root
, "1", "A", "2", "3", NULL
);
129 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "B"), two
);
130 CheckXml(root
, "1", "A", "2", "B", "3", NULL
);
133 root
->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE
, "C"), three
);
134 CheckXml(root
, "1", "A", "2", "B", "3", "C", NULL
);
137 void XmlTestCase::LoadSave()
139 // NB: this is not real XRC but rather some XRC-like XML fragment which
140 // exercises different XML constructs to check that they're saved back
143 // Also note that there should be no blank lines here as they disappear
145 const char *xmlText
=
146 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
147 "<resource xmlns=\"http://www.wxwidgets.org/wxxrc\" version=\"2.3.0.1\">\n"
148 " <object class=\"wxDialog\" name=\"my_dialog\">\n"
150 " <grandchild id=\"1\"/>\n"
157 wxStringInputStream
sis(xmlText
);
160 CPPUNIT_ASSERT( doc
.Load(sis
) );
162 wxStringOutputStream sos
;
163 CPPUNIT_ASSERT( doc
.Save(sos
) );
165 CPPUNIT_ASSERT_EQUAL( xmlText
, sos
.GetString() );
168 const char *utf8xmlText
=
169 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
171 " <lang name=\"fr\">\xc3\xa9t\xc3\xa9</lang>\n"
172 " <lang name=\"ru\">\xd0\xbb\xd0\xb5\xd1\x82\xd0\xbe</lang>\n"
176 wxStringInputStream
sis8(wxString::FromUTF8(utf8xmlText
));
177 CPPUNIT_ASSERT( doc
.Load(sis8
) );
179 // this contents can't be represented in Latin-1 as it contains Cyrillic
181 doc
.SetFileEncoding("ISO-8859-1");
182 CPPUNIT_ASSERT( !doc
.Save(sos
) );
184 // but it should work in UTF-8
185 wxStringOutputStream sos8
;
186 doc
.SetFileEncoding("UTF-8");
187 CPPUNIT_ASSERT( doc
.Save(sos8
) );
188 CPPUNIT_ASSERT_EQUAL( wxString(utf8xmlText
),
189 wxString(sos8
.GetString().ToUTF8()) );