Trim bootstrap jimsh
[jimtcl.git] / tests / subst.test
blob353af5f8c4295211510e2002c6958b579d24f58b
1 # Commands covered:  subst
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) 1994 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-2000 Ajuba Solutions.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 # RCS: @(#) $Id: subst.test,v 1.6.2.1 2001/04/03 22:54:38 hobbs Exp $
16 source [file dirname [info script]]/testing.tcl
18 test subst-1.0 {basics} {
19     subst {\$x}
20 } "\$x"
22 test subst-1.1 {basics} {
23     list [catch {subst} msg]
24 } {1}
25 test subst-1.2 {basics} {
26     list [catch {subst a b c} msg]
27 } {1}
29 test subst-2.1 {simple strings} {
30     subst {}
31 } {}
32 test subst-2.2 {simple strings} {
33     subst a
34 } a
35 test subst-2.3 {simple strings} {
36     subst abcdefg
37 } abcdefg
39 test subst-3.1 {backslash substitutions} {
40     subst {\x\$x\[foo bar]\\}
41 } "x\$x\[foo bar]\\"
43 test subst-4.1 {variable substitutions} {
44     set a 44
45     subst {$a}
46 } {44}
47 test subst-4.2 {variable substitutions} {
48     set a 44
49     subst {x$a.y{$a}.z}
50 } {x44.y{44}.z}
51 test subst-4.3 {variable substitutions} {
52     catch {unset a}
53     set a(13) 82
54     set i 13
55     subst {x.$a($i)}
56 } {x.82}
57 catch {unset a}
58 set long {This is a very long string, intentionally made so long that it
59         will overflow the static character size for dstrings, so that
60         additional memory will have to be allocated by subst.  That way,
61         if the subst procedure forgets to free up memory while returning
62         an error, there will be memory that isn't freed (this will be
63         detected when the tests are run under a checking memory allocator
64         such as Purify).}
65 test subst-4.4 {variable substitutions} {
66     list [catch {subst {$long $a}} msg] $msg
67 } {1 {can't read "a": no such variable}}
69 test subst-5.1 {command substitutions} {
70     subst {[concat {}]}
71 } {}
72 test subst-5.2 {command substitutions} {
73     subst {[concat A test string]}
74 } {A test string}
75 test subst-5.3 {command substitutions} {
76     subst {x.[concat foo].y.[concat bar].z}
77 } {x.foo.y.bar.z}
78 test subst-5.4 {command substitutions} {
79     list [catch {subst {$long [set long] [bogus_command]}} msg] $msg
80 } {1 {invalid command name "bogus_command"}}
82 test subst-6.1 {clear the result after command substitution} {
83     catch {unset a}
84     list [catch {subst {[concat foo] $a}} msg] $msg
85 } {1 {can't read "a": no such variable}}
87 test subst-7.1 {switches} {
88     list [catch {subst foo bar} msg]
89 } {1}
90 test subst-7.2 {switches} {
91     list [catch {subst -no bar} msg]
92 } {1}
93 test subst-7.3 {switches} {
94     list [catch {subst -bogus bar} msg]
95 } {1}
96 test subst-7.4 {switches} {
97     set x 123
98     subst -nobackslashes {abc $x [expr 1+2] \\\x41}
99 } {abc 123 3 \\\x41}
100 test subst-7.5 {switches} {
101     set x 123
102     subst -nocommands {abc $x [expr 1+2] \\\x41}
103 } {abc 123 [expr 1+2] \A}
104 test subst-7.6 {switches} {
105     set x 123
106     subst -novariables {abc $x [expr 1+2] \\\x41}
107 } {abc $x 3 \A}
108 test subst-7.7 {switches} {
109     set x 123
110     subst -nov -nob -noc {abc $x [expr 1+2] \\\x41}
111 } {abc $x [expr 1+2] \\\x41}
113 test subst-8.1 {return in a subst} {
114     subst {foo [return {x}; bogus code] bar}
115 } {foo x bar}
116 test subst-8.2 {return in a subst} {
117     subst {foo [return x ; bogus code] bar}
118 } {foo x bar}
119 test subst-8.3 {return in a subst} {
120     subst {foo [if 1 { return {x}; bogus code }] bar}
121 } {foo x bar}
122 test subst-8.4 {return in a subst} {
123     subst {[eval {return hi}] there}
124 } {hi there}
125 test subst-8.5 {return in a subst} {
126     subst {foo [return {]}; bogus code] bar}
127 } {foo ] bar}
129 test subst-9.1 {error in a subst} {
130     list [catch {subst {[error foo; bogus code]bar}} msg] $msg
131 } {1 foo}
132 test subst-9.2 {error in a subst} {
133     list [catch {subst {[if 1 { error foo; bogus code}]bar}} msg] $msg
134 } {1 foo}
136 test subst-10.1 {break in a subst} {
137     subst {foo [break; bogus code] bar}
138 } {foo }
139 test subst-10.2 {break in a subst} {
140     subst {foo [break; return x; bogus code] bar}
141 } {foo }
142 test subst-10.3 {break in a subst} {
143     subst {foo [if 1 { break; bogus code}] bar}
144 } {foo }
145 test subst-10.4 {break in a subst, parse error} {
146     subst {foo [break ; set a {}{} ; stuff] bar}
147 } {foo }
148 test subst-10.5 {break in a subst, parse error} {
149     subst {foo [break ;set bar baz ;set a {}{} ; stuff] bar}
150 } {foo }
152 test subst-11.1 {continue in a subst} {
153     subst {foo [continue; bogus code] bar}
154 } {foo  bar}
155 test subst-11.2 {continue in a subst} {
156     subst {foo [continue; return x; bogus code] bar}
157 } {foo  bar}
158 test subst-11.3 {continue in a subst} {
159     subst {foo [if 1 { continue; bogus code}] bar}
160 } {foo  bar}
162 test subst-12.1 {lone $} {
163     subst {$}
164 } {$}
166 test subst-12.2 {lone $} {
167     set a 1
168     subst -novar {${a}}
169 } {${a}}
171 test subst-12.3 {variable inside [] with -noc} {
172     set a 1
173     subst -noc {x[join $a]y}
174 } {x[join 1]y}
177 # cleanup
178 testreport