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} {
22 test subst-1.1 {basics} {
23 list [catch {subst} msg]
25 test subst-1.2 {basics} {
26 list [catch {subst a b c} msg]
29 test subst-2.1 {simple strings} {
32 test subst-2.2 {simple strings} {
35 test subst-2.3 {simple strings} {
39 test subst-3.1 {backslash substitutions} {
40 subst {\x\$x\[foo bar]\\}
43 test subst-4.1 {variable substitutions} {
47 test subst-4.2 {variable substitutions} {
51 test subst-4.3 {variable substitutions} {
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
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} {
72 test subst-5.2 {command substitutions} {
73 subst {[concat A test string]}
75 test subst-5.3 {command substitutions} {
76 subst {x.[concat foo].y.[concat 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} {
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]
90 test subst-7.2 {switches} {
91 list [catch {subst -no bar} msg]
93 test subst-7.3 {switches} {
94 list [catch {subst -bogus bar} msg]
96 test subst-7.4 {switches} {
98 subst -nobackslashes {abc $x [expr 1+2] \\\x41}
100 test subst-7.5 {switches} {
102 subst -nocommands {abc $x [expr 1+2] \\\x41}
103 } {abc 123 [expr 1+2] \A}
104 test subst-7.6 {switches} {
106 subst -novariables {abc $x [expr 1+2] \\\x41}
108 test subst-7.7 {switches} {
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}
116 test subst-8.2 {return in a subst} {
117 subst {foo [return x ; bogus code] bar}
119 test subst-8.3 {return in a subst} {
120 subst {foo [if 1 { return {x}; bogus code }] bar}
122 test subst-8.4 {return in a subst} {
123 subst {[eval {return hi}] there}
125 test subst-8.5 {return in a subst} {
126 subst {foo [return {]}; bogus code] bar}
129 test subst-9.1 {error in a subst} {
130 list [catch {subst {[error foo; bogus code]bar}} msg] $msg
132 test subst-9.2 {error in a subst} {
133 list [catch {subst {[if 1 { error foo; bogus code}]bar}} msg] $msg
136 test subst-10.1 {break in a subst} {
137 subst {foo [break; bogus code] bar}
139 test subst-10.2 {break in a subst} {
140 subst {foo [break; return x; bogus code] bar}
142 test subst-10.3 {break in a subst} {
143 subst {foo [if 1 { break; bogus code}] bar}
146 # Note that unlike Tcl, Jim throws an error for these two before
148 test subst-10.4 {break in a subst, parse error} tcl {
149 subst {foo [break ; set a {}{} ; stuff] bar}
151 test subst-10.5 {break in a subst, parse error} tcl {
152 subst {foo [break ;set bar baz ;set a {}{} ; stuff] bar}
155 test subst-11.1 {continue in a subst} {
156 subst {foo [continue; bogus code] bar}
158 test subst-11.2 {continue in a subst} {
159 subst {foo [continue; return x; bogus code] bar}
161 test subst-11.3 {continue in a subst} {
162 subst {foo [if 1 { continue; bogus code}] bar}
165 test subst-12.1 {lone $} {
169 test subst-12.2 {lone $} {
174 test subst-12.3 {variable inside [] with -noc} {
176 subst -noc {x[join $a]y}