1 # Commands covered: apply
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) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # Copyright (c) 2005-2006 Miguel Sofer
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 source [file dirname [info script]]/testing.tcl
20 # Tests for wrong number of arguments
22 test apply-1.1 {too few arguments} -returnCodes error -body {
24 } -result {wrong # args: should be "apply lambdaExpr ?arg ...?"}
26 # Tests for malformed lambda
28 test apply-2.0 {malformed lambda} -returnCodes error -body {
31 } -result {can't interpret "a" as a lambda expression}
32 test apply-2.1 {malformed lambda} -returnCodes error -body {
33 set lambda [list a b c d]
35 } -result {can't interpret "a b c d" as a lambda expression}
36 test apply-2.2 {malformed lambda} -body {
37 set lambda [list {{}} boo]
39 } -returnCodes error -match glob -result {*argument with no name}
40 test apply-2.3 {malformed lambda} {
41 set lambda [list {{a b c}} boo]
42 list [catch {apply $lambda} msg] $msg
43 } {1 {too many fields in argument specifier "a b c"}}
45 # Note that Jim allow both of these
46 test apply-2.4 {malformed lambda} tcl {
47 set lambda [list a(1) {return $a(1)}]
48 list [catch {apply $lambda x} msg] $msg
49 } {1 {formal parameter "a(1)" is an array element}}
50 test apply-2.5 {malformed lambda} tcl {
51 set lambda [list a::b {return $a::b}]
52 list [catch {apply $lambda x} msg] $msg
53 } {1 {formal parameter "a::b" is not a simple name}}
55 # Tests for runtime errors in the lambda expression
57 test apply-4.1 {error in arguments to lambda expression} -body {
58 set lambda [list x {set x 1}]
60 } -returnCodes error -result {wrong # args: should be "apply lambdaExpr x"}
61 test apply-4.2 {error in arguments to lambda expression} -body {
62 set lambda [list x {set x 1}]
64 } -returnCodes error -result {wrong # args: should be "apply lambdaExpr x"}
66 test apply-5.1 {runtime error in lambda expression} {
67 set lambda [list {} {error foo}]
68 list [catch {apply $lambda} msg] $msg
71 # Tests for correct execution; as the implementation is the same as that for
72 # procs, the general functionality is mostly tested elsewhere
74 test apply-6.1 {info level} {
76 set lambda [list {} {info level}]
77 expr {[apply $lambda] - $lev}
79 test apply-6.2 {info level} tcl {
80 set lambda [list {} {info level 0}]
82 } {apply {{} {info level 0}}}
83 test apply-6.3 {info level} tcl {
84 set lambda [list args {info level 0}]
86 } {apply {args {info level 0}} x y}
88 # Tests for correct argument treatment
92 foreach v [lsort [info locals]] {
93 if {$v eq "res"} continue
94 lappend res [list $v [set $v]]
99 test apply-8.1 {args treatment} {
100 apply [list args $applyBody] 1 2 3
102 test apply-8.2 {args treatment} {
103 apply [list {x args} $applyBody] 1 2
105 test apply-8.3 {args treatment} {
106 apply [list {x args} $applyBody] 1 2 3
107 } {{args {2 3}} {x 1}}
108 test apply-8.4 {default values} {
109 apply [list {{x 1} {y 2}} $applyBody]
111 test apply-8.5 {default values} {
112 apply [list {{x 1} {y 2}} $applyBody] 3 4
114 test apply-8.6 {default values} {
115 apply [list {{x 1} {y 2}} $applyBody] 3
117 test apply-8.7 {default values} {
118 apply [list {x {y 2}} $applyBody] 1
120 test apply-8.8 {default values} {
121 apply [list {x {y 2}} $applyBody] 1 3
123 test apply-8.9 {default values} {
124 apply [list {x {y 2} args} $applyBody] 1
125 } {{args {}} {x 1} {y 2}}
126 test apply-8.10 {default values} {
127 apply [list {x {y 2} args} $applyBody] 1 3
128 } {{args {}} {x 1} {y 3}}
130 ::tcltest::cleanupTests