File: attribute.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 (331 lines) | stat: -rwxr-xr-x 9,657 bytes parent folder | download | duplicates (2)
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
# Features covered:  Attribute Lists
#
# This file contains a collection of tests for the TclXML parser.
# This file tests the parser's performance on Attribute Lists.
# Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 1998-2004 Zveno Pty Ltd.
#
# $Id: attribute.test,v 1.12 2004/02/20 09:15:52 balls Exp $

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

testPackage $::tcltest::parser

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

    variable SETUP {
	variable countAttributesOnly 1
	variable result
	catch {unset result}
	array set result {}

	proc EStart {tagName attrList args} {
	    variable result
	    variable countAttributesOnly

	    if {![llength $attrList] && !$countAttributesOnly} {
		if {[info exists result($tagName)]} {
		    set count 0
		    while {[info exists result($tagName/[incr count])]} {}
		    set result($tagName/$count) {}
		} else {
		    set result($tagName) {}
		}
		return {}
	    }

	    foreach {name value} $attrList {
		if {[info exists result($tagName,$name)]} {
		    set count 0
		    while {[info exists result($tagName,$name,[incr count])]} {}
		    set result($tagName,$name,$count) $value
		} else {
		    set result($tagName,$name) $value
		}
	    }
	}

	proc pcdata t {
	    variable pcdata
	    append pcdata $t
	}

	proc EntRef args {
	    variable entrefs
	    if {[catch {incr entrefs}]} {
		set entrefs 1
	    }
	}

	set parser [::xml::parser -elementstartcommand [namespace code EStart]]
    }
    variable SETUP2 {
	eval $SETUP
	$parser configure -characterdatacommand [namespace code pcdata]
    }
    variable SETUP3 {
	eval $SETUP
	$parser configure -entityreferencecommand [namespace code EntRef]
	variable entrefs 0
    }

    variable CLEANUP {
	$parser free
	catch {unset result}
	catch {unset pcdata}
	catch {unset entrefs}
	rename EStart {}
	rename pcdata {}
	rename EntRef {}
    }

    test attrList-1.1 {empty attribute list} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test></Test>
}
	array size result
    } -cleanup $CLEANUP -result 0

    test attrList-1.2 {single attribute} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr="1"></Test>
}

	array get result
    } -cleanup $CLEANUP -result {Test,attr 1}

    test attrList-1.3 {multiple distinct attributes} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test first="1" second='2'></Test>
}

	list [array size result] $result(Test,first) $result(Test,second)
    } -cleanup $CLEANUP -result {2 1 2}

    test attrList-1.4 {hyphen in attribute name} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test first-attr="1"></Test>
}

	array get result
    } -cleanup $CLEANUP -result {Test,first-attr 1}

    test attrList-2.1 {right angle bracket in attribute value} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr="value>"></Test>
}

	array get result
    } -cleanup $CLEANUP -result {Test,attr value>}

    test attrList-2.2 {right angle bracket in attribute value} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr="value1>value2"></Test>
}

	array get result
    } -cleanup $CLEANUP -result {Test,attr value1>value2}

    test attrList-2.3 {right angle bracket in attribute value} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr1="value1" attr2="value2>"></Test>
}

	sortedarray result
    } -cleanup $CLEANUP -result {Test,attr1 value1 Test,attr2 value2>}

    test attrList-2.4 {right angle bracket in attribute value} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr2="value2>" attr1="value1"></Test>
}

	sortedarray result
    } -cleanup $CLEANUP -result {Test,attr1 value1 Test,attr2 value2>}

    test attrList-2.5 {right angle brackets in attribute values} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr1="value>1" attr2="value>2"></Test>
}

	sortedarray result
    } -cleanup $CLEANUP -result {Test,attr1 value>1 Test,attr2 value>2}

    test attrList-2.6 {right angle brackets in attribute values} -setup $SETUP2 -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr1="value>1">some text</Test>
}

	list [array get result] $pcdata
    } -cleanup $CLEANUP -result {{Test,attr1 value>1} {some text}}

    test attrList-3.1 {unnested left brace in attribute value} -setup $SETUP -body {
	$parser parse [format {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr="%svalue"></Test>
} \{]

	array get result
    } -cleanup $CLEANUP -result [list Test,attr [format {%svalue} \{]]

    test attrList-3.2 {unnested right brace in attribute value} -setup $SETUP -body {
	$parser parse [format {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr="value%s"></Test>
} \}]

	array get result
    } -cleanup $CLEANUP -result [list Test,attr [format {value%s} \}]]

    test attrList-3.3 {Tcl special characters in attribute value} -setup $SETUP -body {
	$parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr="dollar $ backslash \ brackets [puts hello]"></Test>
}

	array get result
    } -cleanup $CLEANUP -result {Test,attr {dollar $ backslash \ brackets [puts hello]}}

    test attrList-4.1 {Unquoted attribute value} -constraints {xml_tcl} -setup $SETUP -match glob -body {
	expectError {
	    $parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr=noquotes></Test>
}
	}
    } -cleanup $CLEANUP -result {*invalid attribute list around line 2*}
    test attrList-4.1 {Unquoted attribute value} -constraints {xml_expat} -setup $SETUP -body {
	expectError {
	    $parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr=noquotes></Test>
}
	}
    } -cleanup $CLEANUP -result {error "not well-formed" at line 3 character 11}
    test attrList-4.1 {Unquoted attribute value} -constraints {xml_libxml2} -match glob -setup $SETUP -body {
	expectError {
	    $parser parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test attr=noquotes></Test>
}
	}
    } -cleanup $CLEANUP -result {*attribute-not-started*}

    # Test case contributed by David Sutton <dsutton@coactive.com>
    test attrList-5.0 {Complicated attribute list} -setup $SETUP2 -body {
	variable countAttributesOnly 0

	$parser parse {<?xml version="1.0"?>
<event name='LogAlarmReset' ID='22' startDisabled='no' deleteOnCompletion='no' startDateTime='' endDateTime=''>
  <stateChangeTrigger initialState='true' condition='AlarmSwitch = FALSE'/>
  <eventAction>LightState = LightCtl</eventAction>
  <eventAction>LOG(AlarmSwitch)</eventAction>
  <eventAction>DISABLE(BlinkLight)</eventAction>
  <eventAction>NOTIFY( AlarmSwitch,"Alarm has been reset")</eventAction>
</event>
}

	regsub -all "\[ \t\n\]+" $pcdata { } pcdata
	set sortedResult {}
	foreach key [lsort -dictionary [array names result]] {
	    lappend sortedResult $key $result($key)
	}
	list $sortedResult $pcdata
    } -cleanup $CLEANUP -result [list {event,deleteOnCompletion no event,endDateTime {} event,ID 22 event,name LogAlarmReset event,startDateTime {} event,startDisabled no eventAction {} eventAction/1 {} eventAction/2 {} eventAction/3 {} stateChangeTrigger,condition {AlarmSwitch = FALSE} stateChangeTrigger,initialState true} { LightState = LightCtl LOG(AlarmSwitch) DISABLE(BlinkLight) NOTIFY( AlarmSwitch,"Alarm has been reset") }]

    # Test case contributed by Marshall Rose <mrose@dbc.mtview.ca.us>
    test attrList-5.1 {Attribute list with quoted value} -setup $SETUP -body {
	variable countAttributesOnly 0

	$parser parse {<test example="isn&apos;t this legal?"/>}

	array get result
    } -cleanup $CLEANUP -result {test,example {isn't this legal?}}

    test attrList-5.2 {Attribute list with unresolved entity reference} -match glob -setup $SETUP -body {
	variable countAttributesOnly 0

	expectError {
	    $parser parse {
<test example='isn&apos;t this legal&ques;'/>
}
	}
    } -cleanup $CLEANUP -result *

    test attrList-5.3 {Attribute list with unresolved entity reference and entity callback} -setup $SETUP3 -body {
	variable countAttributesOnly 0

	$parser parse {
<test example='isn&apos;t this great&quot;'/>
}

	set entrefs
    } -cleanup $CLEANUP -result 0

    test attrList-5.4 {Attribute list with entity reference} -setup $SETUP -body {
	variable countAttributesOnly 0

	$parser parse {<!DOCTYPE test [
<!ENTITY ques "?">
]>
<test example='isn&apos;t this great&ques;'/>
}

	array get result
    } -cleanup $CLEANUP -result {test,example {isn't this great?}}

    test attrList-5.5 {Attribute list with nested entity references} -setup $SETUP -body {
	variable countAttributesOnly 0

	$parser parse {<!DOCTYPE test [
<!ENTITY level2 "OK">
<!ENTITY rules "rules, &level2;?">
]>
<test example='XML &rules;'/>
}

	array get result
    } -cleanup $CLEANUP -result {test,example {XML rules, OK?}}

    # Test case contributed by Joe English, bug #546295
    test attrList-5.6 {Attribute list with character entity references} -setup $SETUP -body {
	variable countAttributesOnly 0

	$parser parse {
<foo bar="abc&#xA;def"/>
}

	array get result
    } -cleanup $CLEANUP -result {foo,bar {abc
def}}

    # Test case contributed by Laurent Duperval, bug #620034
    test attrList-5.7 {Attribute with right angle bracket} -setup $SETUP -body {
	variable countAttributesOnly 0

	$parser parse {
<foo><bar att=">=foo"/></foo>
}

	array get result
    } -cleanup $CLEANUP -result {foo {} bar,att >=foo}

    cleanupTests
}

namespace delete ::xml::attributeTest

return