]>
Commit | Line | Data |
---|---|---|
5e05df3c VS |
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" | |
4c493e0b | 25 | #include "wx/sstream.h" |
5e05df3c VS |
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 ); | |
1de532f5 | 50 | CPPUNIT_ASSERT_EQUAL( childName, child->GetName() ); |
5e05df3c VS |
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 ); | |
43a302f2 | 76 | CPPUNIT_TEST( InsertChildAfter ); |
4c493e0b | 77 | CPPUNIT_TEST( LoadSave ); |
5e05df3c VS |
78 | CPPUNIT_TEST_SUITE_END(); |
79 | ||
80 | void InsertChild(); | |
43a302f2 | 81 | void InsertChildAfter(); |
4c493e0b | 82 | void LoadSave(); |
5e05df3c VS |
83 | |
84 | DECLARE_NO_COPY_CLASS(XmlTestCase) | |
85 | }; | |
86 | ||
87 | // register in the unnamed registry so that these tests are run by default | |
88 | CPPUNIT_TEST_SUITE_REGISTRATION( XmlTestCase ); | |
89 | ||
90 | // also include in it's own registry so that these tests can be run alone | |
91 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XmlTestCase, "XmlTestCase" ); | |
92 | ||
93 | void XmlTestCase::InsertChild() | |
94 | { | |
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"); | |
98 | root->AddChild(two); | |
99 | root->AddChild(new wxXmlNode(wxXML_ELEMENT_NODE, "3")); | |
100 | CheckXml(root, "1", "2", "3", NULL); | |
101 | ||
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); | |
107 | ||
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); | |
111 | } | |
43a302f2 VS |
112 | |
113 | void XmlTestCase::InsertChildAfter() | |
114 | { | |
115 | wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE, "root"); | |
116 | ||
117 | root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "1"), NULL); | |
118 | CheckXml(root, "1", NULL); | |
119 | ||
120 | wxXmlNode *two = new wxXmlNode(wxXML_ELEMENT_NODE, "2"); | |
121 | root->AddChild(two); | |
122 | wxXmlNode *three = new wxXmlNode(wxXML_ELEMENT_NODE, "3"); | |
123 | root->AddChild(three); | |
124 | CheckXml(root, "1", "2", "3", NULL); | |
125 | ||
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); | |
131 | ||
132 | // and at the end: | |
133 | root->InsertChildAfter(new wxXmlNode(wxXML_ELEMENT_NODE, "C"), three); | |
134 | CheckXml(root, "1", "A", "2", "B", "3", "C", NULL); | |
135 | } | |
4c493e0b VZ |
136 | |
137 | void XmlTestCase::LoadSave() | |
138 | { | |
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 | |
141 | // correctly | |
142 | // | |
143 | // Also note that there should be no blank lines here as they disappear | |
144 | // after saving. | |
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" | |
149 | " <children>\n" | |
150 | " <grandchild id=\"1\"/>\n" | |
151 | " </children>\n" | |
152 | " <subobject/>\n" | |
153 | " </object>\n" | |
154 | "</resource>\n" | |
155 | ; | |
156 | ||
157 | wxStringInputStream sis(xmlText); | |
158 | ||
159 | wxXmlDocument doc; | |
160 | CPPUNIT_ASSERT( doc.Load(sis) ); | |
161 | ||
162 | wxStringOutputStream sos; | |
163 | CPPUNIT_ASSERT( doc.Save(sos) ); | |
164 | ||
165 | CPPUNIT_ASSERT_EQUAL( xmlText, sos.GetString() ); | |
e767076e VZ |
166 | |
167 | ||
168 | const char *utf8xmlText = | |
169 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | |
170 | "<word>\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" | |
173 | "</word>\n" | |
174 | ; | |
175 | ||
176 | wxStringInputStream sis8(wxString::FromUTF8(utf8xmlText)); | |
177 | CPPUNIT_ASSERT( doc.Load(sis8) ); | |
178 | ||
179 | // this contents can't be represented in Latin-1 as it contains Cyrillic | |
180 | // letters | |
181 | doc.SetFileEncoding("ISO-8859-1"); | |
182 | CPPUNIT_ASSERT( !doc.Save(sos) ); | |
183 | ||
184 | // but it should work in UTF-8 | |
185 | wxStringOutputStream sos8; | |
186 | doc.SetFileEncoding("UTF-8"); | |
187 | CPPUNIT_ASSERT( doc.Save(sos8) ); | |
a0871132 VZ |
188 | CPPUNIT_ASSERT_EQUAL( wxString(utf8xmlText), |
189 | wxString(sos8.GetString().ToUTF8()) ); | |
4c493e0b VZ |
190 | } |
191 |