📄 trace.test
字号:
# Commands covered: trace## This file contains a collection of tests for one or more of the Tcl# built-in commands. Sourcing this file into Tcl runs the tests and# generates output for errors. No output means no errors were found.## Copyright (c) 1991-1993 The Regents of the University of California.# Copyright (c) 1994 Sun Microsystems, Inc.## See the file "license.terms" for information on usage and redistribution# of this file, and for a DISCLAIMER OF ALL WARRANTIES.## SCCS: @(#) trace.test 1.27 97/07/23 17:08:38if {[string compare test [info procs test]] == 1} then {source defs}proc traceScalar {name1 name2 op} { global info set info [list $name1 $name2 $op [catch {uplevel set $name1} msg] $msg]}proc traceScalarAppend {name1 name2 op} { global info lappend info $name1 $name2 $op [catch {uplevel set $name1} msg] $msg}proc traceArray {name1 name2 op} { global info set info [list $name1 $name2 $op [catch {uplevel set [set name1]($name2)} msg] $msg]}proc traceArray2 {name1 name2 op} { global info set info [list $name1 $name2 $op]}proc traceProc {name1 name2 op} { global info set info [concat $info [list $name1 $name2 $op]]}proc traceTag {tag args} { global info set info [concat $info $tag]}proc traceError {args} { error "trace returned error"}proc traceCheck {cmd args} { global info set info [list [catch $cmd msg] $msg]}proc traceCrtElement {value name1 name2 op} { uplevel set ${name1}($name2) $value}# Read-tracing on variablestest trace-1.1 {trace variable reads} { catch {unset x} set info {} trace var x r traceScalar list [catch {set x} msg] $msg $info} {1 {can't read "x": no such variable} {x {} r 1 {can't read "x": no such variable}}}test trace-1.2 {trace variable reads} { catch {unset x} set x 123 set info {} trace var x r traceScalar list [catch {set x} msg] $msg $info} {0 123 {x {} r 0 123}}test trace-1.3 {trace variable reads} { catch {unset x} set info {} trace var x r traceScalar set x 123 set info} {}test trace-1.4 {trace array element reads} { catch {unset x} set info {} trace var x(2) r traceArray list [catch {set x(2)} msg] $msg $info} {1 {can't read "x(2)": no such element in array} {x 2 r 1 {can't read "x(2)": no such element in array}}}test trace-1.5 {trace array element reads} { catch {unset x} set x(2) zzz set info {} trace var x(2) r traceArray list [catch {set x(2)} msg] $msg $info} {0 zzz {x 2 r 0 zzz}}test trace-1.6 {trace array element reads} { catch {unset x} set info {} trace variable x r traceArray2 proc p {} { global x set x(2) willi return $x(2) } list [catch {p} msg] $msg $info} {0 willi {x 2 r}}test trace-1.7 {trace array element reads, create element undefined if nonexistant} { catch {unset x} set info {} trace variable x r q proc q {name1 name2 op} { global info set info [list $name1 $name2 $op] global $name1 set ${name1}($name2) wolf } proc p {} { global x set x(X) willi return $x(Y) } list [catch {p} msg] $msg $info} {0 wolf {x Y r}}test trace-1.8 {trace reads on whole arrays} { catch {unset x} set info {} trace var x r traceArray list [catch {set x(2)} msg] $msg $info} {1 {can't read "x(2)": no such variable} {}}test trace-1.9 {trace reads on whole arrays} { catch {unset x} set x(2) zzz set info {} trace var x r traceArray list [catch {set x(2)} msg] $msg $info} {0 zzz {x 2 r 0 zzz}}test trace-1.10 {trace variable reads} { catch {unset x} set x 444 set info {} trace var x r traceScalar unset x set info} {}# Basic write-tracing on variablestest trace-2.1 {trace variable writes} { catch {unset x} set info {} trace var x w traceScalar set x 123 set info} {x {} w 0 123}test trace-2.2 {trace writes to array elements} { catch {unset x} set info {} trace var x(33) w traceArray set x(33) 444 set info} {x 33 w 0 444}test trace-2.3 {trace writes on whole arrays} { catch {unset x} set info {} trace var x w traceArray set x(abc) qq set info} {x abc w 0 qq}test trace-2.4 {trace variable writes} { catch {unset x} set x 1234 set info {} trace var x w traceScalar set x set info} {}test trace-2.5 {trace variable writes} { catch {unset x} set x 1234 set info {} trace var x w traceScalar unset x set info} {}# append no longer triggers read traces when fetching the old values of# variables before doing the append operation. However, lappend _does_# still trigger these read traces. Also lappend triggers only one write# trace: after appending all arguments to the list.test trace-3.1 {trace variable read-modify-writes} { catch {unset x} set info {} trace var x r traceScalarAppend append x 123 append x 456 lappend x 789 set info} {x {} r 0 123456}test trace-3.2 {trace variable read-modify-writes} { catch {unset x} set info {} trace var x rw traceScalarAppend append x 123 lappend x 456 set info} {x {} w 0 123 x {} r 0 123 x {} w 0 {123 456}}# Basic unset-tracing on variablestest trace-4.1 {trace variable unsets} { catch {unset x} set info {} trace var x u traceScalar catch {unset x} set info} {x {} u 1 {can't read "x": no such variable}}test trace-4.2 {variable mustn't exist during unset trace} { catch {unset x} set x 1234 set info {} trace var x u traceScalar unset x set info} {x {} u 1 {can't read "x": no such variable}}test trace-4.3 {unset traces mustn't be called during reads and writes} { catch {unset x} set info {} trace var x u traceScalar set x 44 set x set info} {}test trace-4.4 {trace unsets on array elements} { catch {unset x} set x(0) 18 set info {} trace var x(1) u traceArray catch {unset x(1)} set info} {x 1 u 1 {can't read "x(1)": no such element in array}}test trace-4.5 {trace unsets on array elements} { catch {unset x} set x(1) 18 set info {} trace var x(1) u traceArray unset x(1) set info} {x 1 u 1 {can't read "x(1)": no such element in array}}test trace-4.6 {trace unsets on array elements} { catch {unset x} set x(1) 18 set info {} trace var x(1) u traceArray unset x set info} {x 1 u 1 {can't read "x(1)": no such variable}}test trace-4.7 {trace unsets on whole arrays} { catch {unset x} set x(1) 18 set info {} trace var x u traceProc catch {unset x(0)} set info} {}test trace-4.8 {trace unsets on whole arrays} { catch {unset x} set x(1) 18 set x(2) 144 set x(3) 14 set info {} trace var x u traceProc unset x(1) set info} {x 1 u}test trace-4.9 {trace unsets on whole arrays} { catch {unset x} set x(1) 18 set x(2) 144 set x(3) 14 set info {} trace var x u traceProc unset x set info} {x {} u}# Trace multiple trace types at once.test trace-5.1 {multiple ops traced at once} { catch {unset x} set info {} trace var x rwu traceProc catch {set x} set x 22 set x set x 33 unset x set info} {x {} r x {} w x {} r x {} w x {} u}test trace-5.2 {multiple ops traced on array element} { catch {unset x} set info {} trace var x(0) rwu traceProc catch {set x(0)} set x(0) 22 set x(0) set x(0) 33 unset x(0) unset x set info} {x 0 r x 0 w x 0 r x 0 w x 0 u}test trace-5.3 {multiple ops traced on whole array} { catch {unset x} set info {} trace var x rwu traceProc catch {set x(0)} set x(0) 22 set x(0) set x(0) 33 unset x(0) unset x set info} {x 0 w x 0 r x 0 w x 0 u x {} u}# Check order of invocation of tracestest trace-6.1 {order of invocation of traces} { catch {unset x} set info {} trace var x r "traceTag 1" trace var x r "traceTag 2" trace var x r "traceTag 3" catch {set x} set x 22 set x set info} {3 2 1 3 2 1}test trace-6.2 {order of invocation of traces} { catch {unset x} set x(0) 44 set info {} trace var x(0) r "traceTag 1" trace var x(0) r "traceTag 2" trace var x(0) r "traceTag 3" set x(0) set info} {3 2 1}test trace-6.3 {order of invocation of traces} { catch {unset x} set x(0) 44 set info {} trace var x(0) r "traceTag 1" trace var x r "traceTag A1" trace var x(0) r "traceTag 2" trace var x r "traceTag A2" trace var x(0) r "traceTag 3" trace var x r "traceTag A3" set x(0) set info} {A3 A2 A1 3 2 1}# Check effects of errors in trace procedurestest trace-7.1 {error returns from traces} { catch {unset x} set x 123 set info {} trace var x r "traceTag 1" trace var x r traceError list [catch {set x} msg] $msg $info} {1 {can't read "x": trace returned error} {}}test trace-7.2 {error returns from traces} { catch {unset x} set x 123 set info {} trace var x w "traceTag 1" trace var x w traceError list [catch {set x 44} msg] $msg $info} {1 {can't set "x": trace returned error} {}}test trace-7.3 {error returns from traces} { catch {unset x} set x 123 set info {} trace var x w traceError list [catch {append x 44} msg] $msg $info} {1 {can't set "x": trace returned error} {}}test trace-7.4 {error returns from traces} { catch {unset x} set x 123 set info {} trace var x u "traceTag 1" trace var x u traceError list [catch {unset x} msg] $msg $info} {0 {} 1}test trace-7.5 {error returns from traces} { catch {unset x} set x(0) 123 set info {} trace var x(0) r "traceTag 1" trace var x r "traceTag 2" trace var x r traceError trace var x r "traceTag 3" list [catch {set x(0)} msg] $msg $info} {1 {can't read "x(0)": trace returned error} 3}test trace-7.6 {error returns from traces} { catch {unset x} set x 123 trace var x u traceError list [catch {unset x} msg] $msg} {0 {}}test trace-7.7 {error returns from traces} { # This test just makes sure that the memory for the error message # gets deallocated correctly when the trace is invoked again or # when the trace is deleted. catch {unset x} set x 123 trace var x r traceError catch {set x} catch {set x} trace vdelete x r traceError} {}# Check to see that variables are expunged before trace# procedures are invoked, so trace procedure can even manipulate# a new copy of the variables.test trace-8.1 {be sure variable is unset before trace is called} { catch {unset x} set x 33 set info {} trace var x u {traceCheck {uplevel set x}} unset x set info} {1 {can't read "x": no such variable}}test trace-8.2 {be sure variable is unset before trace is called} { catch {unset x} set x 33 set info {} trace var x u {traceCheck {uplevel set x 22}} unset x concat $info [list [catch {set x} msg] $msg]} {0 22 0 22}test trace-8.3 {be sure traces are cleared before unset trace called} { catch {unset x} set x 33 set info {} trace var x u {traceCheck {uplevel trace vinfo x}} unset x set info} {0 {}}test trace-8.4 {set new trace during unset trace} { catch {unset x} set x 33 set info {} trace var x u {traceCheck {global x; trace var x u traceProc}} unset x concat $info [trace vinfo x]} {0 {} {u traceProc}}test trace-9.1 {make sure array elements are unset before traces are called} { catch {unset x} set x(0) 33 set info {} trace var x(0) u {traceCheck {uplevel set x(0)}} unset x(0) set info} {1 {can't read "x(0)": no such element in array}}test trace-9.2 {make sure array elements are unset before traces are called} { catch {unset x} set x(0) 33 set info {} trace var x(0) u {traceCheck {uplevel set x(0) zzz}} unset x(0) concat $info [list [catch {set x(0)} msg] $msg]} {0 zzz 0 zzz}test trace-9.3 {array elements are unset before traces are called} { catch {unset x} set x(0) 33 set info {} trace var x(0) u {traceCheck {global x; trace vinfo x(0)}} unset x(0) set info} {0 {}}test trace-9.4 {set new array element trace during unset trace} { catch {unset x} set x(0) 33 set info {} trace var x(0) u {traceCheck {uplevel {trace variable x(0) r {}}}} catch {unset x(0)} concat $info [trace vinfo x(0)]} {0 {} {r {}}}test trace-10.1 {make sure arrays are unset before traces are called} {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -