📄 tcltest.tcl
字号:
ConstraintInitializer unixExecs { set code 1 if {[string equal macintosh $::tcl_platform(platform)]} { set code 0 } if {[string equal windows $::tcl_platform(platform)]} { if {[catch { set file _tcl_test_remove_me.txt makeFile {hello} $file }]} { set code 0 } elseif { [catch {exec cat $file}] || [catch {exec echo hello}] || [catch {exec sh -c echo hello}] || [catch {exec wc $file}] || [catch {exec sleep 1}] || [catch {exec echo abc > $file}] || [catch {exec chmod 644 $file}] || [catch {exec rm $file}] || [llength [auto_execok mkdir]] == 0 || [llength [auto_execok fgrep]] == 0 || [llength [auto_execok grep]] == 0 || [llength [auto_execok ps]] == 0 } { set code 0 } removeFile $file } set code } ConstraintInitializer stdio { set code 0 if {![catch {set f [open "|[list [interpreter]]" w]}]} { if {![catch {puts $f exit}]} { if {![catch {close $f}]} { set code 1 } } } set code } # Deliberately call socket with the wrong number of arguments. The # error message you get will indicate whether sockets are available # on this system. ConstraintInitializer socket { catch {socket} msg string compare $msg "sockets are not available on this system" } # Check for internationalization ConstraintInitializer hasIsoLocale { if {[llength [info commands testlocale]] == 0} { set code 0 } else { set code [string length [SetIso8859_1_Locale]] RestoreLocale } set code }}###################################################################### Usage and command line arguments processing.# tcltest::PrintUsageInfo## Prints out the usage information for package tcltest. This can# be customized with the redefinition of [PrintUsageInfoHook].## Arguments:# none## Results:# none## Side Effects:# noneproc tcltest::PrintUsageInfo {} { puts [Usage] PrintUsageInfoHook}proc tcltest::Usage { {option ""} } { variable Usage variable Verify if {[llength [info level 0]] == 1} { set msg "Usage: [file tail [info nameofexecutable]] script " append msg "?-help? ?flag value? ... \n" append msg "Available flags (and valid input values) are:" set max 0 set allOpts [concat -help [Configure]] foreach opt $allOpts { set foo [Usage $opt] foreach [list x type($opt) usage($opt)] $foo break set line($opt) " $opt $type($opt) " set length($opt) [string length $line($opt)] if {$length($opt) > $max} {set max $length($opt)} } set rest [expr {72 - $max}] foreach opt $allOpts { append msg \n$line($opt) append msg [string repeat " " [expr {$max - $length($opt)}]] set u [string trim $usage($opt)] catch {append u " (default: \[[Configure $opt]])"} regsub -all {\s*\n\s*} $u " " u while {[string length $u] > $rest} { set break [string wordstart $u $rest] if {$break == 0} { set break [string wordend $u 0] } append msg [string range $u 0 [expr {$break - 1}]] set u [string trim [string range $u $break end]] append msg \n[string repeat " " $max] } append msg $u } return $msg\n } elseif {[string equal -help $option]} { return [list -help "" "Display this usage information."] } else { set type [lindex [info args $Verify($option)] 0] return [list $option $type $Usage($option)] }}# tcltest::ProcessFlags --## process command line arguments supplied in the flagArray - this# is called by processCmdLineArgs. Modifies tcltest variables# according to the content of the flagArray.## Arguments:# flagArray - array containing name/value pairs of flags## Results:# sets tcltest variables according to their values as defined by# flagArray## Side effects:# None.proc tcltest::ProcessFlags {flagArray} { # Process -help first if {[lsearch -exact $flagArray {-help}] != -1} { PrintUsageInfo exit 1 } if {[llength $flagArray] == 0} { RemoveAutoConfigureTraces } else { set args $flagArray while {[llength $args]>1 && [catch {eval configure $args} msg]} { # Something went wrong parsing $args for tcltest options # Check whether the problem is "unknown option" if {[regexp {^unknown option (\S+):} $msg -> option]} { # Could be this is an option the Hook knows about set moreOptions [processCmdLineArgsAddFlagsHook] if {[lsearch -exact $moreOptions $option] == -1} { # Nope. Report the error, including additional options, # but keep going if {[llength $moreOptions]} { append msg ", " append msg [join [lrange $moreOptions 0 end-1] ", "] append msg "or [lindex $moreOptions end]" } Warn $msg } } else { # error is something other than "unknown option" # notify user of the error; and exit puts [errorChannel] $msg exit 1 } # To recover, find that unknown option and remove up to it. # then retry while {![string equal [lindex $args 0] $option]} { set args [lrange $args 2 end] } set args [lrange $args 2 end] } if {[llength $args] == 1} { puts [errorChannel] \ "missing value for option [lindex $args 0]" exit 1 } } # Call the hook array set flag $flagArray processCmdLineArgsHook [array get flag] return}# tcltest::ProcessCmdLineArgs --## This procedure must be run after constraint initialization is# set up (by [DefineConstraintInitializers]) because some constraints# can be overridden.## Perform configuration according to the command-line options.## Arguments:# none## Results:# Sets the above-named variables in the tcltest namespace.## Side Effects:# None.#proc tcltest::ProcessCmdLineArgs {} { variable originalEnv variable testConstraints # The "argv" var doesn't exist in some cases, so use {}. if {![info exists ::argv]} { ProcessFlags {} } else { ProcessFlags $::argv } # Spit out everything you know if we're at a debug level 2 or # greater DebugPuts 2 "Flags passed into tcltest:" if {[info exists ::env(TCLTEST_OPTIONS)]} { DebugPuts 2 \ " ::env(TCLTEST_OPTIONS): $::env(TCLTEST_OPTIONS)" } if {[info exists ::argv]} { DebugPuts 2 " argv: $::argv" } DebugPuts 2 "tcltest::debug = [debug]" DebugPuts 2 "tcltest::testsDirectory = [testsDirectory]" DebugPuts 2 "tcltest::workingDirectory = [workingDirectory]" DebugPuts 2 "tcltest::temporaryDirectory = [temporaryDirectory]" DebugPuts 2 "tcltest::outputChannel = [outputChannel]" DebugPuts 2 "tcltest::errorChannel = [errorChannel]" DebugPuts 2 "Original environment (tcltest::originalEnv):" DebugPArray 2 originalEnv DebugPuts 2 "Constraints:" DebugPArray 2 testConstraints}###################################################################### Code to run the tests goes here.# tcltest::TestPuts --## Used to redefine puts in test environment. Stores whatever goes# out on stdout in tcltest::outData and stderr in errData before# sending it on to the regular puts.## Arguments:# same as standard puts## Results:# none## Side effects:# Intercepts puts; data that would otherwise go to stdout, stderr,# or file channels specified in outputChannel and errorChannel# does not get sent to the normal puts function.namespace eval tcltest::Replace { namespace export puts}proc tcltest::Replace::puts {args} { variable [namespace parent]::outData variable [namespace parent]::errData switch [llength $args] { 1 { # Only the string to be printed is specified append outData [lindex $args 0]\n return # return [Puts [lindex $args 0]] } 2 { # Either -nonewline or channelId has been specified if {[string equal -nonewline [lindex $args 0]]} { append outData [lindex $args end] return # return [Puts -nonewline [lindex $args end]] } else { set channel [lindex $args 0] set newline \n } } 3 { if {[string equal -nonewline [lindex $args 0]]} { # Both -nonewline and channelId are specified, unless # it's an error. -nonewline is supposed to be argv[0]. set channel [lindex $args 1] set newline "" } } } if {[info exists channel]} { if {[string equal $channel [[namespace parent]::outputChannel]] || [string equal $channel stdout]} { append outData [lindex $args end]$newline return } elseif {[string equal $channel [[namespace parent]::errorChannel]] || [string equal $channel stderr]} { append errData [lindex $args end]$newline return } } # If we haven't returned by now, we don't know how to handle the # input. Let puts handle it. return [eval Puts $args]}# tcltest::Eval --## Evaluate the script in the test environment. If ignoreOutput is# false, store data sent to stderr and stdout in outData and# errData. Otherwise, ignore this output altogether.## Arguments:# script Script to evaluate# ?ignoreOutput? Indicates whether or not to ignore output# sent to stdout & stderr## Results:# result from running the script## Side effects:# Empties the contents of outData and errData before running a# test if ignoreOutput is set to 0.proc tcltest::Eval {script {ignoreOutput 1}} { variable outData variable errData DebugPuts 3 "[lindex [info level 0] 0] called" if {!$ignoreOutput} { set outData {} set errData {} rename ::puts [namespace current]::Replace::Puts namespace eval :: \ [list namespace import [namespace origin Replace::puts]] namespace import Replace::puts } set result [uplevel 1 $script] if {!$ignoreOutput} { namespace forget puts namespace eval :: namespace forget puts rename [namespace current]::Replace::Puts ::puts } return $result}# tcltest::CompareStrings --## compares the expected answer to the actual answer, depending on# the mode provided. Mode determines whether a regexp, exact,# glob or custom comparison is done.## Arguments:# actual - string containing the actual result# expected - pattern to be matched against# mode - type of comparison to be done## Results:# result of the match## Side effects:# None.proc tcltest::CompareStrings {actual expected mode} { variable CustomMatch if {![info exists CustomMatch($mode)]} { return -code error "No matching command registered for `-match $mode'" } set match [namespace eval :: $CustomMatch($mode) [list $expected $actual]] if {[catch {expr {$match && $match}} result]} { return -code error "Invalid result from `-match $mode' command: $result" } return $match}# tcltest::customMatch --## registers a command to be called when a particular type of# matching is required.## Arguments:# nickname - Keyword for the type of matching# cmd - Incomplete command that implements that type of matching# when completed with expected string and actual string# and then evaluated.## Results:# None.## Side effects:# Sets the variable tcltest::CustomMatchproc tcltest::customMatch {mode script} { variable CustomMatch if {![info complete $script]} { return -code error \ "invalid customMatch script; can't evaluate after completion" } set CustomMatch($mode) $script}# tcltest::SubstArguments list## This helper function takes in a list of words, then perform a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -