# 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.6.2.1 2001/04/03 22:54:38 hobbs Exp $

source [file dirname [info script]]/testing.tcl

test subst-1.0 {basics} {
    subst {\$x}
} "\$x"

test subst-1.1 {basics} {
    list [catch {subst} msg]
} {1}
test subst-1.2 {basics} {
    list [catch {subst a b c} msg]
} {1}

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]
} {1}
test subst-7.2 {switches} {
    list [catch {subst -no bar} msg]
} {1}
test subst-7.3 {switches} {
    list [catch {subst -bogus bar} msg]
} {1}
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-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-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 }

# Note that unlike Tcl, Jim throws an error for these two before
# evaluating 
test subst-10.4 {break in a subst, parse error} tcl {
    subst {foo [break ; set a {}{} ; stuff] bar}
} {foo }
test subst-10.5 {break in a subst, parse error} tcl {
    subst {foo [break ;set bar baz ;set a {}{} ; stuff] 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-12.1 {lone $} {
    subst {$}
} {$}

test subst-12.2 {lone $} {
    set a 1
    subst -novar {${a}}
} {${a}}

test subst-12.3 {variable inside [] with -noc} {
    set a 1
    subst -noc {x[join $a]y}
} {x[join 1]y}


# cleanup
testreport
