File: UnitTests_8cpp-example.html

package info (click to toggle)
sp-gxmlcpp 1.0.20040603-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,596 kB
  • ctags: 266
  • sloc: sh: 7,483; cpp: 1,629; makefile: 119
file content (507 lines) | stat: -rw-r--r-- 35,834 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Example Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="examples.html">Examples</a> &nbsp; </center>
<hr><h1>UnitTests.cpp</h1>
<p>
CPPUnit Tests for this library. Binary should be installed as sp-gxmlcpp-test.
<p>
<div class="fragment"><pre><font class="comment">/*</font>
<font class="comment"> * (C) Copyright 2002, Schlund+Partner AG</font>
<font class="comment"> *</font>
<font class="comment"> * This library is free software; you can redistribute it and/or</font>
<font class="comment"> * modify it under the terms of the GNU Lesser General Public</font>
<font class="comment"> * License as published by the Free Software Foundation; either</font>
<font class="comment"> * version 2.1 of the License, or (at your option) any later version.</font>
<font class="comment"> *</font>
<font class="comment"> * This library is distributed in the hope that it will be useful,</font>
<font class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
<font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</font>
<font class="comment"> * Lesser General Public License for more details.</font>
<font class="comment"> *</font>
<font class="comment"> * You should have received a copy of the GNU Lesser General Public</font>
<font class="comment"> * License along with this library; if not, write to the Free Software</font>
<font class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</font>
<font class="comment">*/</font>

<font class="comment">// Local configuration</font>
<font class="preprocessor">#include "config.h"</font>

<font class="comment">// C++ Libraries</font>
<font class="preprocessor">#include &lt;cppunit/TestCase.h&gt;</font>
<font class="preprocessor">#include &lt;cppunit/TestSuite.h&gt;</font>
<font class="preprocessor">#include &lt;cppunit/TestCaller.h&gt;</font>
<font class="preprocessor">#include &lt;cppunit/TextTestRunner.h&gt;</font>

<font class="preprocessor">#include &lt;<a class="code" href="XMLTree_8hpp.html">sp-gxmlcpp/XMLTree.hpp</a>&gt;</font>
<font class="preprocessor">#include &lt;<a class="code" href="XMLDump_8hpp.html">sp-gxmlcpp/XMLDump.hpp</a>&gt;</font>
<font class="preprocessor">#include &lt;<a class="code" href="XSLTrans_8hpp.html">sp-gxmlcpp/XSLTrans.hpp</a>&gt;</font>
<font class="preprocessor">#include &lt;<a class="code" href="XMLNode_8hpp.html">sp-gxmlcpp/XMLNode.hpp</a>&gt;</font>
<font class="preprocessor">#include &lt;<a class="code" href="XMLNodeSet_8hpp.html">sp-gxmlcpp/XMLNodeSet.hpp</a>&gt;</font>
<font class="preprocessor">#include &lt;<a class="code" href="XMLException_8hpp.html">sp-gxmlcpp/XMLException.hpp</a>&gt;</font>

