File: serialization.test

package info (click to toggle)
tclxml 3.3~svn11-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,392 kB
  • sloc: ansic: 13,292; tcl: 11,656; xml: 3,269; sh: 559; makefile: 15
file content (238 lines) | stat: -rw-r--r-- 9,290 bytes parent folder | download | duplicates (3)
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
# Commands covered:  ::dom::DOMimplementation serialize
#
# This file contains a collection of tests for one or more of the 
# TclDOM commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 2008 Explain
# Copyright (c) 1998-2004 Zveno Pty Ltd.
#
# $Id: serialization.test,v 1.17 2004/09/02 21:21:39 balls Exp $

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

testPackage dom

namespace eval ::dom::serializationTest {
    namespace import -force ::tcltest::*

    variable SETUP {
	set doc [::dom::DOMImplementation create]
	set top [::dom::document createElement $doc Test]
	dom::node appendChild $doc $top
    }
    variable SETUP2 {
	eval $SETUP
	$top appendChild [set e1 [::dom::document createElement $top Element1]]
	$top appendChild [set e2 [::dom::document createElement $top Element2]]
	$top appendChild [set e3 [::dom::document createElement $top Element3]]
    }
    variable CLEANUP {
	dom::destroy $doc
    }

    # The result may or may not use empty element syntax
    test serialize-1.1 {single element document} -match regexp -setup $SETUP -body {
	::dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1\.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<Test(/>|></Test>)}

    test serialize-1.2 {element-only document} -match regexp -setup $SETUP2 -body {
	::dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<Test><Element1/><Element2/><Element3/></Test>}

    test serialize-1.3 {serialize element} -constraints {!dom_libxml2} -setup $SETUP2 -body {
	::dom::DOMImplementation serialize $top
    } -result {<Test><Element1/><Element2/><Element3/></Test>}

    test serialize-1.4 {serialize non-empty element} -match regexp -setup {
	eval $SETUP2
	$e1 appendChild [::dom::document createElement $e1 Child1]
    } -body {
	::dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<Test><Element1><Child1/></Element1><Element2/><Element3/></Test>}

    test serialize-2.1 {missing document element} -match regexp -setup {
	set doc [::dom::DOMImplementation create]
    } -body {
	::dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {(<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>)?}

    # The result may or may not use empty element syntax
    test serialize-3.1 {XML declaration: attributes in correct order} -match regexp -setup {
	set doc [::dom::DOMImplementation parse {<?xml version="1.0"?>
<Test></Test>
}]
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1\.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<Test(/>|></Test>)}

    # This test fails if using TclExpat

    test serialize-3.2 {XML declaration: attributes in correct order} -match regexp -setup {
	set doc [::dom::DOMImplementation parse {<?xml version="1.0" standalone="yes"?>
<Test></Test>
}]
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))? standalone=('|")yes('|")\?>
(<!DOCTYPE Test>
)?<Test(/>|></Test>)}

    test serialize-3.3 {Doctype declaration} -match regexp -setup {
	set doc [::dom::DOMImplementation parse {<?xml version="1.0"?>
<!DOCTYPE Test>
<Test></Test>
}]
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<Test(/>|></Test>)}

    # See bug #525505
    test serialize-3.4 {Document prologue} -match regexp -setup {
	set doc [::dom::parse {<?xml version="1.0"?>
<!-- a comment in the prologue -->
<Test/>
}]
    } -body {
	dom::serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<!-- a comment in the prologue -->(
)?<Test/>}

