sl@0
|
1 |
# Commands covered: switch
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# This file contains a collection of tests for one or more of the Tcl
|
sl@0
|
4 |
# built-in commands. Sourcing this file into Tcl runs the tests and
|
sl@0
|
5 |
# generates output for errors. No output means no errors were found.
|
sl@0
|
6 |
#
|
sl@0
|
7 |
# Copyright (c) 1993 The Regents of the University of California.
|
sl@0
|
8 |
# Copyright (c) 1994 Sun Microsystems, Inc.
|
sl@0
|
9 |
# Copyright (c) 1998-1999 by Scriptics Corporation.
|
sl@0
|
10 |
#
|
sl@0
|
11 |
# See the file "license.terms" for information on usage and redistribution
|
sl@0
|
12 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# RCS: @(#) $Id: switch.test,v 1.7 2001/11/27 13:30:54 dkf Exp $
|
sl@0
|
15 |
|
sl@0
|
16 |
if {[lsearch [namespace children] ::tcltest] == -1} {
|
sl@0
|
17 |
package require tcltest
|
sl@0
|
18 |
namespace import -force ::tcltest::*
|
sl@0
|
19 |
}
|
sl@0
|
20 |
|
sl@0
|
21 |
test switch-1.1 {simple patterns} {
|
sl@0
|
22 |
switch a a {format 1} b {format 2} c {format 3} default {format 4}
|
sl@0
|
23 |
} 1
|
sl@0
|
24 |
test switch-1.2 {simple patterns} {
|
sl@0
|
25 |
switch b a {format 1} b {format 2} c {format 3} default {format 4}
|
sl@0
|
26 |
} 2
|
sl@0
|
27 |
test switch-1.3 {simple patterns} {
|
sl@0
|
28 |
switch x a {format 1} b {format 2} c {format 3} default {format 4}
|
sl@0
|
29 |
} 4
|
sl@0
|
30 |
test switch-1.4 {simple patterns} {
|
sl@0
|
31 |
switch x a {format 1} b {format 2} c {format 3}
|
sl@0
|
32 |
} {}
|
sl@0
|
33 |
test switch-1.5 {simple pattern matches many times} {
|
sl@0
|
34 |
switch b a {format 1} b {format 2} b {format 3} b {format 4}
|
sl@0
|
35 |
} 2
|
sl@0
|
36 |
test switch-1.6 {simple patterns} {
|
sl@0
|
37 |
switch default a {format 1} default {format 2} c {format 3} default {format 4}
|
sl@0
|
38 |
} 2
|
sl@0
|
39 |
test switch-1.7 {simple patterns} {
|
sl@0
|
40 |
switch x a {format 1} default {format 2} c {format 3} default {format 4}
|
sl@0
|
41 |
} 4
|
sl@0
|
42 |
|
sl@0
|
43 |
test switch-2.1 {single-argument form for pattern/command pairs} {
|
sl@0
|
44 |
switch b {
|
sl@0
|
45 |
a {format 1}
|
sl@0
|
46 |
b {format 2}
|
sl@0
|
47 |
default {format 6}
|
sl@0
|
48 |
}
|
sl@0
|
49 |
} {2}
|
sl@0
|
50 |
test switch-2.2 {single-argument form for pattern/command pairs} {
|
sl@0
|
51 |
list [catch {switch z {a 2 b}} msg] $msg
|
sl@0
|
52 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
53 |
|
sl@0
|
54 |
test switch-3.1 {-exact vs. -glob vs. -regexp} {
|
sl@0
|
55 |
switch -exact aaaab {
|
sl@0
|
56 |
^a*b$ {concat regexp}
|
sl@0
|
57 |
*b {concat glob}
|
sl@0
|
58 |
aaaab {concat exact}
|
sl@0
|
59 |
default {concat none}
|
sl@0
|
60 |
}
|
sl@0
|
61 |
} exact
|
sl@0
|
62 |
test switch-3.2 {-exact vs. -glob vs. -regexp} {
|
sl@0
|
63 |
switch -regexp aaaab {
|
sl@0
|
64 |
^a*b$ {concat regexp}
|
sl@0
|
65 |
*b {concat glob}
|
sl@0
|
66 |
aaaab {concat exact}
|
sl@0
|
67 |
default {concat none}
|
sl@0
|
68 |
}
|
sl@0
|
69 |
} regexp
|
sl@0
|
70 |
test switch-3.3 {-exact vs. -glob vs. -regexp} {
|
sl@0
|
71 |
switch -glob aaaab {
|
sl@0
|
72 |
^a*b$ {concat regexp}
|
sl@0
|
73 |
*b {concat glob}
|
sl@0
|
74 |
aaaab {concat exact}
|
sl@0
|
75 |
default {concat none}
|
sl@0
|
76 |
}
|
sl@0
|
77 |
} glob
|
sl@0
|
78 |
test switch-3.4 {-exact vs. -glob vs. -regexp} {
|
sl@0
|
79 |
switch aaaab {^a*b$} {concat regexp} *b {concat glob} \
|
sl@0
|
80 |
aaaab {concat exact} default {concat none}
|
sl@0
|
81 |
} exact
|
sl@0
|
82 |
test switch-3.5 {-exact vs. -glob vs. -regexp} {
|
sl@0
|
83 |
switch -- -glob {
|
sl@0
|
84 |
^g.*b$ {concat regexp}
|
sl@0
|
85 |
-* {concat glob}
|
sl@0
|
86 |
-glob {concat exact}
|
sl@0
|
87 |
default {concat none}
|
sl@0
|
88 |
}
|
sl@0
|
89 |
} exact
|
sl@0
|
90 |
test switch-3.6 {-exact vs. -glob vs. -regexp} {
|
sl@0
|
91 |
list [catch {switch -foo a b c} msg] $msg
|
sl@0
|
92 |
} {1 {bad option "-foo": must be -exact, -glob, -regexp, or --}}
|
sl@0
|
93 |
|
sl@0
|
94 |
test switch-4.1 {error in executed command} {
|
sl@0
|
95 |
list [catch {switch a a {error "Just a test"} default {format 1}} msg] \
|
sl@0
|
96 |
$msg $errorInfo
|
sl@0
|
97 |
} {1 {Just a test} {Just a test
|
sl@0
|
98 |
while executing
|
sl@0
|
99 |
"error "Just a test""
|
sl@0
|
100 |
("a" arm line 1)
|
sl@0
|
101 |
invoked from within
|
sl@0
|
102 |
"switch a a {error "Just a test"} default {format 1}"}}
|
sl@0
|
103 |
test switch-4.2 {error: not enough args} {
|
sl@0
|
104 |
list [catch {switch} msg] $msg
|
sl@0
|
105 |
} {1 {wrong # args: should be "switch ?switches? string pattern body ... ?default body?"}}
|
sl@0
|
106 |
test switch-4.3 {error: pattern with no body} {
|
sl@0
|
107 |
list [catch {switch a b} msg] $msg
|
sl@0
|
108 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
109 |
test switch-4.4 {error: pattern with no body} {
|
sl@0
|
110 |
list [catch {switch a b {format 1} c} msg] $msg
|
sl@0
|
111 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
112 |
test switch-4.5 {error in default command} {
|
sl@0
|
113 |
list [catch {switch foo a {error switch1} b {error switch 3} \
|
sl@0
|
114 |
default {error switch2}} msg] $msg $errorInfo
|
sl@0
|
115 |
} {1 switch2 {switch2
|
sl@0
|
116 |
while executing
|
sl@0
|
117 |
"error switch2"
|
sl@0
|
118 |
("default" arm line 1)
|
sl@0
|
119 |
invoked from within
|
sl@0
|
120 |
"switch foo a {error switch1} b {error switch 3} default {error switch2}"}}
|
sl@0
|
121 |
|
sl@0
|
122 |
test switch-5.1 {errors in -regexp matching} {
|
sl@0
|
123 |
list [catch {switch -regexp aaaab {
|
sl@0
|
124 |
*b {concat glob}
|
sl@0
|
125 |
aaaab {concat exact}
|
sl@0
|
126 |
default {concat none}
|
sl@0
|
127 |
}} msg] $msg
|
sl@0
|
128 |
} {1 {couldn't compile regular expression pattern: quantifier operand invalid}}
|
sl@0
|
129 |
|
sl@0
|
130 |
test switch-6.1 {backslashes in patterns} {
|
sl@0
|
131 |
switch -exact {\a\$\.\[} {
|
sl@0
|
132 |
\a\$\.\[ {concat first}
|
sl@0
|
133 |
\a\\$\.\\[ {concat second}
|
sl@0
|
134 |
\\a\\$\\.\\[ {concat third}
|
sl@0
|
135 |
{\a\\$\.\\[} {concat fourth}
|
sl@0
|
136 |
{\\a\\$\\.\\[} {concat fifth}
|
sl@0
|
137 |
default {concat none}
|
sl@0
|
138 |
}
|
sl@0
|
139 |
} third
|
sl@0
|
140 |
test switch-6.2 {backslashes in patterns} {
|
sl@0
|
141 |
switch -exact {\a\$\.\[} {
|
sl@0
|
142 |
\a\$\.\[ {concat first}
|
sl@0
|
143 |
{\a\$\.\[} {concat second}
|
sl@0
|
144 |
{{\a\$\.\[}} {concat third}
|
sl@0
|
145 |
default {concat none}
|
sl@0
|
146 |
}
|
sl@0
|
147 |
} second
|
sl@0
|
148 |
|
sl@0
|
149 |
test switch-7.1 {"-" bodies} {
|
sl@0
|
150 |
switch a {
|
sl@0
|
151 |
a -
|
sl@0
|
152 |
b -
|
sl@0
|
153 |
c {concat 1}
|
sl@0
|
154 |
default {concat 2}
|
sl@0
|
155 |
}
|
sl@0
|
156 |
} 1
|
sl@0
|
157 |
test switch-7.2 {"-" bodies} {
|
sl@0
|
158 |
list [catch {
|
sl@0
|
159 |
switch a {
|
sl@0
|
160 |
a -
|
sl@0
|
161 |
b -
|
sl@0
|
162 |
c -
|
sl@0
|
163 |
}
|
sl@0
|
164 |
} msg] $msg
|
sl@0
|
165 |
} {1 {no body specified for pattern "c"}}
|
sl@0
|
166 |
test switch-7.3 {"-" bodies} {
|
sl@0
|
167 |
list [catch {
|
sl@0
|
168 |
switch a {
|
sl@0
|
169 |
a -
|
sl@0
|
170 |
b -foo
|
sl@0
|
171 |
c -
|
sl@0
|
172 |
}
|
sl@0
|
173 |
} msg] $msg
|
sl@0
|
174 |
} {1 {no body specified for pattern "c"}}
|
sl@0
|
175 |
|
sl@0
|
176 |
test switch-8.1 {empty body} {
|
sl@0
|
177 |
set msg {}
|
sl@0
|
178 |
switch {2} {
|
sl@0
|
179 |
1 {set msg 1}
|
sl@0
|
180 |
2 {}
|
sl@0
|
181 |
default {set msg 2}
|
sl@0
|
182 |
}
|
sl@0
|
183 |
} {}
|
sl@0
|
184 |
|
sl@0
|
185 |
test switch-9.1 {empty pattern/body list} {
|
sl@0
|
186 |
list [catch {switch x} msg] $msg
|
sl@0
|
187 |
} {1 {wrong # args: should be "switch ?switches? string pattern body ... ?default body?"}}
|
sl@0
|
188 |
test switch-9.2 {empty pattern/body list} {
|
sl@0
|
189 |
list [catch {switch -- x} msg] $msg
|
sl@0
|
190 |
} {1 {wrong # args: should be "switch ?switches? string pattern body ... ?default body?"}}
|
sl@0
|
191 |
test switch-9.3 {empty pattern/body list} {
|
sl@0
|
192 |
list [catch {switch x {}} msg] $msg
|
sl@0
|
193 |
} {1 {wrong # args: should be "switch ?switches? string {pattern body ... ?default body?}"}}
|
sl@0
|
194 |
test switch-9.4 {empty pattern/body list} {
|
sl@0
|
195 |
list [catch {switch -- x {}} msg] $msg
|
sl@0
|
196 |
} {1 {wrong # args: should be "switch ?switches? string {pattern body ... ?default body?}"}}
|
sl@0
|
197 |
test switch-9.5 {unpaired pattern} {
|
sl@0
|
198 |
list [catch {switch x a {} b} msg] $msg
|
sl@0
|
199 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
200 |
test switch-9.6 {unpaired pattern} {
|
sl@0
|
201 |
list [catch {switch x {a {} b}} msg] $msg
|
sl@0
|
202 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
203 |
test switch-9.7 {unpaired pattern} {
|
sl@0
|
204 |
list [catch {switch x a {} # comment b} msg] $msg
|
sl@0
|
205 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
206 |
test switch-9.8 {unpaired pattern} {
|
sl@0
|
207 |
list [catch {switch x {a {} # comment b}} msg] $msg
|
sl@0
|
208 |
} {1 {extra switch pattern with no body, this may be due to a comment incorrectly placed outside of a switch body - see the "switch" documentation}}
|
sl@0
|
209 |
test switch-9.9 {unpaired pattern} {
|
sl@0
|
210 |
list [catch {switch x a {} x {} # comment b} msg] $msg
|
sl@0
|
211 |
} {1 {extra switch pattern with no body}}
|
sl@0
|
212 |
test switch-9.10 {unpaired pattern} {
|
sl@0
|
213 |
list [catch {switch x {a {} x {} # comment b}} msg] $msg
|
sl@0
|
214 |
} {1 {extra switch pattern with no body, this may be due to a comment incorrectly placed outside of a switch body - see the "switch" documentation}}
|
sl@0
|
215 |
|
sl@0
|
216 |
# cleanup
|
sl@0
|
217 |
::tcltest::cleanupTests
|
sl@0
|
218 |
return
|