3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
7 # Copyright (c) 1996 Sun Microsystems, Inc.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 if {[string compare test [info procs test]] == 1} then {source defs}
16 # Basic "if" operation.
19 test if-1.1 {TclCompileIfCmd: missing if/elseif test} {
20 list [catch {if} msg] $msg
21 } {1 {wrong # args: no expression after "if" argument}}
22 test if-1.2 {TclCompileIfCmd: error in if/elseif test} {
23 list [catch {if {[error "error in condition"]} foo} msg] $msg
24 } {1 {error in condition}}
25 test if-1.3 {TclCompileIfCmd: error in if/elseif test} {
26 list [catch {if {1+}} msg] $msg $errorInfo
27 } {1 {syntax error in expression "1+"} {syntax error in expression "1+"
28 ("if" test expression)
31 test if-1.4 {TclCompileIfCmd: if/elseif test in braces} {
36 test if-1.5 {TclCompileIfCmd: if/elseif test not in braces} {
41 test if-1.6 {TclCompileIfCmd: multiline test expr} {
43 if {($tcl_platform(platform) != "foobar1") && \
44 ($tcl_platform(platform) != "foobar2")} {set a 3} else {set a 4}
47 test if-1.7 {TclCompileIfCmd: "then" after if/elseif test} {
52 test if-1.8 {TclCompileIfCmd: keyword other than "then" after if/elseif test} {
54 catch {if 1<2 therefore {set a 1}} msg
56 } {invalid command name "therefore"}
57 test if-1.9 {TclCompileIfCmd: missing "then" body} {
59 catch {if 1<2 then} msg
61 } {wrong # args: no script following "then" argument}
62 test if-1.10 {TclCompileIfCmd: error in "then" body} {
64 list [catch {if {$a!="xxx"} then {set}} msg] $msg $errorInfo
65 } {1 {wrong # args: should be "set varName ?newValue?"} {wrong # args: should be "set varName ?newValue?"
68 ("if" then script line 1)
70 "if {$a!="xxx"} then {set}"}}
71 test if-1.11 {TclCompileIfCmd: error in "then" body} {
72 list [catch {if 2 then {[error "error in then clause"]}} msg] $msg
73 } {1 {error in then clause}}
74 test if-1.12 {TclCompileIfCmd: "then" body in quotes} {
79 test if-1.13 {TclCompileIfCmd: computed "then" body} {
84 set x2 {; append a x2}
89 test if-1.14 {TclCompileIfCmd: taking proper branch} {
94 test if-1.15 {TclCompileIfCmd: taking proper branch} {
99 test if-1.16 {TclCompileIfCmd: test jumpFalse instruction replacement after long "then" body} {
104 while {$a != "xxx"} {
107 if {[string compare $a "bar"] < 0} {
111 if {[string compare $a "bar"] < 0} {
115 if {[string compare $a "bar"] < 0} {
119 if {[string compare $a "bar"] < 0} {
127 while {$a != "xxx"} {
130 if {[string compare $a "bar"] < 0} {
134 if {[string compare $a "bar"] < 0} {
138 if {[string compare $a "bar"] < 0} {
142 if {[string compare $a "bar"] < 0} {
153 test if-1.17 {TclCompileIfCmd: if/elseif test in quotes} {
155 list [catch {if {"0 < 3"} {set a 1}} msg] $msg
156 } {1 {expected boolean value but got "0 < 3"}}
159 test if-2.1 {TclCompileIfCmd: "elseif" after if/elseif test} {
161 if 3>4 {set a 1} elseif 1 {set a 2}
164 # Since "else" is optional, the "elwood" below is treated as a command.
165 # But then there shouldn't be any additional argument words for the "if".
166 test if-2.2 {TclCompileIfCmd: keyword other than "elseif"} {
168 catch {if 1<2 {set a 1} elwood {set a 2}} msg
170 } {wrong # args: extra words after "else" clause in "if" command}
171 test if-2.3 {TclCompileIfCmd: missing expression after "elseif"} {
173 catch {if 1<2 {set a 1} elseif} msg
175 } {wrong # args: no expression after "elseif" argument}
176 test if-2.4 {TclCompileIfCmd: error in expression after "elseif"} {
178 list [catch {if 3>4 {set a 1} elseif {1>}} msg] $msg $errorInfo
179 } {1 {syntax error in expression "1>"} {syntax error in expression "1>"
180 ("if" test expression)
182 "if 3>4 {set a 1} elseif {1>}"}}
183 test if-2.5 {TclCompileIfCmd: test jumpFalse instruction replacement after long "elseif" body} {
188 while {$a != "xxx"} {
191 if {[string compare $a "bar"] < 0} {
195 if {[string compare $a "bar"] < 0} {
199 if {[string compare $a "bar"] < 0} {
203 if {[string compare $a "bar"] < 0} {
211 while {$a != "xxx"} {
214 if {[string compare $a "bar"] < 0} {
218 if {[string compare $a "bar"] < 0} {
222 if {[string compare $a "bar"] < 0} {
226 if {[string compare $a "bar"] < 0} {
234 } elseif 1<2 then { #; this if arm should be taken
236 while {$a != "xxx"} {
239 if {[string compare $a "bar"] < 0} {
243 if {[string compare $a "bar"] < 0} {
247 if {[string compare $a "bar"] < 0} {
251 if {[string compare $a "bar"] < 0} {
259 while {$a != "xxx"} {
262 if {[string compare $a "bar"] < 0} {
266 if {[string compare $a "bar"] < 0} {
270 if {[string compare $a "bar"] < 0} {
274 if {[string compare $a "bar"] < 0} {
286 test if-3.1 {TclCompileIfCmd: "else" clause} {
288 if 3>4 {set a 1} elseif {$a == "foo"} {set a 2} else {set a 3}
291 # Since "else" is optional, the "elsex" below is treated as a command.
292 # But then there shouldn't be any additional argument words for the "if".
293 test if-3.2 {TclCompileIfCmd: keyword other than "else"} {
295 catch {if 1<2 then {set a 1} elsex {set a 2}} msg
297 } {wrong # args: extra words after "else" clause in "if" command}
298 test if-3.3 {TclCompileIfCmd: missing body after "else"} {
300 catch {if 2<1 {set a 1} else} msg
302 } {wrong # args: no script following "else" argument}
303 test if-3.4 {TclCompileIfCmd: error compiling body after "else"} {
305 catch {if 2<1 {set a 1} else {set}} msg
307 } {wrong # args: should be "set varName ?newValue?"
310 ("if" else script line 1)
312 "if 2<1 {set a 1} else {set}"}
313 test if-3.5 {TclCompileIfCmd: extra arguments after "else" argument} {
315 catch {if 2<1 {set a 1} else {set a 2} or something} msg
317 } {wrong # args: extra words after "else" clause in "if" command}
318 # The following test also checks whether contained loops and other
319 # commands are properly relocated because a short jump must be replaced
320 # by a "long distance" one.
321 test if-3.6 {TclCompileIfCmd: test jumpFalse instruction replacement after long "else" clause} {
326 while {$a != "xxx"} {
329 if {[string compare $a "bar"] < 0} {
333 if {[string compare $a "bar"] < 0} {
337 if {[string compare $a "bar"] < 0} {
341 if {[string compare $a "bar"] < 0} {
349 while {$a != "xxx"} {
352 if {[string compare $a "bar"] < 0} {
356 if {[string compare $a "bar"] < 0} {
360 if {[string compare $a "bar"] < 0} {
364 if {[string compare $a "bar"] < 0} {
372 } elseif 1==2 then { #; this if arm should be taken
374 while {$a != "xxx"} {
377 if {[string compare $a "bar"] < 0} {
381 if {[string compare $a "bar"] < 0} {
385 if {[string compare $a "bar"] < 0} {
389 if {[string compare $a "bar"] < 0} {
397 while {$a != "xxx"} {
400 if {[string compare $a "bar"] < 0} {
404 if {[string compare $a "bar"] < 0} {
408 if {[string compare $a "bar"] < 0} {
412 if {[string compare $a "bar"] < 0} {
422 while {$a != "xxx"} {
425 if {[string compare $a "bar"] < 0} {
429 if {[string compare $a "bar"] < 0} {
433 if {[string compare $a "bar"] < 0} {
437 if {[string compare $a "bar"] < 0} {
445 while {$a != "xxx"} {
448 if {[string compare $a "bar"] < 0} {
452 if {[string compare $a "bar"] < 0} {
456 if {[string compare $a "bar"] < 0} {
460 if {[string compare $a "bar"] < 0} {
472 test if-4.1 {TclCompileIfCmd: "if" command result} {
474 set a [if 3<4 {set i 27}]
477 test if-4.2 {TclCompileIfCmd: "if" command result} {
479 set a [if 3>4 {set i 27}]
482 test if-4.3 {TclCompileIfCmd: "if" command result} {
484 set a [if 0 {set i 1} elseif 1 {set i 2}]
487 test if-4.4 {TclCompileIfCmd: "if" command result} {
489 set a [if 0 {set i 1} elseif 0 {set i 2} elseif 2>5 {set i 3} else {set i 4}]
492 test if-4.5 {TclCompileIfCmd: return value} {
493 if 0 then {set a 22; concat abc} elseif 1 {concat def} {concat ghi}
496 # Check "if" and computed command names.
498 test if-5.1 {if and computed command names} {