    test serialize-3.5 {Document epilogue} -match regexp -setup {
	set doc [::dom::parse {<?xml version="1.0"?>
<Test/>
<!-- a comment in the epilogue -->
}]
    } -body {
	dom::serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE Test>
)?<Test/>(
)?<!-- a comment in the epilogue -->}

    # Test serialization of markup characters:

    variable stText "<test>LT: &lt; GT: &gt; AMP: &amp;</test>"
    test serialize-4.1 {Serialization of markup characters in #PCDATA} -constraints {!dom_libxml2} -setup {
	set doc [dom::DOMImplementation parse $stText]
    } -body {
	dom::DOMImplementation serialize [dom::document cget $doc -documentElement]
    } -cleanup $CLEANUP -result $stText

    variable stText {<tests>
<test LT='&lt;'/>
<test GT='&gt;'/>
<test AMP='&amp;'/>
<test QUOT='&quot;'/>
<test APOS='&apos;'/>
</tests>}
    # Result may use different attribute quotes.
    # Also, only the '<' and '&' are required to be escaped,
    # but its OK for others to be as well.
    test serialize-4.2 {Serialization of markup characters in attribute values} -constraints {!dom_libxml2} -match regexp -setup {
	set doc [dom::DOMImplementation parse $stText]
    } -body {
	dom::DOMImplementation serialize [dom::document cget $doc -documentElement]
    } -cleanup $CLEANUP -result {<tests>
<test LT=('|")\&lt;('|")/>
<test GT=('|")(\&gt;|>)('|")/>
<test AMP=('|")\&amp;('|")/>
<test QUOT='(\&quot;|")'/>
<test APOS="(\&apos;|')"/>
</tests>}

    # CDATA Sections may or may not be serialized using CDATA Section syntax
    variable stText \
    {<test>some plain text <![CDATA[ a CDATA section ]]> more text</test>}
    test serialize-4.3 {Preserve CDATA sections} -constraints {!dom_libxml2} -match regexp -setup {
	set doc [dom::DOMImplementation parse $stText]
    } -body {
	dom::DOMImplementation serialize [dom::document cget $doc -documentElement]
    } -cleanup $CLEANUP -result {<test>some plain text (<!\[CDATA\[)? a CDATA section (]]>)? more text</test>}

    # Test for bug #512704
    test serialize-5.1 {Serialize reserved characters in attribute value} -constraints {!dom_libxml2} -match regexp -setup {
	set doc [dom::DOMImplementation create]
	set top [dom::document createElement $doc test]
	dom::element setAttribute $top q {"all lll" <who@where.com>}
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
<!DOCTYPE test>
<test q='"all lll" &lt;who@where.com>'/>}
    test serialize-5.1 {Serialize reserved characters in attribute value} -constraints {dom_libxml2} -match regexp -setup {
	set doc [dom::DOMImplementation create]
	set top [dom::document createElement $doc test]
	dom::element setAttribute $top q {"all lll" <who@where.com>}
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=('|")1.0('|")( encoding=('|")[^'"]+('|"))?\?>
<test q=('|")&quot;all lll&quot; &lt;who@where.com&gt;('|")/>
}

    # XML Namespace support
    test serialize-6.1 {XML Namespaces - from parsed document} -match regexp -setup {
	set doc [dom::DOMImplementation parse {<t:Test xmlns:t='urn:test'><t:Value>ABC</t:Value></t:Test>
}]
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=("|')1\.0("|')( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE t:Test>
)?<t:Test xmlns:t=("|')urn:test("|')><t:Value>ABC</t:Value></t:Test>}

    test serialize-6.2 {XML Namespaces - from generated document, prefix supplied} -match regexp -setup {
	set doc [dom::DOMImplementation create]
	set de [dom::document createElementNS $doc urn:test-uri test:Test]
	dom::document createElementNS $de urn:test-uri test:Value
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=("|')1\.0("|')( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE test:Test>
)?<test:Test xmlns:test=("|')urn:test-uri("|')><test:Value/></test:Test>}

    test serialize-6.3 {XML Namespaces - from generated document, prefix not supplied} -match regexp -setup {
	set doc [dom::DOMImplementation create]
	set de [dom::document createElementNS $doc urn:test-uri Test]
	dom::document createElementNS $de urn:test-uri Value
    } -body {
	dom::DOMImplementation serialize $doc
    } -cleanup $CLEANUP -result {<\?xml version=("|')1\.0("|')( encoding=('|")[^'"]+('|"))?\?>
(<!DOCTYPE [a-zA-Z0-9]+:Test>
)?<[a-zA-Z0-9]+:Test xmlns:[a-zA-Z0-9]+=("|')urn:test-uri("|')><[a-zA-Z0-9]+:Value/></[a-zA-Z0-9]+:Test>}

    # Bug 606621
    test serialize-7.1 {Document prologue} -match regexp -setup {
	set doc [dom::parse {<?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="test_view.xsl"?>
<!DOCTYPE test SYSTEM "test.dtd">
<test />
}]
    } -body {
	dom::serialize $doc
    } -cleanup {
	dom::destroy $doc
    } -result {<\?xml version=("|')1.0("|') encoding=("|')utf-8("|') standalone=("|')no("|')\?>
<\?xml-stylesheet type=("|')text/xsl("|') href=("|')test_view.xsl("|')\?>
<!DOCTYPE test SYSTEM ("|')test.dtd("|')>
<test */>
}

    cleanupTests
}

namespace delete ::dom::serializationTest

return