<font class="keyword">namespace </font>SP {
<font class="keyword">namespace </font>GXML {

<font class="comment">//</font>
<font class="comment">// XMLTree (&amp;&amp; XMLDump)</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLTreeOld: <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
        TestXMLTreeOld()
        {}
        <font class="keywordtype">void</font> testRun()
        {
                <font class="comment">// ^^ Note: dump will always use " (not ') to quote, so we must do this here too for string compare</font>
                <font class="keyword">const</font> std::string xmlString(
                        <font class="stringliteral">"&lt;?xml version=\"1.0\"?&gt;\n"</font>
                        <font class="stringliteral">"&lt;req&gt;\n"</font>
                        <font class="stringliteral">" &lt;uid&gt;5&lt;/uid&gt;\n"</font>
                        <font class="stringliteral">" &lt;home&gt;/home/absurd&lt;/home&gt;\n"</font>
                        <font class="stringliteral">" &lt;key&gt;AAAABBBBCCCCDDDD&lt;/key&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">" &lt;a&gt;\n"</font>
                        <font class="stringliteral">"   &lt;b&gt;\n"</font>
                        <font class="stringliteral">"     &lt;c&gt;Wert1&lt;/c&gt;\n"</font>
                        <font class="stringliteral">"   &lt;/b&gt;\n"</font>
                        <font class="stringliteral">"   &lt;b&gt;\n"</font>
                        <font class="stringliteral">"     &lt;c&gt; Wert 2&lt;/c&gt;\n"</font>
                        <font class="stringliteral">"  &lt;/b&gt;\n"</font>
                        <font class="stringliteral">" &lt;/a&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">" &lt;X&gt;5&lt;/X&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">" &lt;int&gt;-42&lt;/int&gt;\n"</font>
                        <font class="stringliteral">" &lt;int&gt;17&lt;/int&gt;\n"</font>
                        <font class="stringliteral">" &lt;bool&gt;0&lt;/bool&gt;\n"</font>
                        <font class="stringliteral">" &lt;string&gt; abc def &lt;/string&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">" &lt;X&gt;13&lt;/X&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">"&lt;tags&gt;\n"</font>
                        <font class="stringliteral">" &lt;subtag&gt;EINS&lt;/subtag&gt;\n"</font>
                        <font class="stringliteral">"&lt;/tags&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">" &lt;tags&gt;\n"</font>
                        <font class="stringliteral">"  &lt;subtag&gt;ZWEI&lt;/subtag&gt;\n"</font>
                        <font class="stringliteral">" &lt;/tags&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">" &lt;X&gt;17&lt;/X&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">"&lt;/req&gt;\n"</font>);

                <font class="comment">// ^^ Note: dump will always add a \n to the last line (in a full ("/") dump, that is), so our string should have this too for string compare</font>
                <font class="comment">// Note2: It's a bit dumb to literally compare dumps here, as the default way of producing dumps might change... STS</font>

                <font class="comment">// Partial Tree &amp;&amp; Dump</font>
                XMLTree xmlTree1(xmlString.c_str(), xmlString.size());
                std::string dump1 = XMLDump(&amp;xmlTree1, <font class="stringliteral">"/req/tags"</font>).get();
                CPPUNIT_ASSERT(dump1 ==
                                                                         <font class="stringliteral">"&lt;tags&gt;\n"</font>
                                                                         <font class="stringliteral">" &lt;subtag&gt;EINS&lt;/subtag&gt;\n"</font>
                                                                         <font class="stringliteral">"&lt;/tags&gt;"</font>);

                <font class="comment">// Full Tree &amp;&amp; Dump</font>
                XMLTree xmlTree(xmlString.c_str(), xmlString.size());
                std::string dump = XMLDump(&amp;xmlTree, <font class="stringliteral">"/"</font>).get();
                <font class="comment">// STS This does not work anymore with default encoding added in XMLDump</font>
                <font class="comment">//CPPUNIT_ASSERT(dump == xmlString);</font>

                <font class="comment">// XMLTree methods</font>
                CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/string"</font>) == <font class="stringliteral">" abc def "</font>);

                CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/int"</font>) == 2);
                CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/int"</font>) == <font class="stringliteral">"-42"</font>);
                CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/int[position()=2]"</font>) == <font class="stringliteral">"17"</font>);

                CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/tags/subtag"</font>) == 2);
                CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/tags/subtag"</font>) == <font class="stringliteral">"EINS"</font>);

                CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/nonexisting"</font>) == 0);
                CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/nonexisting/tag"</font>) == 0);
                CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/nonexisting/tag"</font>) == <font class="stringliteral">""</font>);

                xmlTree.delTag(<font class="stringliteral">"/req/int"</font>);
                CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/int"</font>) == 1);

                xmlTree.addTag(<font class="stringliteral">"/req"</font>, <font class="stringliteral">"newtag"</font>, <font class="stringliteral">"contents of new tag"</font>);
                CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/newtag"</font>) == <font class="stringliteral">"contents of new tag"</font>);
        }
};


<font class="comment">// XSLTrans Test</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXSLTrans : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
        TestXSLTrans()
        {}
        <font class="keywordtype">void</font> testRun()
        {
                std::string xmlString(
                        <font class="stringliteral">"&lt;?xml version=\"1.0\"?&gt;\n"</font>
                        <font class="stringliteral">"&lt;!DOCTYPE guestbook&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">"&lt;guestbook&gt;\n"</font>
                        <font class="stringliteral">"       &lt;record&gt;\n"</font>
                        <font class="stringliteral">"               &lt;name&gt;kurt&lt;/name&gt;\n"</font>
                        <font class="stringliteral">"               &lt;date&gt;2.3.01&lt;/date&gt;\n"</font>
                        <font class="stringliteral">"               &lt;text&gt;\n"</font>
                        <font class="stringliteral">"               find ich total klasse, koennte aber noch besser sein\n"</font>
                        <font class="stringliteral">"               &lt;/text&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/record&gt;\n"</font>
                        <font class="stringliteral">"       &lt;record&gt;\n"</font>
                        <font class="stringliteral">"               &lt;name&gt;kurt&lt;/name&gt;\n"</font>
                        <font class="stringliteral">"               &lt;date&gt;2.3.01&lt;/date&gt;\n"</font>
                        <font class="stringliteral">"               &lt;text&gt;\n"</font>
                        <font class="stringliteral">"               find ich total klasse, koennte aber noch besser sein\n"</font>
                        <font class="stringliteral">"               &lt;/text&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/record&gt;\n"</font>
                        <font class="stringliteral">"&lt;/guestbook&gt;\n"</font>);

                std::string xslString(
                        <font class="stringliteral">"&lt;?xml version=\"1.0\"?&gt;\n"</font>
                        <font class="stringliteral">"&lt;xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"&gt;\n"</font>
                        <font class="stringliteral">"&lt;!-- Basic stylesheet for GUESTBOOK --&gt;\n"</font>
                        <font class="stringliteral">"&lt;xsl:output indent=\"yes\" /&gt;\n"</font>
                        <font class="stringliteral">"&lt;xsl:template match=\"guestbook\"&gt;\n"</font>
                        <font class="stringliteral">"       &lt;html&gt;\n"</font>
                        <font class="stringliteral">"               &lt;head&gt;\n"</font>
                        <font class="stringliteral">"                       &lt;title&gt;Gaestebuch&lt;/title&gt;\n"</font>
                        <font class="stringliteral">"               &lt;/head&gt; \n"</font>
                        <font class="stringliteral">"               &lt;body bgcolor=\"FFFFFF\"&gt;\n"</font>
                        <font class="stringliteral">"                       &lt;h1&gt;Mein Gaestebuch&lt;/h1&gt;\n"</font>
                        <font class="stringliteral">"                       &lt;center&gt;\n"</font>
                        <font class="stringliteral">"                       &lt;table cellspacing=\"2\" cellpadding=\"3\" border=\"0\"&gt;\n"</font>
                        <font class="stringliteral">"                       \n"</font>
                        <font class="stringliteral">"                       &lt;xsl:apply-templates/&gt;\n"</font>
                        <font class="stringliteral">"                       \n"</font>
                        <font class="stringliteral">"                       &lt;/table&gt;\n"</font>
                        <font class="stringliteral">"                       &lt;/center&gt;\n"</font>
                        <font class="stringliteral">"               &lt;/body&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/html&gt;\n"</font>
                        <font class="stringliteral">"&lt;/xsl:template&gt;\n"</font>
                        <font class="stringliteral">"\n"</font>
                        <font class="stringliteral">"&lt;xsl:template match=\"record\"&gt;\n"</font>
                        <font class="stringliteral">"       \n"</font>
                        <font class="stringliteral">"       &lt;tr&gt; \n"</font>
                        <font class="stringliteral">"       &lt;td bgcolor=\"336699\"&gt;&lt;font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"&gt;Name&lt;/font&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;td bgcolor=\"336699\"&gt;&lt;font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"&gt;&lt;xsl:value-of select=\"name\"/&gt;&lt;/font&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/tr&gt;\n"</font>
                        <font class="stringliteral">"       &lt;tr&gt;\n"</font>
                        <font class="stringliteral">"       &lt;td bgcolor=\"336699\"&gt;&lt;font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"&gt;Datum&lt;/font&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;td bgcolor=\"336699\"&gt;&lt;font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"&gt;&lt;xsl:value-of select=\"date\"/&gt;&lt;/font&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/tr&gt;\n"</font>
                        <font class="stringliteral">"       &lt;tr&gt;\n"</font>
                        <font class="stringliteral">"       &lt;td bgcolor=\"336699\"&gt;&lt;font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"&gt;Text&lt;/font&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;td bgcolor=\"336699\"&gt;&lt;font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"&gt;&lt;xsl:value-of select=\"text\"/&gt;&lt;/font&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/tr&gt;\n"</font>
                        <font class="stringliteral">"       &lt;tr &gt;\n"</font>
                        <font class="stringliteral">"       &lt;td colspan=\"2\"&gt;&lt;hr height=\"1\"/&gt;&lt;/td&gt;\n"</font>
                        <font class="stringliteral">"       &lt;/tr&gt;\n"</font>
                        <font class="stringliteral">"       \n"</font>
                        <font class="stringliteral">"&lt;/xsl:template&gt;\n"</font>
                        <font class="stringliteral">"&lt;/xsl:stylesheet&gt;\n"</font>);

                XMLTree xmlTree(xmlString);
                XSLTrans xslTrans(xslString);

                std::auto_ptr&lt;XMLTree&gt; resultTree = xslTrans.transToXMLTree(xmlTree.getDocPtr());

                CPPUNIT_ASSERT(resultTree.get()-&gt;getValue(<font class="stringliteral">"/html/head/title"</font>) == <font class="stringliteral">"Gaestebuch"</font>);
                CPPUNIT_ASSERT(resultTree.get()-&gt;getValue(<font class="stringliteral">"/html/body/h1"</font>) == <font class="stringliteral">"Mein Gaestebuch"</font>);
                CPPUNIT_ASSERT(resultTree.get()-&gt;getCount(<font class="stringliteral">"/html/body/center/table"</font>) == 1);
        }
};


