File: decls.test

package info (click to toggle)
tdom 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,260 kB
  • sloc: ansic: 56,762; xml: 20,797; tcl: 3,618; sh: 658; makefile: 83; cpp: 30
file content (373 lines) | stat: -rw-r--r-- 10,036 bytes parent folder | download | duplicates (4)
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
# Features covered:  Declarations
#
# This file tests the parser's performance on markup declarations.
# Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 2000 Zveno Pty Ltd.
#
# $Id$

source [file join [file dir [info script]] loadtdom.tcl]

set baseURI file://[file join [pwd] [file dir [info script]] decls.test]
catch {unset elements}
proc elementDecl {name cmodel} {
    set ::elements($name) $cmodel
}
catch {unset attrs}
proc attlistDecl {name attName type dfltValue isrequired} {
    lappend ::attrs($name/$attName) $type $isrequired $dfltValue
}
catch {unset entities}
proc entityDecl {name is_param value base systemId publicId notationName} {
    set ::entities($name) [list $is_param $value $base $systemId $publicId $notationName]
}
catch {unset cdata}
proc CData data {
    append ::cdata [string trim $data]
}

proc extRefH {base systemId publicId} {
    if {![regexp {^[a-zA-Z]+://} $systemId]}  {
        regsub {^[a-zA-Z]+://} $base {} base
        set basedir [file dirname $base]
        set systemId "[set basedir]/[set systemId]"
    } else {
        regsub {^[a-zA-Z]+://} $systemId systemId
    }
    if {[catch {set fd [open $systemId]}]} {
        return -code error \
                -errorinfo "Failed to open external entity $systemId"
    }
    return [list channel $systemId $fd]
}

proc extRefHstr {base systemId publicId} {
    if {![regexp {^[a-zA-Z]+://} $systemId]}  {
        regsub {^[a-zA-Z]+://} $base {} base
        set basedir [file dirname $base]
        set systemId "[set basedir]/[set systemId]"
    } else {
        regsub {^[a-zA-Z]+://} $systemId systemId
    }
    if {[catch {set fd [open $systemId]}]} {
        return -code error \
                -errorinfo "Failed to open external entity $systemId"
    }
    return [list string  $systemId [read $fd [file size $systemId]]]
}


# Internal DTD subset

test decls-1.1 {element declaration} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-1.1 {}}
    set parser [xml::parser decls-1.1 \
	-elementdeclcommand elementDecl]
    $parser parse {<?xml version="1.0"?>
<!DOCTYPE Test [
<!ELEMENT Test (#PCDATA)>
]>
<Test></Test>
}
    array get ::elements
} {Test {MIXED {} {} {}}}

test decls-2.1 {attribute list declaration, implied} {
    catch {unset ::attrs}
    array set ::attrs {}

    catch {rename xml::decls-2.1 {}}
    set parser [xml::parser decls-2.1 \
	-attlistdeclcommand attlistDecl]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test [
<!ELEMENT Test (#PCDATA)>
<!ATTLIST Test
	test CDATA #IMPLIED>
]>
<Test test="value"/>}

    array get ::attrs
} {Test/test {CDATA 0 {}}}

test decls-2.2 {attribute list declaration, enum} {
    catch {unset ::attrs}
    array set ::attrs {}

    catch {rename xml::decls-2.2 {}}
    set parser [xml::parser decls-2.2 \
	-attlistdeclcommand attlistDecl]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test [
<!ELEMENT Test (#PCDATA)>
<!ATTLIST Test
	test (LGL|OTH) "LGL">
]>
<Test test="value"/>}

    array get ::attrs
} {Test/test {(LGL|OTH) 0 LGL}}

test decls-3.1 {entity declarations} {
    catch {unset ::entities}
    array set ::entities {}

    catch {rename xml::decls-3.1 {}}
    set parser [xml::parser decls-3.1 \
	-entitydeclcommand entityDecl]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test [
<!ENTITY testEnt "replacement text">
<!ELEMENT Test (#PCDATA)>
]>
<Test/>}

    array get ::entities
} {testEnt {0 {replacement text} {} {} {} {}}}

test decls-4.1 {parameter entity declarations} {
    catch {unset ::entities}
    array set ::entities {}
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-4.1 {}}
    set parser [xml::parser decls-4.1 \
            -paramentityparsing notstandalone \
            -elementdeclcommand elementDecl]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test [
<!ENTITY % PEnt "&#60;!ELEMENT Test (#PCDATA)&#62;">
%PEnt;
]>
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}

# NB. entity.test tests entity replacement as well

# External entities

test decls-5.1 {external entity} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-5.1 {}}
    set parser [xml::parser decls-5.1 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-5.1.dtd">
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}

test decls-5.2 {external DTD subset} {
    catch {unset ::elements}
    array set ::elements {}
    catch {unset ::entities}
    array set ::entities {}

    catch {rename xml::decls-5.2 {}}
    set parser [xml::parser decls-5.2 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-5.2.dtd">
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}

test decls-5.3 {external entity} {
    catch {unset ::elements}
    array set ::elements {}
    catch {unset ::entities}
    array set ::entities {}
    catch {unset ::externals}
    array set ::externals {}

    catch {rename xml::decls-5.3 {}}
    set parser [xml::parser decls-5.3 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test [
<!ENTITY % module SYSTEM "data/dtd-5.2.dtd">
%module;
]>
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}


test decls-5.4 {external entity} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-5.4 {}}
    set parser [xml::parser decls-5.4 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefHstr \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-5.1.dtd">
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}

test decls-5.5 {external DTD subset} {
    catch {unset ::elements}
    array set ::elements {}
    catch {unset ::entities}
    array set ::entities {}

    catch {rename xml::decls-5.5 {}}
    set parser [xml::parser decls-5.5 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefHstr \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-5.2.dtd">
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}


test decls-5.6 {external entity} {
    catch {unset ::elements}
    array set ::elements {}
    catch {unset ::entities}
    array set ::entities {}
    catch {unset ::externals}
    array set ::externals {}

    catch {rename xml::decls-5.6 {}}
    set parser [xml::parser decls-5.6 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefHstr \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test [
<!ENTITY % module SYSTEM "data/dtd-5.2.dtd">
%module;
]>
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}


# Conditional Sections

test decls-6.1 {conditional section: include} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-6.1 {}}
    set parser [xml::parser decls-6.1 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-6.1.dtd">
<Test/>}

    array get ::elements
} {Test {MIXED {} {} {}}}

test decls-6.2 {conditional section: include, empty} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-6.2 {}}
    set parser [xml::parser decls-6.2 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-6.2.dtd">
<Test/>}

    array get ::elements
} {}

test decls-6.3 {conditional section: include, empty} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-6.3 {}}
    set parser [xml::parser decls-6.3 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-6.3.dtd">
<Test/>}

    array get ::elements
} {}

test decls-6.4 {conditional section: include, nested} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-6.4 {}}
    set parser [xml::parser decls-6.4 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-6.4.dtd">
<Test/>}

    array size ::elements
} 3

test decls-6.6 {conditional section/PE combo} {
    catch {unset ::elements}
    array set ::elements {}

    catch {rename xml::decls-6.6 {}}
    set parser [xml::parser decls-6.6 \
            -paramentityparsing notstandalone \
            -externalentitycommand extRefH \
            -elementdeclcommand elementDecl \
            -baseurl $baseURI]
    $parser parse {<?xml version='1.0'?>
<!DOCTYPE Test SYSTEM "data/dtd-6.6.dtd">
<Test/>}

    array size ::elements
} 2

foreach parser [info commands decls-*] {
    $parser free
}

# cleanup
::tcltest::cleanupTests
return