2 set testinfo
(verbose
) 0
3 set testinfo
(numpass
) 0
4 set testinfo
(stoponerror
) 0
5 set testinfo
(numfail
) 0
6 set testinfo
(numskip
) 0
7 set testinfo
(numtests
) 0
8 set testinfo
(failed
) {}
10 set testdir
[file dirname
[info script
]]
11 set bindir
[file dirname
[info nameofexecutable
]]
13 if {[lsearch $argv "-verbose"] >= 0 ||
[info exists env
(testverbose
)]} {
14 incr testinfo
(verbose
)
16 if {[lsearch $argv "-stoponerror"] >= 0 ||
[info exists env
(stoponerror
)]} {
17 incr testinfo
(stoponerror
)
20 proc needs
{type what
{packages
{}}} {
21 if {$type eq
"constraint"} {
22 if {![info exists
::tcltest::testConstraints($what)]} {
23 set ::tcltest::testConstraints($what) 0
25 if {![set ::tcltest::testConstraints($what)]} {
26 skiptest
" (constraint $what)"
31 # Does it exist already?
32 if {[info commands
$what] ne
""} {
35 if {$packages eq
""} {
36 # e.g. exec command is in exec package
40 catch {package require
$p}
42 if {[info commands
$what] ne
""} {
45 skiptest
" (command $what)"
47 error "Unknown needs type: $type"
50 proc skiptest
{{msg
{}}} {
51 puts [format "%16s: --- skipped$msg" $::argv0]
55 # If tcl, just use tcltest
56 if {[catch {info version
}]} {
57 package require
Tcl 8.5
58 package require
tcltest 2.1
59 namespace import
tcltest::*
61 if {$testinfo(verbose
)} {
62 configure
-verbose bps
67 ::tcltest::cleanupTests
72 lappend auto_path
$testdir $bindir [file dirname
[pwd]]
74 # For Jim, this is reasonable compatible tcltest
75 proc makeFile
{contents name
} {
77 stdout
puts "About to 'puts $f $contents'"
83 proc removeFile
{name
} {
87 # In case tclcompat is not selected
88 if {![exists
-proc puts]} {
89 proc puts {{-nonewline {}} {chan stdout
} msg
} {
90 if {${-nonewline} ni
{-nonewline {}}} {
91 ${-nonewline} puts $msg
93 $chan puts {*}${-nonewline} $msg
96 proc close {chan args
} {
99 proc fileevent {args
} {
104 proc script_source
{script
} {
105 lassign
[info source $script] f l
112 proc error_source
{} {
113 lassign
[info stacktrace
] p f l
120 proc package-or-skip
{name
} {
122 package require
$name
124 puts [format "%16s: --- skipped" $::argv0]
129 proc testConstraint
{constraint bool
} {
130 set ::tcltest::testConstraints($constraint) $bool
133 testConstraint
{utf8
} [expr {[string length
"\xc2\xb5"] == 1}]
134 testConstraint
{references
} [expr {[info commands ref
] ne
""}]
135 testConstraint
{jim
} 1
136 testConstraint
{tcl
} 0
138 proc bytestring
{x
} {
142 # Note: We don't support -output or -errorOutput yet
143 proc test
{id descr args
} {
144 set a
[dict create
-returnCodes {ok
return} -match exact
-result {} -constraints {} -body {} -setup {} -cleanup {}]
145 if {[lindex $args 0] ni
[dict keys
$a]} {
146 if {[llength $args] == 2} {
147 lassign
$args body result constraints
148 } elseif
{[llength $args] == 3} {
149 lassign
$args constraints body result
151 return -code error "$id: Wrong syntax for tcltest::test v1"
153 tailcall test
$id $descr -body $body -result $result -constraints $constraints
155 # tcltest::test v2 syntax
158 incr ::testinfo(numtests
)
159 if {$::testinfo(verbose
)} {
160 puts -nonewline "$id "
163 foreach c
$a(-constraints) {
164 if {[info exists
::tcltest::testConstraints($c)]} {
165 if {$::tcltest::testConstraints($c)} {
168 incr ::testinfo(numskip
)
169 if {$::testinfo(verbose
)} {
176 catch {uplevel 1 $a(-setup)}
177 set rc
[catch {uplevel 1 $a(-body)} result opts
]
178 catch {uplevel 1 $a(-cleanup)}
180 if {[info return $rc] ni
$a(-returnCodes) && $rc ni
$a(-returnCodes)} {
182 set expected
"rc=$a(-returnCodes) result=$a(-result)"
183 set result
"rc=[info return $rc] result=$result"
185 if {$a(-match) eq
"exact"} {
186 set ok
[string equal
$a(-result) $result]
187 } elseif
{$a(-match) eq
"glob"} {
188 set ok
[string match
$a(-result) $result]
189 } elseif
{$a(-match) eq
"regexp"} {
190 set ok
[regexp $a(-result) $result]
192 return -code error "$id: unknown match type: $a(-match)"
194 set expected
$a(-result)
198 if {$::testinfo(verbose
)} {
201 incr ::testinfo(numpass
)
205 if {!$::testinfo(verbose
)} {
206 puts -nonewline "$id "
210 set source [script_source
$a(-body)]
212 set source [error_source
]
214 puts "Expected: '$expected'"
215 puts "Got : '$result'"
217 incr ::testinfo(numfail
)
218 lappend ::testinfo(failed
) [list $id $descr $source $expected $result]
219 if {$::testinfo(stoponerror
)} {
224 proc ::tcltest::cleanupTests {} {
229 if {$::testinfo(verbose
)} {
230 puts -nonewline "\n$::argv0"
232 puts -nonewline [format "%16s" $::argv0]
234 puts [format ": Total %5d Passed %5d Skipped %5d Failed %5d" \
235 $::testinfo(numtests
) $::testinfo(numpass
) $::testinfo(numskip
) $::testinfo(numfail
)]
236 if {$::testinfo(numfail
)} {
237 puts [string repeat
- 60]
238 puts "FAILED: $::testinfo(numfail)"
239 foreach failed
$::testinfo(failed
) {
240 foreach {id descr
source expected result
} $failed {}
243 puts [string repeat
- 60]
245 if {$::testinfo(numfail
)} {
251 error "deliberate error"
254 if {$testinfo(verbose
)} {
255 puts "==== $argv0 ===="