<font class="comment">// TestXMLTree</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLTree : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
        TestXMLTree()
                :tree_(NULL)
        {
                <font class="comment">// test XMLTree's following functions:</font>
                <font class="comment">// getString</font>
                <font class="comment">// getBool (XSLT-functions: boolean, false, true, not)</font>
                <font class="comment">// getFloat</font>
                <font class="comment">// getNodeSet</font>
                <font class="comment">// getXML</font>
                <font class="comment">// getTree</font>
                xmlTree_ = <font class="stringliteral">"&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n"</font>
                        <font class="stringliteral">"&lt;test&gt;\n"</font>
                        <font class="stringliteral">"       &lt;string&gt;StringValue&lt;/string&gt;\n"</font>
                        <font class="stringliteral">"       &lt;string&gt;\n"</font>
                        <font class="stringliteral">"               Also a string value\n"</font>
                        <font class="stringliteral">"       &lt;/string&gt;\n"</font>
                        <font class="stringliteral">"       &lt;boolean&gt;1&lt;/boolean&gt;\n"</font>
                        <font class="stringliteral">"       &lt;boolean&gt;0&lt;/boolean&gt;\n"</font>
                        <font class="stringliteral">"       &lt;boolean&gt;-1&lt;/boolean&gt;\n"</font>
                        <font class="stringliteral">"       &lt;float&gt;3.5&lt;/float&gt;\n"</font>
                        <font class="stringliteral">"       &lt;float&gt;1.6&lt;/float&gt;\n"</font>
                        <font class="stringliteral">"       &lt;float&gt;-123.2&lt;/float&gt;\n"</font>
                        <font class="stringliteral">"&lt;/test&gt;"</font>;
                tree_ = <font class="keyword">new</font> XMLTree( xmlTree_.c_str() );
        }

        ~TestXMLTree()
        {
                <font class="keyword">delete</font> tree_;
        }
        <font class="keywordtype">void</font> testRun()
        {
                CPPUNIT_ASSERT( tree_ );

                testGetString();
                testGetBool();
                testGetFloat();
                testGetNodeSet();
                testGetXML();
                testGetTree();
        }

        <font class="keywordtype">void</font> testGetString()
        {
                <font class="comment">// test getString</font>
                <font class="comment">// string( xpath ) returns content of first xpath-node</font>
                <font class="comment">// string( invalid_xpath ) returns ""</font>
                CPPUNIT_ASSERT( tree_-&gt;getString( <font class="stringliteral">"string( /test/string )"</font> ) == <font class="stringliteral">"StringValue"</font> );
                CPPUNIT_ASSERT( tree_-&gt;getString( <font class="stringliteral">"string( /test/string[position() = 2] )"</font> ) == <font class="stringliteral">"\n\t\tAlso a string value\n\t"</font> );
                CPPUNIT_ASSERT( tree_-&gt;getString( <font class="stringliteral">"string( /test/something )"</font> ) == <font class="stringliteral">""</font> );
                <font class="keywordflow">try</font> {
                        <font class="comment">// xpath won't return a string</font>
                        tree_-&gt;getString( <font class="stringliteral">"/test/string"</font> );
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( GXML::XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_STRING_FROM_PATH );
                }
                <font class="keywordflow">try</font> {
                        <font class="comment">// xpath won't return a string</font>
                        tree_-&gt;getString( <font class="stringliteral">"/test/something"</font> );
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_STRING_FROM_PATH );
                }
        }

        <font class="keywordtype">void</font> testGetBool()
        {
                <font class="comment">// test getBool</font>
                <font class="comment">// 0 = false else true, "" = false else true</font>
                CPPUNIT_ASSERT( tree_-&gt;getBool( <font class="stringliteral">"boolean( number( /test/boolean ) )"</font> ) );
                CPPUNIT_ASSERT( ! tree_-&gt;getBool( <font class="stringliteral">"boolean( number( /test/boolean[position() = 2] ) )"</font> ) );
                CPPUNIT_ASSERT( tree_-&gt;getBool( <font class="stringliteral">"boolean( number( /test/boolean[position() = 3] ) )"</font> ) );
                CPPUNIT_ASSERT( tree_-&gt;getBool( <font class="stringliteral">"true()"</font> ) );
                CPPUNIT_ASSERT( ! tree_-&gt;getBool( <font class="stringliteral">"false()"</font> ) );
                CPPUNIT_ASSERT( tree_-&gt;getBool( <font class="stringliteral">"not( /test/something )"</font> ) );
                CPPUNIT_ASSERT( ! tree_-&gt;getBool( <font class="stringliteral">"not( /test/string )"</font> ) );
                CPPUNIT_ASSERT( tree_-&gt;getBool( <font class="stringliteral">"boolean( /test/string )"</font> ) );
                CPPUNIT_ASSERT( ! tree_-&gt;getBool( <font class="stringliteral">"boolean( /test/something )"</font> ) );
                CPPUNIT_ASSERT( tree_-&gt;getBool( <font class="stringliteral">"boolean( 'anything' )"</font> ) );
                CPPUNIT_ASSERT( ! tree_-&gt;getBool( <font class="stringliteral">"boolean( '' )"</font> ) );
                <font class="keywordflow">try</font> {
                        <font class="comment">// xpath won't return a boolean</font>
                        tree_-&gt;getBool( <font class="stringliteral">"/test/string"</font> );
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_BOOL_FROM_PATH );
                }
        }

        <font class="keywordtype">void</font> testGetFloat()
        {
                <font class="comment">// test getFloat</font>
                CPPUNIT_ASSERT_DOUBLES_EQUAL( <font class="keywordtype">double</font>(-118.1), tree_-&gt;getFloat( <font class="stringliteral">"sum( /test/float )"</font> ), 1E-10 );
                CPPUNIT_ASSERT_DOUBLES_EQUAL( <font class="keywordtype">double</font>(-123.2), tree_-&gt;getFloat( <font class="stringliteral">"number( /test/float[position() = 3 ])"</font> ), 1E-10 );
                CPPUNIT_ASSERT( tree_-&gt;getFloat( <font class="stringliteral">"count( /test/boolean )"</font> ) == 3 );
                <font class="keywordflow">try</font> {
                        <font class="comment">// xpath won't return a number</font>
                        tree_-&gt;getFloat( <font class="stringliteral">"/test/string"</font> );
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_FLOAT_FROM_PATH );
                }
        }

        <font class="keywordtype">void</font> testGetNodeSet()
        {
                std::auto_ptr&lt;XMLNodeSet&gt; set = tree_-&gt;getNodeSet( <font class="stringliteral">"/test/*"</font> );
                CPPUNIT_ASSERT( set-&gt;size() == 8 );
                <font class="keywordflow">try</font> {
                        <font class="comment">// xpath will return an empty set</font>
                        std::auto_ptr&lt;XMLNodeSet&gt; set = tree_-&gt;getNodeSet( <font class="stringliteral">"/something"</font> );
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODESET_NO_MATCH );
                }
                <font class="keywordflow">try</font> {
                        <font class="comment">// xpath won't return a set</font>
                        std::auto_ptr&lt;XMLNodeSet&gt; set = tree_-&gt;getNodeSet( <font class="stringliteral">"true()"</font> );
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODESET_NO_SET );
                }
        }

        <font class="keywordtype">void</font> testGetXML()
        {
                CPPUNIT_ASSERT( xmlTree_+<font class="stringliteral">"\n"</font> == tree_-&gt;getXML( <font class="stringliteral">"/"</font> ) );
        }
        <font class="keywordtype">void</font> testGetTree()
        {
                CPPUNIT_ASSERT( tree_-&gt;getXML() == tree_-&gt;getTree()-&gt;getXML() );
        }
