OSDN Git Service

e4e8b8eff09e49a5acd5f19e3ee1c89721504522
[pf3gnuchains/sourceware.git] / tcl / tests / subst.test
1 # Commands covered:  subst
2 #
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.
6 #
7 # Copyright (c) 1994 The Regents of the University of California.
8 # Copyright (c) 1994 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 #
14 # RCS: @(#) $Id$
15
16 if {[lsearch [namespace children] ::tcltest] == -1} {
17     package require tcltest
18     namespace import -force ::tcltest::*
19 }
20
21 test subst-1.1 {basics} {
22     list [catch {subst} msg] $msg
23 } {1 {wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"}}
24 test subst-1.2 {basics} {
25     list [catch {subst a b c} msg] $msg
26 } {1 {wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"}}
27
28 test subst-2.1 {simple strings} {
29     subst {}
30 } {}
31 test subst-2.2 {simple strings} {
32     subst a
33 } a
34 test subst-2.3 {simple strings} {
35     subst abcdefg
36 } abcdefg
37
38 test subst-3.1 {backslash substitutions} {
39     subst {\x\$x\[foo bar]\\}
40 } "x\$x\[foo bar]\\"
41
42 test subst-4.1 {variable substitutions} {
43     set a 44
44     subst {$a}
45 } {44}
46 test subst-4.2 {variable substitutions} {
47     set a 44
48     subst {x$a.y{$a}.z}
49 } {x44.y{44}.z}
50 test subst-4.3 {variable substitutions} {
51     catch {unset a}
52     set a(13) 82
53     set i 13
54     subst {x.$a($i)}
55 } {x.82}
56 catch {unset a}
57 set long {This is a very long string, intentionally made so long that it
58         will overflow the static character size for dstrings, so that
59         additional memory will have to be allocated by subst.  That way,
60         if the subst procedure forgets to free up memory while returning
61         an error, there will be memory that isn't freed (this will be
62         detected when the tests are run under a checking memory allocator
63         such as Purify).}
64 test subst-4.4 {variable substitutions} {
65     list [catch {subst {$long $a}} msg] $msg
66 } {1 {can't read "a": no such variable}}
67
68 test subst-5.1 {command substitutions} {
69     subst {[concat {}]}
70 } {}
71 test subst-5.2 {command substitutions} {
72     subst {[concat A test string]}
73 } {A test string}
74 test subst-5.3 {command substitutions} {
75     subst {x.[concat foo].y.[concat bar].z}
76 } {x.foo.y.bar.z}
77 test subst-5.4 {command substitutions} {
78     list [catch {subst {$long [set long] [bogus_command]}} msg] $msg
79 } {1 {invalid command name "bogus_command"}}
80
81 test subst-6.1 {clear the result after command substitution} {
82     catch {unset a}
83     list [catch {subst {[concat foo] $a}} msg] $msg
84 } {1 {can't read "a": no such variable}}
85
86 test subst-7.1 {switches} {
87     list [catch {subst foo bar} msg] $msg
88 } {1 {wrong # args: should be "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"}}
89 test subst-7.2 {switches} {
90     list [catch {subst -no bar} msg] $msg
91 } {1 {ambiguous switch "-no": must be -nobackslashes, -nocommands, or -novariables}}
92 test subst-7.3 {switches} {
93     list [catch {subst -bogus bar} msg] $msg
94 } {1 {bad switch "-bogus": must be -nobackslashes, -nocommands, or -novariables}}
95 test subst-7.4 {switches} {
96     set x 123
97     subst -nobackslashes {abc $x [expr 1+2] \\\x41}
98 } {abc 123 3 \\\x41}
99 test subst-7.5 {switches} {
100     set x 123
101     subst -nocommands {abc $x [expr 1+2] \\\x41}
102 } {abc 123 [expr 1+2] \A}
103 test subst-7.6 {switches} {
104     set x 123
105     subst -novariables {abc $x [expr 1+2] \\\x41}
106 } {abc $x 3 \A}
107 test subst-7.7 {switches} {
108     set x 123
109     subst -nov -nob -noc {abc $x [expr 1+2] \\\x41}
110 } {abc $x [expr 1+2] \\\x41}
111
112 # cleanup
113 ::tcltest::cleanupTests
114 return
115
116
117
118
119
120
121
122
123
124
125
126
127