aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2024-07-24 21:06:26 +1000
committerSteve Bennett <steveb@workware.net.au>2024-07-24 21:06:26 +1000
commit6a7cec0f56de3cff289431334700804bac79fb36 (patch)
treebfc7ba8b70749315edfaa19dad518165907751ab
parent7f0142e1f99ff6b9948cfe595da5e701a155e28c (diff)
downloadjimtcl-6a7cec0f56de3cff289431334700804bac79fb36.zip
jimtcl-6a7cec0f56de3cff289431334700804bac79fb36.tar.gz
jimtcl-6a7cec0f56de3cff289431334700804bac79fb36.tar.bz2
leval: add additional tests
Including error conditions. Catch break and continue invoked within leval. Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--jim.c10
-rw-r--r--tests/leval.test69
2 files changed, 77 insertions, 2 deletions
diff --git a/jim.c b/jim.c
index 1be9c5e..ef58cc4 100644
--- a/jim.c
+++ b/jim.c
@@ -11045,6 +11045,7 @@ static Jim_Obj *JimInterpolateTokens(Jim_Interp *interp, const ScriptToken * tok
Jim_Obj *sintv[JIM_EVAL_SINTV_LEN];
Jim_Obj *objPtr;
char *s;
+ const char *error_action = NULL;
if (tokens <= JIM_EVAL_SINTV_LEN)
intv = sintv;
@@ -11064,14 +11065,16 @@ static Jim_Obj *JimInterpolateTokens(Jim_Interp *interp, const ScriptToken * tok
tokens = i;
continue;
}
- /* XXX: Should probably set an error about break outside loop */
+ error_action = "break";
/* fall through to error */
case JIM_CONTINUE:
if (flags & JIM_SUBST_FLAG) {
intv[i] = NULL;
continue;
}
- /* XXX: Ditto continue outside loop */
+ if (!error_action) {
+ error_action = "continue";
+ }
/* fall through to error */
default:
while (i--) {
@@ -11080,6 +11083,9 @@ static Jim_Obj *JimInterpolateTokens(Jim_Interp *interp, const ScriptToken * tok
if (intv != sintv) {
Jim_Free(intv);
}
+ if (error_action) {
+ Jim_SetResultFormatted(interp, "invoked \"%s\" outside of a loop", error_action);
+ }
return NULL;
}
Jim_IncrRefCount(intv[i]);
diff --git a/tests/leval.test b/tests/leval.test
index 1d924d1..fed6733 100644
--- a/tests/leval.test
+++ b/tests/leval.test
@@ -56,4 +56,73 @@ test leval-1.7 {expand} -body {
}
} -result {1 2 1 2 3 4 5 6 7}
+test leval-1.8 {empty case} -body {
+ leval {
+ # Nothing
+ }
+} -result {}
+
+test leval-1.9 {backslash escapes} -body {
+ leval {
+ # char escapes
+ \r\n\t
+ # unicode escapes
+ \u00b5
+ # hex escapes
+ \x41\x42
+ }
+} -result [list \r\n\t \u00b5 AB]
+
+test leval-2.1 {error, missing [} -body {
+ leval {
+ # Missing bracket
+ [string cat
+ }
+} -returnCodes error -result {unmatched "["}
+
+test leval-2.2 {error, invalid command} -body {
+ leval {
+ a
+ [dummy]
+ b
+ }
+} -returnCodes error -result {invalid command name "dummy"}
+
+test leval-2.3 {error, unset variable} -body {
+ leval {
+ a
+ $doesnotexist
+ b
+ }
+} -returnCodes error -result {can't read "doesnotexist": no such variable}
+
+test leval-2.4 {break} -body {
+ leval {
+ a
+ [break]
+ b
+ }
+} -returnCodes error -result {invoked "break" outside of a loop}
+
+test leval-2.5 {continue} -body {
+ leval {
+ a
+ [continue]
+ b
+ }
+} -returnCodes error -result {invoked "continue" outside of a loop}
+
+test leval-3.1 {preservation of line numbers} -body {
+ set x abc
+ set src1 [info source $x]
+ set list [leval {
+ a
+ $x
+ b
+ }]
+ if {[info source [lindex $list 1]] ne [info source $x]} {
+ error "source does not match
+ }
+} -result {}
+
testreport