<font class="keyword">private</font>:
        std::string xmlTree_;
        XMLTree *tree_;
};


<font class="comment">// TestXMLNode</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLNode : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
        TestXMLNode()
        {
                <font class="comment">// test XMLNode's following functions:</font>
                <font class="comment">// getNext</font>
                <font class="comment">// getPrev</font>
                <font class="comment">// getParent</font>
                <font class="comment">// getLast</font>
                <font class="comment">// getChild</font>
                <font class="comment">// getName</font>
                <font class="comment">// getContent</font>
                <font class="comment">// getAttribute( name )</font>
                xmlTree_ = <font class="stringliteral">"&lt;?xml version=\"1.0\"?&gt;\n"</font>
                        <font class="stringliteral">"&lt;test&gt;&lt;node&gt;node1&lt;/node&gt;&lt;node name=\"node2\"&gt;node2&lt;child&gt;child of node 2&lt;/child&gt;&lt;/node&gt;&lt;node&gt;last node&lt;/node&gt;&lt;/test&gt;"</font>;
                tree_ = <font class="keyword">new</font> XMLTree( xmlTree_.c_str() );
        }
        <font class="keywordtype">void</font> testRun()
        {
                XMLNode node1 = XMLNode();
                XMLNode node2 = XMLNode();
                XMLNode node3 = XMLNode();
                std::auto_ptr&lt;XMLNodeSet&gt; set = tree_-&gt;getNodeSet( <font class="stringliteral">"/test/node"</font> );
                <font class="keywordflow">for</font> ( XMLNodeSet::Iterator iter = set-&gt;begin(); iter != set-&gt;end(); ++iter )
                {
                        <font class="keywordflow">switch</font> ( iter.getPosition() )
                        {
                        <font class="keywordflow">case</font> 0: node1 = *iter;
                                <font class="keywordflow">break</font>;
                        <font class="keywordflow">case</font> 1: node2 = *iter;
                                <font class="keywordflow">break</font>;
                        <font class="keywordflow">case</font> 2: node3 = *iter;
                                <font class="keywordflow">break</font>;
                        <font class="keywordflow">default</font>: CPPUNIT_ASSERT( <font class="keyword">false</font> ); <font class="comment">//should never get here.</font>
                        }
                }

                CPPUNIT_ASSERT( node1.getName() == <font class="stringliteral">"node"</font> );
                CPPUNIT_ASSERT( node1.getContent() == <font class="stringliteral">"node1"</font> );
                CPPUNIT_ASSERT( node1.getNext().getContent() == node2.getContent() );
                <font class="comment">//      CPPUNIT_ASSERT( node1.getLast().getContent() == node3.getContent() );</font>

                CPPUNIT_ASSERT( node2.getContent() == <font class="stringliteral">"node2"</font> );
                CPPUNIT_ASSERT( node2.getNext().getContent() == node3.getContent() );
                CPPUNIT_ASSERT( node2.getAttribute( <font class="stringliteral">"name"</font> ) == <font class="stringliteral">"node2"</font> );
                <font class="comment">// first child of node2 is its content text-node, therefore take child-&gt;next</font>
                CPPUNIT_ASSERT( node2.getChild().getNext().getContent() == <font class="stringliteral">"child of node 2"</font> );

                CPPUNIT_ASSERT( node3.getPrev().getContent() == node2.getContent() );
                CPPUNIT_ASSERT( node3.getParent().getName() == node2.getParent().getName() );
                CPPUNIT_ASSERT( node3.getParent().getChild().getContent() == node1.getContent() );
                CPPUNIT_ASSERT( node3.getParent().getLast().getContent() == node3.getContent() );

                <font class="comment">// first child of node with content is a text-node</font>
                CPPUNIT_ASSERT( node3.getChild().getName() == <font class="stringliteral">"text"</font> );

                <font class="comment">// test for exceptions</font>
                <font class="keywordflow">try</font> {
                        node3 = node3.getNext();
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_LAST_NODE );
                }
                <font class="keywordflow">try</font> {
                        node1 = node1.getPrev();
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_FIRST_NODE );
                }
                <font class="keywordflow">try</font> {
                        <font class="comment">// parent of parent is document node, which has no parent</font>
                        node1 = node1.getParent().getParent().getParent();
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_NO_PARENT );
                }
                <font class="keywordflow">try</font> {
                        node1 = node1.getPrev();
                        CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
                } <font class="keywordflow">catch</font> ( XMLException &amp; e ) {
                        CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_FIRST_NODE );
                }
        }

