File: parser.test

package info (click to toggle)
tclxml 3.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,080 kB
  • ctags: 876
  • sloc: ansic: 6,064; tcl: 5,116; xml: 4,642; sh: 3,112; makefile: 60
file content (305 lines) | stat: -rwxr-xr-x 7,560 bytes parent folder | download | duplicates (5)
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
# Features covered:  Parser functions
#
# This file contains a collection of tests for the TclXML parser.
# This file tests the parser's basic functions.
# Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 1999-2004 Zveno Pty Ltd.
#
# $Id: parser.test,v 1.10 2004/02/20 09:15:52 balls Exp $

package require tcltest
source [file join [tcltest::workingDirectory] tclxmlutils.tcl]

testPackage $::tcltest::parser

namespace eval ::xml::parserTest {
    namespace import -force ::tcltest::*

    variable SETUP {
	variable started
	array set started {}
	variable ended
	array set ended {}
	variable elList {}
	variable data {}

	proc parray arrayName {
	    upvar #0 $arrayName arr
	    foreach key [lsort [array names $arrayName]] {
		lappend result $key $arr($key)
	    }
	    return $result
	}

	proc Start {name atList args} {
	    variable started
	    array set opts $args
	    array set atts $atList

	    if {![info exists started($name)]} {
		set started($name) 1
	    } else {
		incr started($name)
	    }

	    if {[info exists atts(class)]} {
		switch $atts(class) {
		    continue {
			return -code continue
		    }
		    break {
			return -code break
		    }
		    error {
			return -code error "error condition in callback"
		    }
		    default {
			return -code $atts(class) -errorcode $atts(class)
		    }
		}
	    }
	}

	proc End {name args} {
	    variable ended
	    array set opts $args

	    if {![info exists ended($name)]} {
		set ended($name) 1
	    } else {
		incr ended($name)
	    }
	}
	proc PI {name args} {
	    return -code $name
	}

	proc ElStart {name atList args} {
	    variable elList
	    array set opts {-empty 0}
	    array set opts $args
	    lappend elList start $name $opts(-empty)
	}
	proc ElEnd {name args} {
	    variable elList
	    array set opts {-empty 0}
	    array set opts $args
	    lappend elList end $name $opts(-empty)
	}
	proc pcdata text {
	    variable data
	    append data $text
	}
    }
    variable SETUP2 {
	eval $SETUP
	set p [::xml::parser -elementstartcommand [namespace code Start]]
    }
    variable CLEANUP {
	catch {unset started}
	catch {unset ended}
	catch {unset elList}
	catch {unset data}
	rename parray {}
	rename Start {}
	rename End {}
	rename PI {}
	rename ElStart {}
	rename ElEnd {}
	rename pcdata {}
	catch {$p free}
    }

    test parser-1.1 {parser creation} -setup $SETUP -body {
	set p [::xml::parser]
	list [regexp {^xmlparser[0-9]+$} $p] \
		[string equal [info commands $p] $p]
    } -cleanup $CLEANUP -result {1 1}

    test parser-1.2 {parser creation, only options} -setup $SETUP -body {
	set p [::xml::parser -elementstartcommand EStart]
	list [regexp {^xmlparser[0-9]+$} $p] \
		[string equal [info commands $p] $p]
    } -cleanup $CLEANUP -result {1 1}

    test parser-1.3 {parser creation, named} -body {
	set result [::xml::parser testparser]
	list $result [info commands testparser]
    } -cleanup {
	rename testparser {}
    } -result {testparser testparser}

    test parser-1.4 {parser creation, named with options} -body {
	set result [::xml::parser testparser -elementstartcommand EStart]
	list $result [info commands testparser]
    } -cleanup {
	rename testparser {}
    } -result {testparser testparser}

