問題があります。問題は、最初のスクリプトでsig
は、への呼び出しが終了すると消えるローカル変数です。test
後から調べることはできません。たまたま、 の結果はtest
に割り当てられた値sig
です。テスト目的でそれを当てにできるかどうかはわかりません。これで十分な場合は、これを行うことができます (Tcl 8.5 を使用していると仮定します。8.4 では、apply
用語の代わりにヘルパー プロシージャが必要です)。
source first.tcl
trace add execution test leave {apply {{cmd code result op} {
# Copy the result of [test] to the global sig variable
global sig
set sig $result
}}}
これは (アスペクト指向プログラミングと同様に) の結果をインターセプトし、グローバル変数test
に保存します。ただし、テストされたコードの問題に対しては正しくありません。代入は変数への代入であり、その直後に消えます。 sig
多くのテストを行っている場合は、tcltest を使用して作業を行うことを検討してください。これは、Tcl 自体をテストするために使用されるパッケージであり、スクリプトの実行結果のテストを非常に簡単に記述できます。
# Setup of test harness
package require tcltest
source first.tcl
# The tests
tcltest::test test-1.1 {check if larger} -body {
test 10
} -result 1
tcltest::test test-1.2 {check if smaller} -body {
test 5
} -result 0
# Produce the final report
tcltest::cleanupTests