blob: b360b6f9a9f3c9ef9a7ef2653addbc89fa75e7d3 (
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
|
# 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-1999 by Scriptics Corporation.
#
# 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.6 2000/04/10 17:19:05 ericm 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 {wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"}}
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-3.1 {backslash substitutions} {
subst {\x\$x\[foo bar]\\}
} "x\$x\[foo bar]\\"
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-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 {wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"}}
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}
# cleanup
::tcltest::cleanupTests
return
|