1 # Commands covered: append lappend
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # 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.
11 # See the file "license.terms" for information on usage and redistribution of
12 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
20 test append-1.1 {append command} {
22 list [append x 1 2 abc "long string"] $x
23 } {{12abclong string} {12abclong string}}
24 test append-1.2 {append command} {
26 list [append x first] [append x second] [append x third] $x
27 } {first firstsecond firstsecondthird firstsecondthird}
28 test append-1.3 {append command} {
33 test append-2.1 {long appends} {
35 for {set i 0} {$i < 1000} {incr i} {
39 set y "$y $y $y $y $y $y $y $y $y $y"
40 set y "$y $y $y $y $y $y $y $y $y $y"
41 set y "$y $y $y $y $y $y $y $y $y $y "
45 test append-3.1 {append errors} -returnCodes error -body {
47 } -result {wrong # args: should be "append varName ?value ...?"}
48 test append-3.2 {append errors} -returnCodes error -body {
51 } -result {can't set "x(0)": variable isn't array}
52 test append-3.3 {append errors} -returnCodes error -body {
55 } -result {can't read "x": no such variable}
57 test append-4.1 {lappend command} {
59 list [lappend x 1 2 abc "long string"] $x
60 } {{1 2 abc {long string}} {1 2 abc {long string}}}
61 test append-4.2 {lappend command} {
63 list [lappend x first] [lappend x second] [lappend x third] $x
64 } {first {first second} {first second third} {first second third}}
65 test append-4.3 {lappend command} -body {
76 test append-4.4 {lappend command} {
80 test append-4.5 {lappend command} {
84 test append-4.6 {lappend command} {
88 test append-4.7 {lappend command} {
92 test append-4.8 {lappend command} {
96 test append-4.9 {lappend command} -returnCodes error -body {
99 } -result {unmatched open brace in list}
100 test append-4.10 {lappend command} -returnCodes error -body {
103 } -result {unmatched open brace in list}
104 test append-4.11 {lappend command} -returnCodes error -body {
107 } -result {unmatched open brace in list}
108 test append-4.12 {lappend command} -returnCodes error -body {
111 } -result {unmatched open brace in list}
112 test append-4.13 {lappend command} {
115 } "x\\\{\\\{\\\{ abc"
116 test append-4.14 {lappend command} {
120 test append-4.15 {lappend command} {
124 test append-4.16 {lappend command} {
128 test append-4.17 {lappend command} {
132 test append-4.18 {lappend command} {
136 test append-4.19 {lappend command} {
140 test append-4.20 {lappend command} {
145 test append-4.21 {lappend command} -returnCodes error -body {
148 } -result {unmatched open quote in list}
149 test append-4.22 {lappend command} -returnCodes error -body {
152 } -result {unmatched open quote in list}
154 test append-5.1 {long lappends} -setup {
156 proc check {var size} {
159 return "length mismatch: should have been $size, was $l"
161 for {set i 0} {$i < $size} {incr i} {
162 set j [lindex $var $i]
163 if {$j ne "item $i"} {
164 return "element $i should have been \"item $i\", was \"$j\""
171 for {set i 0} {$i < 300} {incr i} {
179 test append-6.1 {lappend errors} -returnCodes error -body {
181 } -result {wrong # args: should be "lappend varName ?value ...?"}
182 test append-6.2 {lappend errors} -returnCodes error -body {
185 } -result {can't set "x(0)": variable isn't array}
187 test append-7.1 {lappend-created var and error in trace on that var} -setup {
188 catch {rename foo ""}
191 trace variable x w foo
192 proc foo {} {global x; unset x}
194 proc foo {args} {global x; unset x}
198 list [info exists x] [catch {set x} msg] $msg
199 } -result {0 1 {can't read "x": no such variable}}
200 test append-7.2 {lappend var triggers read trace} -setup {
201 unset -nocomplain myvar
202 unset -nocomplain ::result
204 trace variable myvar r foo
205 proc foo {args} {append ::result $args}
208 } -result {myvar {} r}
209 test append-7.3 {lappend var triggers read trace, array var} -setup {
210 unset -nocomplain myvar
211 unset -nocomplain ::result
213 # The behavior of read triggers on lappend changed in 8.0 to not trigger
214 # them, and was changed back in 8.4.
215 trace variable myvar r foo
216 proc foo {args} {append ::result $args}
219 } -result {myvar b r}
220 test append-7.4 {lappend var triggers read trace, array var exists} -setup {
221 unset -nocomplain myvar
222 unset -nocomplain ::result
225 trace variable myvar r foo
226 proc foo {args} {append ::result $args}
229 } -result {myvar b r}
230 test append-7.5 {append var does not trigger read trace} -setup {
231 unset -nocomplain myvar
232 unset -nocomplain ::result
234 trace variable myvar r foo
235 proc foo {args} {append ::result $args}
240 # THERE ARE NO append-8.* TESTS
242 # New tests for bug 3057639 to show off the more consistent behaviour of
243 # lappend in both direct-eval and bytecompiled code paths (see appendComp.test
244 # for the compiled variants). lappend now behaves like append. 9.0/1 lappend -
247 test append-9.0 {bug 3057639, lappend direct eval, read trace on non-existing array variable element} -setup {
248 unset -nocomplain myvar
251 proc nonull {var key val} {
253 if {![info exists lvar($key)]} {
254 return -code error "no such variable"
257 trace add variable myvar read nonull
259 lappend myvar(key) "new value"
261 } -result {0 {{new value}}}
262 test append-9.1 {bug 3057639, lappend direct eval, read trace on non-existing env element} -setup {
263 unset -nocomplain ::env(__DUMMY__)
266 lappend ::env(__DUMMY__) "new value"
269 unset -nocomplain ::env(__DUMMY__)
270 } -result {0 {{new value}}}
271 test append-9.2 {bug 3057639, append direct eval, read trace on non-existing array variable element} -setup {
272 unset -nocomplain myvar
275 proc nonull {var key val} {
277 if {![info exists lvar($key)]} {
278 return -code error "no such variable"
281 trace add variable myvar read nonull
283 append myvar(key) "new value"
285 } -result {0 {new value}}
286 test append-9.3 {bug 3057639, append direct eval, read trace on non-existing env element} -setup {
287 unset -nocomplain ::env(__DUMMY__)
290 append ::env(__DUMMY__) "new value"
293 unset -nocomplain ::env(__DUMMY__)
294 } -result {0 {new value}}
296 test append-10.1 {Bug 214cc0eb22: lappend with no values} {
300 test append-10.2 {Bug 214cc0eb22: lappend with no values} -body {
303 } -returnCodes error -result {unmatched open brace in list}
304 test append-10.3 {Bug 214cc0eb22: expanded lappend with no values} {
306 [subst lappend] lst {*}[list]
308 test append-10.4 {Bug 214cc0eb22: expanded lappend with no values} -body {
310 [subst lappend] lst {*}[list]
311 } -returnCodes error -result {unmatched open brace in list}
313 unset -nocomplain i x result y
314 catch {rename foo ""}
317 ::tcltest::cleanupTests