1# Commands covered:  if
2#
3# This file contains the original set of tests for Tcl's if command.
4# Since the if command is now compiled, a new set of tests covering
5# the new implementation is in the file "if.test". Sourcing this file
6# into Tcl runs the tests and generates output for errors.
7# No output means no errors were found.
8#
9# Copyright © 1991-1993 The Regents of the University of California.
10# Copyright © 1994-1996 Sun Microsystems, Inc.
11# Copyright © 1998-1999 Scriptics Corporation.
12#
13# See the file "license.terms" for information on usage and redistribution
14# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
16if {"::tcltest" ni [namespace children]} {
17    package require tcltest 2.5
18    namespace import -force ::tcltest::*
19}
20
21test if-old-1.1 {taking proper branch} {
22    set a {}
23    if 0 {set a 1} else {set a 2}
24    set a
25} 2
26test if-old-1.2 {taking proper branch} {
27    set a {}
28    if 1 {set a 1} else {set a 2}
29    set a
30} 1
31test if-old-1.3 {taking proper branch} {
32    set a {}
33    if 1<2 {set a 1}
34    set a
35} 1
36test if-old-1.4 {taking proper branch} {
37    set a {}
38    if 1>2 {set a 1}
39    set a
40} {}
41test if-old-1.5 {taking proper branch} {
42    set a {}
43    if 0 {set a 1} else {}
44    set a
45} {}
46test if-old-1.6 {taking proper branch} {
47    set a {}
48    if 0 {set a 1} elseif 1 {set a 2} elseif 1 {set a 3} else {set a 4}
49    set a
50} {2}
51test if-old-1.7 {taking proper branch} {
52    set a {}
53    if 0 {set a 1} elseif 0 {set a 2} elseif 1 {set a 3} else {set a 4}
54    set a
55} {3}
56test if-old-1.8 {taking proper branch} {
57    set a {}
58    if 0 {set a 1} elseif 0 {set a 2} elseif 0 {set a 3} else {set a 4}
59    set a
60} {4}
61test if-old-1.9 {taking proper branch, multiline test expr} {
62    set a {}
63    if {($tcl_platform(platform) != "foobar1") && \
64	($tcl_platform(platform) != "foobar2")} {set a 3} else {set a 4}
65    set a
66} {3}
67
68
69test if-old-2.1 {optional then-else args} {
70    set a 44
71    if 0 then {set a 1} elseif 0 then {set a 3} else {set a 2}
72    set a
73} 2
74test if-old-2.2 {optional then-else args} {
75    set a 44
76    if 1 then {set a 1} else {set a 2}
77    set a
78} 1
79test if-old-2.3 {optional then-else args} {
80    set a 44
81    if 0 {set a 1} else {set a 2}
82    set a
83} 2
84test if-old-2.4 {optional then-else args} {
85    set a 44
86    if 1 {set a 1} else {set a 2}
87    set a
88} 1
89test if-old-2.5 {optional then-else args} {
90    set a 44
91    if 0 then {set a 1} {set a 2}
92    set a
93} 2
94test if-old-2.6 {optional then-else args} {
95    set a 44
96    if 1 then {set a 1} {set a 2}
97    set a
98} 1
99test if-old-2.7 {optional then-else args} {
100    set a 44
101    if 0 then {set a 1} else {set a 2}
102    set a
103} 2
104test if-old-2.8 {optional then-else args} {
105    set a 44
106    if 0 then {set a 1} elseif 0 {set a 2} elseif 0 {set a 3} {set a 4}
107    set a
108} 4
109
110test if-old-3.1 {return value} {
111    if 1 then {set a 22; concat abc}
112} abc
113test if-old-3.2 {return value} {
114    if 0 then {set a 22; concat abc} elseif 1 {concat def} {concat ghi}
115} def
116test if-old-3.3 {return value} {
117    if 0 then {set a 22; concat abc} else {concat def}
118} def
119test if-old-3.4 {return value} {
120    if 0 then {set a 22; concat abc}
121} {}
122test if-old-3.5 {return value} {
123    if 0 then {set a 22; concat abc} elseif 0 {concat def}
124} {}
125
126test if-old-4.1 {error conditions} {
127    list [catch {if} msg] $msg
128} {1 {wrong # args: no expression after "if" argument}}
129test if-old-4.2 {error conditions} {
130    list [catch {if {[error "error in condition"]} foo} msg] $msg
131} {1 {error in condition}}
132test if-old-4.3 {error conditions} {
133    list [catch {if 2} msg] $msg
134} {1 {wrong # args: no script following "2" argument}}
135test if-old-4.4 {error conditions} {
136    list [catch {if 2 then} msg] $msg
137} {1 {wrong # args: no script following "then" argument}}
138test if-old-4.5 {error conditions} {
139    list [catch {if 2 the} msg] $msg
140} {1 {invalid command name "the"}}
141test if-old-4.6 {error conditions} {
142    list [catch {if 2 then {[error "error in then clause"]}} msg] $msg
143} {1 {error in then clause}}
144test if-old-4.7 {error conditions} {
145    list [catch {if 0 then foo elseif} msg] $msg
146} {1 {wrong # args: no expression after "elseif" argument}}
147test if-old-4.8 {error conditions} {
148    list [catch {if 0 then foo elsei} msg] $msg
149} {1 {invalid command name "elsei"}}
150test if-old-4.9 {error conditions} {
151    list [catch {if 0 then foo elseif 0 bar else} msg] $msg
152} {1 {wrong # args: no script following "else" argument}}
153test if-old-4.10 {error conditions} {
154    list [catch {if 0 then foo elseif 0 bar els} msg] $msg
155} {1 {invalid command name "els"}}
156test if-old-4.11 {error conditions} {
157    list [catch {if 0 then foo elseif 0 bar else {[error "error in else clause"]}} msg] $msg
158} {1 {error in else clause}}
159
160# cleanup
161::tcltest::cleanupTests
162return
163