    test parser-1.5 {configure -baseuri} -body {
	set parser [::xml::parser -baseuri file:///tmp/parser-1.5.test]
	ok
    } -cleanup {
	$parser free
    } -result {}

    # Test break return code from callback

    test parser-2.1 {break in callback} -setup $SETUP2 -body {
	$p parse {<?xml version="1.0"?>
<Test>
<Element id="el1">Should see this data</Element>
<Element class="break" id="el2">Should not see this data<Element id="el3"/></Element>
<Element id='el4'>Should not see this data</Element>
</Test>
}
	set started(Element)
    } -cleanup $CLEANUP -result 2

    test parser-2.2 {break in callback} -setup $SETUP2 -body {
	$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data<Element class="break"/></Element>
<Element>Should not see this data</Element>
</Test>
}
	set started(Element)
    } -cleanup $CLEANUP -result 3

    test parser-2.3 {break in callback} -setup $SETUP2 -body {
	$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data
  <?break?>
</Element>
<Element>Should not see this data</Element>
</Test>
}
	set started(Element)
    } -cleanup $CLEANUP -result 3

    test parser-3.1 {continue in callback} -setup $SETUP2 -body {
	$p parse {<?xml version="1.0"?>
<Test>
<Element id='el1'>Should see this data</Element>
<Element class="continue" id='el2'>Should not see this data
  <Element id='el3'/>
</Element>
<Element id='el4'>Should see this data</Element>
</Test>
}
	set started(Element)
    } -cleanup $CLEANUP -result 3

    test parser-3.2 {continue in callback} -setup $SETUP2 -body {
	$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data
  <Element class="continue">
    Should not see this data
    <Element/>
  </Element>
  Should see this data
  <Element/>
</Element>
<Element>Should see this data</Element>
</Test>
}
	set started(Element)
    } -cleanup $CLEANUP -result 5

    test parser-3.3 {continue in callback} -setup $SETUP2 -body {
	$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element>Should see this data
  <Element class="continue">
    Should not see this data
    <Element class="break"/>
    break will have no effect
  </Element>
  Should see this data
  <Element/>
</Element>
<Element>Should see this data</Element>
</Test>
}
	set started(Element)
    } -cleanup $CLEANUP -result 5

    test parser-4.1 {error in callback} -setup $SETUP2 -body {
	set result [catch {
	    $p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="error"/>
<Element>Should not see this data</Element>
</Test>
}
	}]
	list $result $started(Element)
    } -cleanup $CLEANUP -result {1 2}

    # catch doesn't seem to return the actual error code
    test parser-4.2 {error in callback} -setup $SETUP2 -body {
	set errcode [catch {$p parse {<?xml version="1.0"?>
<Test>
<Element>Should see this data</Element>
<Element class="13"/>
<Element>Should not see this data</Element>
</Test>
}} result]
	list $errcode $started(Element)
    } -cleanup $CLEANUP -result {1 2}

    test parser-5.0 {free parser} -body {
	set p [::xml::parser]
	$p free
	info commands $p
    } -result {}

    # Test for bug #510418
    test parser-5.1 {free in namespace} -body {
	namespace eval ::xml::parserTest::test51 {
	    set p [::xml::parser -elementstartcommand Foo]
	    $p free
	}
    } -cleanup {
	namespace delete ::xml::parserTest::test51
    } -result {}

    # Test for bug #510419
    test parser-6.1 {reset parser instance} -setup $SETUP2 -body {
	$p reset
	$p parse {<Test/>}
	set first $started(Test)

	catch {unset started}
	array set started {}
	$p reset
	$p parse {<Test><Test/></Test>}

	list $first $started(Test)
    } -cleanup $CLEANUP -result [list 1 2]

    # Test for bug #579264
    # libxml2 doesn't recognise ignorable whitespace unless validating
    test parser-7.1 {-ignorewhitespace option} -setup $SETUP -constraints {!xml_libxml2} -body {
	set p [::xml::parser -characterdatacommand [namespace code pcdata] -ignorewhitespace 1]
	$p parse {<Test>
  <Value/>
</Test>
}
	string length $data
    } -cleanup $CLEANUP -result 0

    cleanupTests
}

namespace delete ::xml::parserTest
return