tclxslt/testutils.tcl

Go to the documentation of this file.
00001 /* */
00002 /*  testutils.tcl --*/
00003 /* */
00004 /*      Auxilliary utilities for use with the tcltest package.*/
00005 /*      Author: Joe English <jenglish@flightlab.com>*/
00006 /*      Version: 1.1*/
00007 /* */
00008 /*  This file is hereby placed in the public domain.*/
00009 /* */
00010 
00011 variable tracing 0      ;/*  Set to '1' to enable the 'trace' command*/
00012 variable tracingErrors 0    ;/*  If set, 'expectError' prints error messages*/
00013 
00014 /*  ok --*/
00015 /*  Returns an empty string.*/
00016 /*  May be used as the last statement in test scripts */
00017 /*  that are only evaluated for side-effects or in cases*/
00018 /*  where you just want to make sure that an operation succeeds*/
00019 /* */
00020 ret  ok () { return {} }
00021 
00022 /*  result result --*/
00023 /*  Just returns $result*/
00024 /* */
00025 ret  result (type result) { return $result }
00026 
00027 /*  tracemsg msg --*/
00028 /*  Prints tracing message if $::tracing is nonzero.*/
00029 /* */
00030 ret  tracemsg (type string) {
00031     if {$::tracing} {
00032     puts $::tcltest::outputChannel $string
00033     }
00034 }
00035 
00036 /*  assert expr ?msg? --*/
00037 /*  Evaluates 'expr' and signals an error*/
00038 /*  if the condition is not true.*/
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 /*  expectError script  ? pattern ? --*/
00047 /*  Evaluate 'script', which is expected to fail*/
00048 /*  with an error message matching 'pattern'.*/
00049 /* */
00050 /*  Returns the error message if the script 'correctly' fails,*/
00051 /*  raises an error otherwise*/
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 /*  comparenodes*/
00067 /*  Compares two nodes, taking implementations into account*/
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 /*  nodelist list1 list2*/
00078 /*  Compares two lists of DOM nodes, in an ordered fashion.*/
00079 /*  NB. the node identities are compared, not their tokens.*/
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 /*  nodeset set1 set2*/
00094 /*  Compares two sets of DOM nodes, in an unordered fashion.*/
00095 /*  NB. the node identities are compared, not their tokens.*/
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 /*  checkTree doc list*/
00110 /*  Tests that a DOM tree has a structure specified as a Tcl list*/
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 /*  testPackage package ?version?*/
00162 /*  Loads specified package with 'package require $package $version',*/
00163 /*  then prints message describing how the package was loaded.*/
00164 /* */
00165 /*  This is useful when you've got several versions of a*/
00166 /*  package to lying around and want to make sure you're */
00167 /*  testing the right one.*/
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 /* *EOF**/
00182 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1