summaryrefslogtreecommitdiffstats
path: root/tests/subst.test
blob: c6d93444ad3e96140caa5358be1593f7f4c83dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Commands covered:  subst
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1994 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
# Copyright (c) 1998-2000 Ajuba Solutions.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: subst.test,v 1.13.2.5 2004/10/26 20:54:52 dgp Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

test subst-1.1 {basics} {
    list [catch {subst} msg] $msg
} {1 {wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"}}
test subst-1.2 {basics} {
    list [catch {subst a b c} msg] $msg
} {1 {bad switch "a": must be -nobackslashes, -nocommands, or -novariables}}

test subst-2.1 {simple strings} {
    subst {}
} {}
test subst-2.2 {simple strings} {
    subst a
} a
test subst-2.3 {simple strings} {
    subst abcdefg
} abcdefg
test subst-2.4 {simple strings} {
    # Tcl Bug 685106
    subst [bytestring bar\x00soom]
} [bytestring bar\x00soom]

test subst-3.1 {backslash substitutions} {
    subst {\x\$x\[foo bar]\\}
} "x\$x\[foo bar]\\"
test subst-3.2 {backslash substitutions with utf chars} {
    # 'j' is just a char that doesn't mean anything, and \344 is 'ä'
    # that also doesn't mean anything, but is multi-byte in UTF-8.
    list [subst \j] [subst \\j] [subst \\344] [subst \\\344]
} "j j \344 \344"

test subst-4.1 {variable substitutions} {
    set a 44
    subst {$a}
} {44}
test subst-4.2 {variable substitutions} {
    set a 44
    subst {x$a.y{$a}.z}
} {x44.y{44}.z}
test subst-4.3 {variable substitutions} {
    catch {unset a}
    set a(13) 82
    set i 13
    subst {x.$a($i)}
} {x.82}
catch {unset a}
set long {This is a very long string, intentionally made so long that it
	will overflow the static character size for dstrings, so that
	additional memory will have to be allocated by subst.  That way,
	if the subst procedure forgets to free up memory while returning
	an error, there will be memory that isn't freed (this will be
	detected when the tests are run under a checking memory allocator
	such as Purify).}
test subst-4.4 {variable substitutions} {
    list [catch {subst {$long $a}} msg] $msg
} {1 {can't read "a": no such variable}}

test subst-5.1 {command substitutions} {
    subst {[concat {}]}
} {}
test subst-5.2 {command substitutions} {
    subst {[concat A test string]}
} {A test string}
test subst-5.3 {command substitutions} {
    subst {x.[concat foo].y.[concat bar].z}
} {x.foo.y.bar.z}
test subst-5.4 {command substitutions} {
    list [catch {subst {$long [set long] [bogus_command]}} msg] $msg
} {1 {invalid command name "bogus_command"}}
test subst-5.5 {command substitutions} {
    set a 0
    list [catch {subst {[set a 1}} msg] $a $msg 
} {1 0 {missing close-bracket}}
test subst-5.6 {command substitutions} {
    set a 0
    list [catch {subst {0[set a 1}} msg] $a $msg 
} {1 0 {missing close-bracket}}
test subst-5.7 {command substitutions} {
    set a 0
    list [catch {subst {0[set a 1; set a 2}} msg] $a $msg 
} {1 1 {missing close-bracket}}

# repeat the tests above simulating cmd line input
test subst-5.8 {command substitutions} {
    set script {[subst {[set a 1}]}
    list [catch {exec [info nameofexecutable] << $script} msg] $msg 
} {1 {missing close-bracket}}
test subst-5.9 {command substitutions} {
    set script {[subst {0[set a 1}]}
    list [catch {exec [info nameofexecutable] << $script} msg] $msg 
} {1 {missing close-bracket}}
test subst-5.10 {command substitutions} {
    set script {[subst {0[set a 1; set a 2}]}
    list [catch {exec [info nameofexecutable] << $script} msg] $msg 
} {1 {missing close-bracket}}

test subst-6.1 {clear the result after command substitution} {
    catch {unset a}
    list [catch {subst {[concat foo] $a}} msg] $msg
} {1 {can't read "a": no such variable}}

test subst-7.1 {switches} {
    list [catch {subst foo bar} msg] $msg
} {1 {bad switch "foo": must be -nobackslashes, -nocommands, or -novariables}}
test subst-7.2 {switches} {
    list [catch {subst -no bar} msg] $msg
} {1 {ambiguous switch "-no": must be -nobackslashes, -nocommands, or -novariables}}
test subst-7.3 {switches} {
    list [catch {subst -bogus bar} msg] $msg
} {1 {bad switch "-bogus": must be -nobackslashes, -nocommands, or -novariables}}
test subst-7.4 {switches} {
    set x 123
    subst -nobackslashes {abc $x [expr 1+2] \\\x41}
} {abc 123 3 \\\x41}
test subst-7.5 {switches} {
    set x 123
    subst -nocommands {abc $x [expr 1+2] \\\x41}
} {abc 123 [expr 1+2] \A}
test subst-7.6 {switches} {
    set x 123
    subst -novariables {abc $x [expr 1+2] \\\x41}
} {abc $x 3 \A}
test subst-7.7 {switches} {
    set x 123
    subst -nov -nob -noc {abc $x [expr 1+2] \\\x41}
} {abc $x [expr 1+2] \\\x41}

test subst-8.1 {return in a subst} {
    subst {foo [return {x}; bogus code] bar}
} {foo x bar}
test subst-8.2 {return in a subst} {
    subst {foo [return x ; bogus code] bar}
} {foo x bar}
test subst-8.3 {return in a subst} {
    subst {foo [if 1 { return {x}; bogus code }] bar}
} {foo x bar}
test subst-8.4 {return in a subst} {
    subst {[eval {return hi}] there}
} {hi there}
test subst-8.5 {return in a subst} {
    subst {foo [return {]}; bogus code] bar}
} {foo ] bar}
test subst-8.6 {return in a subst} {
    list [catch {subst {foo [return {x}; bogus code bar}} msg] $msg
} {1 {missing close-bracket}}
test subst-8.7 {return in a subst, parse error} {
    subst {foo [return {x} ; set a {}" ; stuff] bar}
} {foo xset a {}" ; stuff] bar}
test subst-8.8 {return in a subst, parse error} {
    subst {foo [return {x} ; set bar baz ; set a {}" ; stuff] bar}
} {foo xset bar baz ; set a {}" ; stuff] bar}
test subst-8.9 {return in a variable subst} {
    subst {foo $var([return {x}]) bar}
} {foo x bar}

test subst-9.1 {error in a subst} {
    list [catch {subst {[error foo; bogus code]bar}} msg] $msg
} {1 foo}
test subst-9.2 {error in a subst} {
    list [catch {subst {[if 1 { error foo; bogus code}]bar}} msg] $msg
} {1 foo}
test subst-9.3 {error in a variable subst} {
    list [catch {subst {foo $var([error foo]) bar}} msg] $msg
} {1 foo}

test subst-10.1 {break in a subst} {
    subst {foo [break; bogus code] bar}
} {foo }
test subst-10.2 {break in a subst} {
    subst {foo [break; return x; bogus code] bar}
} {foo }
test subst-10.3 {break in a subst} {
    subst {foo [if 1 { break; bogus code}] bar}
} {foo }
test subst-10.4 {break in a subst, parse error} {
    subst {foo [break ; set a {}{} ; stuff] bar}
} {foo }
test subst-10.5 {break in a subst, parse error} {
    subst {foo [break ;set bar baz ;set a {}{} ; stuff] bar}
} {foo }
test subst-10.6 {break in a variable subst} {
    subst {foo $var([break]) bar}
} {foo }

test subst-11.1 {continue in a subst} {
    subst {foo [continue; bogus code] bar}
} {foo  bar}
test subst-11.2 {continue in a subst} {
    subst {foo [continue; return x; bogus code] bar}
} {foo  bar}
test subst-11.3 {continue in a subst} {
    subst {foo [if 1 { continue; bogus code}] bar}
} {foo  bar}
test subst-11.4 {continue in a subst, parse error} {
    subst {foo [continue ; set a {}{} ; stuff] bar}
} {foo set a {}{} ; stuff] bar}
test subst-11.5 {continue in a subst, parse error} {
    subst {foo [continue ;set bar baz ;set a {}{} ; stuff] bar}
} {foo set bar baz ;set a {}{} ; stuff] bar}
test subst-11.6 {continue in a variable subst} {
    subst {foo $var([continue]) bar}
} {foo  bar}

test subst-12.1 {nasty case, Bug 1036649} {
    for {set i 0} {$i < 10} {incr i} {
	set res [list [catch {subst "\[subst {};"} msg] $msg]
	if {$msg ne "missing close-bracket"} break
    }
    set res
} {1 {missing close-bracket}}
test subst-12.2 {nasty case, Bug 1036649} {
    for {set i 0} {$i < 10} {incr i} {
	set res [list [catch {subst "\[subst {}; "} msg] $msg]
	if {$msg ne "missing close-bracket"} break
    }
    set res
} {1 {missing close-bracket}}
test subst-12.3 {nasty case, Bug 1036649} {
    set x 0
    for {set i 0} {$i < 10} {incr i} {
        set res [list [catch {subst "\[incr x;"} msg] $msg]
        if {$msg ne "missing close-bracket"} break
    }
    list $res $x
} {{1 {missing close-bracket}} 0}
test subst-12.4 {nasty case, Bug 1036649} {
    set x 0
    for {set i 0} {$i < 10} {incr i} {
        set res [list [catch {subst "\[incr x; "} msg] $msg]
        if {$msg ne "missing close-bracket"} break
    }
    list $res $x
} {{1 {missing close-bracket}} 10}

# cleanup
::tcltest::cleanupTests
return