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-1999 by Scriptics Corporation.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 if {[lsearch [namespace children] ::tcltest] == -1} {
17 package require tcltest
18 namespace import -force ::tcltest::*
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"}}
28 test subst-2.1 {simple strings} {
31 test subst-2.2 {simple strings} {
34 test subst-2.3 {simple strings} {
38 test subst-3.1 {backslash substitutions} {
39 subst {\x\$x\[foo bar]\\}
42 test subst-4.1 {variable substitutions} {
46 test subst-4.2 {variable substitutions} {
50 test subst-4.3 {variable substitutions} {
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
64 test subst-4.4 {variable substitutions} {
65 list [catch {subst {$long $a}} msg] $msg
66 } {1 {can't read "a": no such variable}}
68 test subst-5.1 {command substitutions} {
71 test subst-5.2 {command substitutions} {
72 subst {[concat A test string]}
74 test subst-5.3 {command substitutions} {
75 subst {x.[concat foo].y.[concat 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"}}
81 test subst-6.1 {clear the result after command substitution} {
83 list [catch {subst {[concat foo] $a}} msg] $msg
84 } {1 {can't read "a": no such variable}}
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} {
97 subst -nobackslashes {abc $x [expr 1+2] \\\x41}
99 test subst-7.5 {switches} {
101 subst -nocommands {abc $x [expr 1+2] \\\x41}
102 } {abc 123 [expr 1+2] \A}
103 test subst-7.6 {switches} {
105 subst -novariables {abc $x [expr 1+2] \\\x41}
107 test subst-7.7 {switches} {
109 subst -nov -nob -noc {abc $x [expr 1+2] \\\x41}
110 } {abc $x [expr 1+2] \\\x41}
113 ::tcltest::cleanupTests