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
|
# Features covered: stacked handler, tdom command
#
# This file contains a collection of tests for using several handler scripts
# for an event, and the mixed usage of tcl handler scripts and C coded
# parser extensions.
#
# Copyright (c) 2002 Rolf Ade.
#
# RCS: @(#) $Id$
source [file join [file dir [info script]] loadtdom.tcl]
proc Count {args} {
if {![info exists ::count]} {
set ::count 1
} else {
incr ::count
}
}
proc CharCount {name args} {
if {![info exists ::charcount]} {
set ::charcount [string length $name]
} else {
incr ::charcount [string length $name]
}
}
proc CDataHandler {data} {
if {![info exists ::cdata]} {
set ::cdata [string length $data]
} else {
incr ::cdata [string length $data]
}
}
catch {unset started}
proc Start {name atList} {
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"
}
return {
return -code return
}
default {
return -code $atts(class)
}
}
}
}
test stackedhdl-1.1 {two handlers for element start} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat -elementstartcommand Count \
-handlerset charcount -elementstartcommand CharCount]
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {3 6}
test stackedhdl-1.2 {two handlers for element start} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat]
$p configure -elementstartcommand Count
$p configure -handlerset charcount -elementstartcommand CharCount
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {3 6}
test stackedhdl-1.3 {two handlers for element start} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat]
$p configure -handlerset charcount -elementstartcommand CharCount
$p configure -elementstartcommand Count
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {3 6}
test stackedhdl-1.4 {two handlers for element start and end} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat -elementstartcommand Count -elementendcommand Count \
-handlerset charcount -elementstartcommand CharCount \
-elementendcommand CharCount]
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {6 12}
test stackedhdl-1.5 {two handlers for element start} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat]
$p configure -elementstartcommand Count -elementendcommand Count
$p configure -handlerset charcount -elementstartcommand CharCount \
-elementendcommand CharCount
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {6 12}
test stackedhdl-1.6 {two handlers for element start} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat]
$p configure -handlerset charcount -elementstartcommand CharCount \
-elementendcommand CharCount
$p configure -elementstartcommand Count -elementendcommand Count
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {6 12}
test stackedhdl-1.7 {two handlers for element start} {
catch {unset ::count}
catch {unset ::charcount}
set p [expat]
$p configure -handlerset charcount -elementstartcommand CharCount
$p configure -elementstartcommand Count
$p configure -handlerset charcount -elementendcommand CharCount
$p configure -elementendcommand Count
$p parse {<root><a/><a></a></root>}
list $::count $::charcount
} {6 12}
test stackedhdl-1.8 {same handler script for the same event in differen handler sets} {
catch {unset ::count}
set p [expat -elementstartcommand Count \
-handlerset charcount -elementstartcommand Count]
$p parse {<root><a/><a></a></root>}
set ::count
} {6}
test stackedhdl-1.9 {same handler script for the same event in differen handler sets} {
catch {unset ::count}
set p [expat -elementendcommand Count \
-handlerset charcount -elementendcommand Count]
$p parse {<root><a/><a></a></root>}
set ::count
} {6}
test stackedhdl-1.10 {lots of handler sets, without default handler set} {
catch {unset ::count}
catch {unset ::charcount}
catch {unset ::cdata}
set p [expat]
for {set x 0} {$x < 100} {incr x} {
$p configure -handlerset set$x -elementstartcommand Count \
-elementendcommand CharCount \
-characterdatacommand CDataHandler
}
$p parse {<root> <a/><a></a></root>}
list $::count $::charcount $::cdata
} {300 600 200}
test stackedhdl-2.1 {tcl handler and C coded parser extension} {
catch {unset ::count}
set p [expat -elementstartcommand Count]
tdom $p enable
$p parse {<root><a/><a>boo</a></root>}
set doc [tdom $p getdoc]
set root [$doc documentElement]
set result [list $::count [llength [$root childNodes]]]
$doc delete
set result
} {3 2}
test stackedhdl-2.2 {tcl handler and C coded parser extension} {
catch {unset ::count}
set p [expat -elementstartcommand Count]
tdom $p enable
$p parse {<root><a/><a>boo</a></root>}
set doc [tdom $p getdoc]
$p free
set root [$doc documentElement]
set result [list $::count [llength [$root childNodes]]]
$doc delete
set result
} {3 2}
test stackedhdl-2.3 {return -code return with tcl and C coded handler} -setup {
catch {unset started}
} -body {
set p [expat -elementstartcommand Start]
tdom $p enable
set resultcode [catch {$p parse {<doc><e/><e class="return"/><e/></doc>}}]
set result [list $resultcode $::started(e) [[tdom $p getdoc] asXML -indent none]]
$p free
set result
} -result {0 2 {<doc><e/><e class="return"/></doc>}}
test stackedhdl-2.4 {return -code error with tcl and C coded handler} -setup {
catch {unset started}
} -body {
set p [expat -elementstartcommand Start]
tdom $p enable
set resultcode [catch {$p parse {<doc><e/><e class="error"/><e/></doc>}} msg]
set result [list $resultcode $msg $::started(e) [[tdom $p getdoc] asXML -indent none]]
$p free
set result
} -result {1 {error condition in callback} 2 {<doc><e/><e class="error"/></doc>}}
test stackedhdl-2.5 {return -code return with tcl and C coded handler} -setup {
catch {unset started}
} -body {
set p [expat -elementstartcommand Start]
tdom $p enable
set resultcode [catch {$p parse {<doc><e/><e class="return"/><e/></doc>}}]
set result [list $resultcode $::started(e) [[tdom $p getdoc] asXML -indent none]]
$p reset
catch {unset started}
set resultcode [catch {$p parse {<doc><e/><e/><e/></doc>}}]
lappend result $resultcode $::started(e) [[tdom $p getdoc] asXML -indent none]
$p free
set result
} -result {0 2 {<doc><e/><e class="return"/></doc>} 0 3 <doc><e/><e/><e/></doc>}
test stackedhdl-2.6 {return -code error with tcl and C coded handler} -setup {
catch {unset started}
} -body {
set p [expat -elementstartcommand Start]
tdom $p enable
set resultcode [catch {$p parse {<doc><e/><e class="error"/><e/></doc>}} msg]
set result [list $resultcode $msg $::started(e) [[tdom $p getdoc] asXML -indent none]]
$p reset
catch {unset started}
set resultcode [catch {$p parse {<doc><e/><e/><e/></doc>}}]
lappend result $resultcode $::started(e) [[tdom $p getdoc] asXML -indent none]
$p free
set result
} -result {1 {error condition in callback} 2 {<doc><e/><e class="error"/></doc>} 0 3 <doc><e/><e/><e/></doc>}
test stackedhdl-3.1 {don't request the DOM tree from a tdom enabled parser} {
set p [expat]
tdom $p enable
$p parse {<root><a/><a>boo</a></root>}
$p free
} {}
foreach parser [info commands xmlparser*] {
$parser free
}
# cleanup
::tcltest::cleanupTests
return
|