tclxslt/testutils.tcl
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 variable tracing 0      ;
00012 variable tracingErrors 0    ;
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 ret  ok () { return {} }
00021 
00022 
00023 
00024 
00025 ret  result (type result) { return $result }
00026 
00027 
00028 
00029 
00030 ret  tracemsg (type string) {
00031     if {$::tracing} {
00032     puts $::tcltest::outputChannel $string
00033     }
00034 }
00035 
00036 
00037 
00038 
00039 
00040 ret  assert (type expr , optional message ="") {
00041     if {![uplevel 1 [list expr $expr]]} {
00042     return -code error "Assertion {$expr} failed:\n$message"
00043     }
00044 }
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 ret  expectError (type script , optional pattern ="*") {
00054     set rc [catch [list uplevel 1 $script] result]
00055     if {$::tracingErrors} {
00056     puts stderr "==> [string replace $result 70 end ...]"
00057     }
00058     set rmsg [string replace $result 40 end ...]
00059     if {$rc != 1} {
00060     return -code error \
00061         "Expected error, got '$rmsg' (rc=$rc)"
00062     }
00063     return $result
00064 }
00065 
00066 
00067 
00068 
00069 ret  comparenodes (type node1 , type node2) {
00070     if {[::tcltest::testConstraint dom_libxml2] || [::tcltest::testConstraint dom_tcl]} {
00071     ::dom::node isSameNode $node1 $node2
00072     } else {
00073     return [expr ![string compare $node1 $node2]]
00074     }
00075 }
00076 
00077 
00078 
00079 
00080 
00081 ret  nodelist (type list1 , type list2) {
00082     if {[llength $list1] != [llength $list2]} {
00083     return 0
00084     }
00085     foreach node1 $list1 node2 $list2 {
00086     if {![comparenodes $node1 $node2]} {
00087         return 0
00088     }
00089     }
00090     return 1
00091 }
00092 
00093 
00094 
00095 
00096 
00097 ret  nodeset (type set1 , type set2) {
00098     if {[llength $set1] != [llength $set2]} {
00099     return 0
00100     }
00101     foreach node1 [lsort $set1] node2 [lsort $set2] {
00102     if {![comparenodes $node1 $node2]} {
00103         return 0
00104     }
00105     }
00106     return 1
00107 }
00108 
00109 
00110 
00111 
00112 ret  checkTree (type node , type spec) {
00113     foreach child [dom::node children $node] specchild $spec {
00114     switch [lindex $specchild 0] {
00115         element {
00116         if {[dom::node cget $child -nodeType] != "element"} {
00117             return 0
00118         }
00119         if {[dom::node cget $child -nodeName] != [lindex $specchild 1]} {
00120             return 0
00121         }
00122         foreach {name value} [lindex $specchild 2] {
00123             if {[dom::element getAttribute $child $name] != $value} {
00124             return 0
00125             }
00126         }
00127         set result [checkTree $child [lindex $specchild 3]]
00128         if {!$result} {
00129             return 0
00130         }
00131         }
00132         pi {
00133         if {[dom::node cget $child -nodeType] != "processingInstruction"} {
00134             return 0
00135         }
00136         if {[dom::node cget $child -nodeName] != [lindex $specchild 1]} {
00137             return 0
00138         }
00139         }
00140         dtd {
00141         if {[dom::node cget $child -nodeType] != "dtd"} {
00142             return 0
00143         }
00144         }
00145         text {
00146         if {[dom::node cget $child -nodeType] != "textNode"} {
00147             return 0
00148         }
00149         if {[dom::node cget $child -nodeValue] != [lindex $specchild 1]} {
00150             return 0
00151         }
00152         }
00153         default {
00154         }
00155     }
00156     }
00157 
00158     return 1
00159 }
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 ret  testPackage (type package , optional version ="") {
00171     if {![catch "package present $package $version"]} { return }
00172     set rc [catch "package require $package $version" result]
00173     if {$rc} { return -code $rc $result }
00174     set version $result
00175     set loadScript [package ifneeded $package $version]
00176     puts $::tcltest::outputChannel \
00177     "Loaded $package version $version via {$loadScript}"
00178     return;
00179 }
00180 
00181 
00182