# Commands covered: expression scripts "(...)" (TIP 759)
#
# This file contains a collection of tests for the expression scripts of
# TIP 759: a script that begins with "(" and ends with ")" is one
# expression. Sourcing this file into Tcl runs the tests and generates
# output for errors. No output means no errors were found.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
if {"::tcltest" ni [namespace children]} {
package require tcltest 2.5
namespace import -force ::tcltest::*
}
::tcltest::loadTestedCommands
catch [list package require -exact tcl::test [info patchlevel]]
testConstraint testevalex [llength [info commands testevalex]]
# Forms given by the parser: {(...)}, "(...)", (...)
test exprScript-1.1 {procedure body} -setup {
proc sq {x} {($x*$x)}
} -body {
sq 5
} -cleanup {
rename sq {}
} -result 25
test exprScript-1.2 {procedure body, function and white space} -setup {
proc hyp {a b} {( sqrt($a**2 + $b**2) )}
} -body {
hyp 3 4
} -cleanup {
rename hyp {}
} -result 5.0
test exprScript-1.3 {braced loop body} {
lmap v {1 2 3} {($v*10)}
} {10 20 30}
test exprScript-1.4 {quoted loop body} {
lmap v {1 2 3} "(\$v+1)"
} {2 3 4}
test exprScript-1.5 {bare word, substituted before the call} {
set n 4
lmap v {1 2} ($n*2)
} {8 8}
test exprScript-1.6 {eval} {
set a 3
eval {($a+1)}
} 4
test exprScript-1.7 {eval, quoted} {
set a 3
eval "(\$a+2)"
} 5
test exprScript-1.8 {uplevel} {
uplevel 0 {(1+1)}
} 2
test exprScript-1.9 {if body} {
if {1} {(42)}
} 42
test exprScript-1.10 {the first "(" is closed early} {
eval {(1)+(2)}
} 3
test exprScript-1.11 {lambda} {
apply {{x} {($x - 1)}} 10
} 9
test exprScript-1.12 {newline after the close paren} {
eval "(2*3)\n"
} 6
test exprScript-1.13 {only a whole script: not its last command} -body {
eval {set b 5
($b+1)}
} -returnCodes error -result {invalid command name "(5+1)"}
test exprScript-1.13.1 {only a whole script: in a procedure body} -setup {
proc p {} {set b 5
($b+1)}
} -body {
p
} -cleanup {
rename p {}
} -returnCodes error -result {invalid command name "(5+1)"}
test exprScript-1.13.2 {nested script in brackets} {
set b 5
list [eval {set c [eval {($b*2)}]}] $c
} {10 10}
test exprScript-1.14 {command substitution inside} -setup {
proc f {} {( [llength {a b c}] )}
} -body {
f
} -cleanup {
rename f {}
} -result 3
test exprScript-1.15 {same script object twice} {
set a 3
set s {($a*$a)}
list [eval $s] [eval $s]
} {9 9}
test exprScript-1.16 {semicolon and newline are not separators} {
eval {(1 +
2)}
} 3
# Not compiled: Tcl_EvalEx
test exprScript-2.1 {Tcl_EvalEx} -constraints testevalex -body {
set a 3
testevalex {($a * 7)}
} -result 21
test exprScript-2.2 {Tcl_EvalEx, first "(" closed early} -constraints {
testevalex
} -body {
testevalex {(1)+(2)}
} -result 3
test exprScript-2.3 {Tcl_EvalEx, newline after} -constraints testevalex -body {
testevalex "(2*3)\n"
} -result 6
test exprScript-2.4 {Tcl_EvalEx, only a whole script} -constraints {
testevalex
} -body {
testevalex {set b 5
($b+1)}
} -returnCodes error -result {invalid command name "(5+1)"}
test exprScript-2.6 {Tcl_EvalEx, sourced file} -setup {
set f [makeFile {( 6 * 7 )} exprScript26.tcl]
} -body {
source $f
} -cleanup {
removeFile exprScript26.tcl
} -result 42
test exprScript-2.5 {Tcl_EvalEx, error} -constraints testevalex -body {
testevalex {(1/0)}
} -returnCodes error -result {divide by zero}
# Errors and limits
test exprScript-3.1 {error in the expression} -body {
eval {(1/0)}
} -returnCodes error -result {divide by zero}
test exprScript-3.2 {error info} -setup {
proc e {} {(1/0)}
} -body {
catch e m o
dict get $o -errorinfo
} -cleanup {
rename e {}
} -result {divide by zero
invoked from within
"(1/0)"
(procedure "e" line 1)
invoked from within
"e"}
test exprScript-3.3 {syntax error} -body {
eval {(a b)}
} -returnCodes error -match glob -result {invalid bareword "a"*}
test exprScript-3.4 {empty expression} -body {
eval {()}
} -returnCodes error -match glob -result {empty subexpression*}
test exprScript-3.5 {script that does not end with ")" is not one} -body {
eval {(1+2) ;}
} -returnCodes error -result {invalid command name "(1+2)"}
test exprScript-3.6 {command name that starts with "("} -setup {
proc (x) {} {return cmd}
} -body {
list [eval {(x) }] [eval { (x)}]
} -cleanup {
rename (x) {}
} -result {cmd cmd}
test exprScript-3.7 {expression substitution alone: its value is the result} {
eval {[(1+2)]}
} 3
test exprScript-3.8 {nested command substitution is not an expression script} -setup {
proc (x) {} {return cmd}
} -body {
set r [(x)]
} -cleanup {
rename (x) {}
} -returnCodes error -match glob -result {invalid bareword "x"*}
# Bytecode
test exprScript-4.1 {compiled as an expression} -setup {
proc sq {x} {($x*$x)}
} -body {
string match "*mult*" [tcl::unsupported::disassemble proc sq]
} -cleanup {
rename sq {}
} -result 1
# Canonical lists (TIP 407): a list evaluated as a script is one command.
# A first element that begins with "(" is quoted, as one that begins with
# "#", so that a list is never an expression script.
test exprScript-6.1 {first element that begins with "(" is braced} {
list (cmd) (x)
} {{(cmd)} (x)}
test exprScript-6.2 {only the first element} {
list x (y) (z)
} {x (y) (z)}
test exprScript-6.3 {a list is one command, not an expression script} -setup {
proc (cmd) {args} {return "called with $args"}
} -body {
set script [string range [list (cmd) (x)] 0 end]
uplevel #0 $script
} -cleanup {
rename (cmd) {}
} -result {called with {(x)}}
test exprScript-6.4 {one element} -setup {
proc (a) {} {return called}
} -body {
uplevel #0 [string range [list (a)] 0 end]
} -cleanup {
rename (a) {}
} -result called
test exprScript-6.5 {escaped form} {
list "(\}" b)
} {\(\} b)}
test exprScript-6.6 {round trip} {
set L [list "(\}" b) (c)]
expr {[lindex [string range $L 0 end] 0] eq "(\}"}
} 1
test exprScript-6.7 {dict keys} {
dict create (k) v
} {{(k)} v}
test exprScript-6.8 {concat keeps the string} {
concat [list a] [list (b) c]
} {a {(b)} c}
# cleanup
::tcltest::cleanupTests
return
# Local Variables:
# mode: tcl
# End: