On tcl tests - how to interpret tcltest::test

不打扰是莪最后的温柔 提交于 2019-12-25 08:36:42

问题


I am looking at the tests in TCL source tree and I see this one in compExpr-old.test:

test compExpr-old-14.17 {CompilePrimaryExpr: string primary that looks like var ref} {
    expr $
} $

It looks wrong to me: the test runs the script expr $ and expects the return value is "$". Is my interpretation right?

It cannot be right because expr $ is wrong syntactically.

I checked out tcltest.tcl, the definition of tcltest::test is so long, wish someone can help me over here.


回答1:


I don't know what version of the test suite you are looking at (probably some 8.4 variant?), but when I look at the whole of that test in the Tcl trunk, I see this:

test compExpr-old-14.17 {CompilePrimaryExpr: string primary that looks like var ref} -body {
    expr $
} -returnCodes error -match glob -result *

In this case, it is checking that the result is an error and that the value of the result (i.e., the content of the error message) glob matches with *, i.e., is anything (effectively ignoring it). That is, the test checks that an error is obtained from expr $ and otherwise doesn't care.

The test you posted (which uses an older syntax for tcltest) won't pass on modern versions of Tcl. But in 8.4, it did pass; this was an area where Tcl's semantics changed between 8.4 and 8.5:

dkf$ tclsh8.4
% expr $
$
% exit
dkf$ tclsh8.5
% expr $
invalid character "$"
in expression "$"
% exit

Quick guide to Tcltest test cases: The -body describes a scrip to run, the -returnCode can be used to select whether normal results or errors are expected, the -result can be used to say what string to expect as a result of the body script, and -match can be used to pick an alternate matching scheme than the default (exact string equality). There's also -constraints for specifying preconditions on a test, -setup for doing setup code, and -cleanup for cleanup code. The two leading mandatory arguments are the name of the test (which must be unique within a test suite for your own sanity) and a short description of the test, used in reporting of failures.

In the old syntax (used in much of Tcl's test suite because updating it is a ton of boring work), you instead had the same two mandatory arguments, then an optional list of constraints (as for -constraints), then a mandatory body (as for -body), then the mandatory string to match for equality (as for -result). Less flexible, but not too hard to understand.



来源:https://stackoverflow.com/questions/40490079/on-tcl-tests-how-to-interpret-tcltesttest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!