sl@0: # Commands covered: append lappend sl@0: # sl@0: # This file contains a collection of tests for one or more of the Tcl sl@0: # built-in commands. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1991-1993 The Regents of the University of California. sl@0: # Copyright (c) 1994-1996 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: append.test,v 1.7.12.1 2006/10/05 11:44:04 msofer Exp $ sl@0: sl@0: if {[lsearch [namespace children] ::tcltest] == -1} { sl@0: package require tcltest sl@0: namespace import -force ::tcltest::* sl@0: } sl@0: catch {unset x} sl@0: sl@0: test append-1.1 {append command} { sl@0: catch {unset x} sl@0: list [append x 1 2 abc "long string"] $x sl@0: } {{12abclong string} {12abclong string}} sl@0: test append-1.2 {append command} { sl@0: set x "" sl@0: list [append x first] [append x second] [append x third] $x sl@0: } {first firstsecond firstsecondthird firstsecondthird} sl@0: test append-1.3 {append command} { sl@0: set x "abcd" sl@0: append x sl@0: } abcd sl@0: sl@0: test append-2.1 {long appends} { sl@0: set x "" sl@0: for {set i 0} {$i < 1000} {set i [expr $i+1]} { sl@0: append x "foobar " sl@0: } sl@0: set y "foobar" sl@0: set y "$y $y $y $y $y $y $y $y $y $y" sl@0: set y "$y $y $y $y $y $y $y $y $y $y" sl@0: set y "$y $y $y $y $y $y $y $y $y $y " sl@0: expr {$x == $y} sl@0: } 1 sl@0: sl@0: test append-3.1 {append errors} { sl@0: list [catch {append} msg] $msg sl@0: } {1 {wrong # args: should be "append varName ?value value ...?"}} sl@0: test append-3.2 {append errors} { sl@0: set x "" sl@0: list [catch {append x(0) 44} msg] $msg sl@0: } {1 {can't set "x(0)": variable isn't array}} sl@0: test append-3.3 {append errors} { sl@0: catch {unset x} sl@0: list [catch {append x} msg] $msg sl@0: } {1 {can't read "x": no such variable}} sl@0: sl@0: test append-4.1 {lappend command} { sl@0: catch {unset x} sl@0: list [lappend x 1 2 abc "long string"] $x sl@0: } {{1 2 abc {long string}} {1 2 abc {long string}}} sl@0: test append-4.2 {lappend command} { sl@0: set x "" sl@0: list [lappend x first] [lappend x second] [lappend x third] $x sl@0: } {first {first second} {first second third} {first second third}} sl@0: test append-4.3 {lappend command} { sl@0: proc foo {} { sl@0: global x sl@0: set x old sl@0: unset x sl@0: lappend x new sl@0: } sl@0: set result [foo] sl@0: rename foo {} sl@0: set result sl@0: } {new} sl@0: test append-4.4 {lappend command} { sl@0: set x {} sl@0: lappend x \{\ abc sl@0: } {\{\ abc} sl@0: test append-4.5 {lappend command} { sl@0: set x {} sl@0: lappend x \{ abc sl@0: } {\{ abc} sl@0: test append-4.6 {lappend command} { sl@0: set x {1 2 3} sl@0: lappend x sl@0: } {1 2 3} sl@0: test append-4.7 {lappend command} { sl@0: set x "a\{" sl@0: lappend x abc sl@0: } "a\\\{ abc" sl@0: test append-4.8 {lappend command} { sl@0: set x "\\\{" sl@0: lappend x abc sl@0: } "\\{ abc" sl@0: test append-4.9 {lappend command} { sl@0: set x " \{" sl@0: list [catch {lappend x abc} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test append-4.10 {lappend command} { sl@0: set x " \{" sl@0: list [catch {lappend x abc} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test append-4.11 {lappend command} { sl@0: set x "\{\{\{" sl@0: list [catch {lappend x abc} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test append-4.12 {lappend command} { sl@0: set x "x \{\{\{" sl@0: list [catch {lappend x abc} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test append-4.13 {lappend command} { sl@0: set x "x\{\{\{" sl@0: lappend x abc sl@0: } "x\\\{\\\{\\\{ abc" sl@0: test append-4.14 {lappend command} { sl@0: set x " " sl@0: lappend x abc sl@0: } "abc" sl@0: test append-4.15 {lappend command} { sl@0: set x "\\ " sl@0: lappend x abc sl@0: } "{ } abc" sl@0: test append-4.16 {lappend command} { sl@0: set x "x " sl@0: lappend x abc sl@0: } "x abc" sl@0: test append-4.17 {lappend command} { sl@0: catch {unset x} sl@0: lappend x sl@0: } {} sl@0: test append-4.18 {lappend command} { sl@0: catch {unset x} sl@0: lappend x {} sl@0: } {{}} sl@0: test append-4.19 {lappend command} { sl@0: catch {unset x} sl@0: lappend x(0) sl@0: } {} sl@0: test append-4.20 {lappend command} { sl@0: catch {unset x} sl@0: lappend x(0) abc sl@0: } {abc} sl@0: unset x sl@0: test append-4.21 {lappend command} { sl@0: set x \" sl@0: list [catch {lappend x} msg] $msg sl@0: } {1 {unmatched open quote in list}} sl@0: test append-4.22 {lappend command} { sl@0: set x \" sl@0: list [catch {lappend x abc} msg] $msg sl@0: } {1 {unmatched open quote in list}} sl@0: sl@0: proc check {var size} { sl@0: set l [llength $var] sl@0: if {$l != $size} { sl@0: return "length mismatch: should have been $size, was $l" sl@0: } sl@0: for {set i 0} {$i < $size} {set i [expr $i+1]} { sl@0: set j [lindex $var $i] sl@0: if {$j != "item $i"} { sl@0: return "element $i should have been \"item $i\", was \"$j\"" sl@0: } sl@0: } sl@0: return ok sl@0: } sl@0: test append-5.1 {long lappends} { sl@0: catch {unset x} sl@0: set x "" sl@0: for {set i 0} {$i < 300} {set i [expr $i+1]} { sl@0: lappend x "item $i" sl@0: } sl@0: check $x 300 sl@0: } ok sl@0: sl@0: test append-6.1 {lappend errors} { sl@0: list [catch {lappend} msg] $msg sl@0: } {1 {wrong # args: should be "lappend varName ?value value ...?"}} sl@0: test append-6.2 {lappend errors} { sl@0: set x "" sl@0: list [catch {lappend x(0) 44} msg] $msg sl@0: } {1 {can't set "x(0)": variable isn't array}} sl@0: sl@0: test append-7.1 {lappend-created var and error in trace on that var} { sl@0: catch {rename foo ""} sl@0: catch {unset x} sl@0: trace variable x w foo sl@0: proc foo {} {global x; unset x} sl@0: catch {lappend x 1} sl@0: proc foo {args} {global x; unset x} sl@0: info exists x sl@0: set x sl@0: lappend x 1 sl@0: list [info exists x] [catch {set x} msg] $msg sl@0: } {0 1 {can't read "x": no such variable}} sl@0: test append-7.2 {lappend var triggers read trace} { sl@0: catch {unset myvar} sl@0: catch {unset ::result} sl@0: trace variable myvar r foo sl@0: proc foo {args} {append ::result $args} sl@0: lappend myvar a sl@0: list [catch {set ::result} msg] $msg sl@0: } {0 {myvar {} r}} sl@0: test append-7.3 {lappend var triggers read trace, array var} { sl@0: # The behavior of read triggers on lappend changed in 8.0 to sl@0: # not trigger them, and was changed back in 8.4. sl@0: catch {unset myvar} sl@0: catch {unset ::result} sl@0: trace variable myvar r foo sl@0: proc foo {args} {append ::result $args} sl@0: lappend myvar(b) a sl@0: list [catch {set ::result} msg] $msg sl@0: } {0 {myvar b r}} sl@0: test append-7.4 {lappend var triggers read trace, array var exists} { sl@0: catch {unset myvar} sl@0: catch {unset ::result} sl@0: set myvar(0) 1 sl@0: trace variable myvar r foo sl@0: proc foo {args} {append ::result $args} sl@0: lappend myvar(b) a sl@0: list [catch {set ::result} msg] $msg sl@0: } {0 {myvar b r}} sl@0: test append-7.5 {append var does not trigger read trace} { sl@0: catch {unset myvar} sl@0: catch {unset ::result} sl@0: trace variable myvar r foo sl@0: proc foo {args} {append ::result $args} sl@0: append myvar a sl@0: info exists ::result sl@0: } {0} sl@0: sl@0: sl@0: catch {unset i x result y} sl@0: catch {rename foo ""} sl@0: catch {rename check ""} sl@0: sl@0: # cleanup sl@0: ::tcltest::cleanupTests sl@0: return