<font class="keyword">private</font>:
        std::string xmlTree_;
        XMLTree *tree_;
};


<font class="comment">// XMLNodeSet Test</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLNodeSet : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
        TestXMLNodeSet()
        {};
        <font class="keywordtype">void</font> testRun()
        {}
};

<font class="comment">// Master Class for all tests</font>
<font class="comment">//</font>
<font class="keyword">class </font>UnitTests: <font class="keyword">public</font> CppUnit::TestSuite
{
<font class="keyword">public</font>:
        UnitTests()
        {
                addTest(<font class="keyword">new</font> CppUnit::TestCaller&lt;TestXMLTreeOld&gt;(<font class="stringliteral">"Testing XMLTree (old)"</font>, &amp;TestXMLTreeOld::testRun));
                addTest(<font class="keyword">new</font> CppUnit::TestCaller&lt;TestXMLTree&gt;   (<font class="stringliteral">"Testing XMLTree"</font>,       &amp;TestXMLTree::testRun));
                addTest(<font class="keyword">new</font> CppUnit::TestCaller&lt;TestXSLTrans&gt;  (<font class="stringliteral">"Testing XSLTrans"</font>,      &amp;TestXSLTrans::testRun));
                addTest(<font class="keyword">new</font> CppUnit::TestCaller&lt;TestXMLNode&gt;   (<font class="stringliteral">"Testing XMLNode"</font>,       &amp;TestXMLNode::testRun));
                addTest(<font class="keyword">new</font> CppUnit::TestCaller&lt;TestXMLNodeSet&gt;(<font class="stringliteral">"Testing XMLNodeSet"</font>,    &amp;TestXMLNodeSet::testRun));
        }
};

}}

<font class="comment">// create binary that tests the XML* stuff</font>
<font class="keywordtype">int</font>
main(<font class="keywordtype">int</font> argc, <font class="keywordtype">char</font> **argv)
{
        CppUnit::TextTestRunner runner;
        <font class="comment">// This must be an object, and must be created with new, as it is deleted by runner (this is braindead, but well).</font>
        SP::GXML::UnitTests * unitTests = <font class="keyword">new</font> SP::GXML::UnitTests();
        runner.addTest(unitTests);
        <font class="keywordflow">if</font> (runner.run())
                <font class="keywordflow">return</font>(0);
        <font class="keywordflow">else</font>
                <font class="keywordflow">return</font>(1);
}
</pre></div><hr><address align="right"><small>Generated on Thu Jun 3 19:20:12 2004 for sp-gxmlcpp by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>