Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/upvar.test @ 68

Last change on this file since 68 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 13.2 KB
Line 
1# Commands covered:  'upvar', 'namespace upvar'
2#
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.
6#
7# Copyright (c) 1991-1993 The Regents of the University of California.
8# Copyright (c) 1994 Sun Microsystems, Inc.
9# Copyright (c) 1998-1999 by Scriptics Corporation.
10#
11# See the file "license.terms" for information on usage and redistribution
12# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13#
14# RCS: @(#) $Id: upvar.test,v 1.16 2007/12/13 15:26:07 dgp Exp $
15
16if {[lsearch [namespace children] ::tcltest] == -1} {
17    package require tcltest 2
18    namespace import -force ::tcltest::*
19}
20
21testConstraint testupvar [llength [info commands testupvar]]
22
23test upvar-1.1 {reading variables with upvar} {
24    proc p1 {a b} {set c 22; set d 33; p2}
25    proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
26    p1 foo bar
27} {foo bar 22 33 abc}
28test upvar-1.2 {reading variables with upvar} {
29    proc p1 {a b} {set c 22; set d 33; p2}
30    proc p2 {} {p3}
31    proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
32    p1 foo bar
33} {foo bar 22 33 abc}
34test upvar-1.3 {reading variables with upvar} {
35    proc p1 {a b} {set c 22; set d 33; p2}
36    proc p2 {} {p3}
37    proc p3 {} {
38        upvar #1 a x1 b x2 c x3 d x4
39        set a abc
40        list $x1 $x2 $x3 $x4 $a
41    }
42    p1 foo bar
43} {foo bar 22 33 abc}
44test upvar-1.4 {reading variables with upvar} {
45    set x1 44
46    set x2 55
47    proc p1 {} {p2}
48    proc p2 {} {
49        upvar 2 x1 x1 x2 a
50        upvar #0 x1 b
51        set c $b
52        incr b 3
53        list $x1 $a $b
54    }
55    p1
56} {47 55 47}
57test upvar-1.5 {reading array elements with upvar} {
58    proc p1 {} {set a(0) zeroth; set a(1) first; p2}
59    proc p2 {} {upvar a(0) x; set x}
60    p1
61} {zeroth}
62
63test upvar-2.1 {writing variables with upvar} {
64    proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
65    proc p2 {} {
66        upvar a x1 b x2 c x3 d x4
67        set x1 14
68        set x4 88
69    }
70    p1 foo bar
71} {14 bar 22 88}
72test upvar-2.2 {writing variables with upvar} {
73    set x1 44
74    set x2 55
75    proc p1 {x1 x2} {
76        upvar #0 x1 a
77        upvar x2 b
78        set a $x1
79        set b $x2
80    }
81    p1 newbits morebits
82    list $x1 $x2
83} {newbits morebits}
84test upvar-2.3 {writing variables with upvar} {
85    catch {unset x1}
86    catch {unset x2}
87    proc p1 {x1 x2} {
88        upvar #0 x1 a
89        upvar x2 b
90        set a $x1
91        set b $x2
92    }
93    p1 newbits morebits
94    list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
95} {0 newbits 0 morebits}
96test upvar-2.4 {writing array elements with upvar} {
97    proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)}
98    proc p2 {} {upvar a(0) x; set x xyzzy}
99    p1
100} {xyzzy xyzzy}
101
102test upvar-3.1 {unsetting variables with upvar} {
103    proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
104    proc p2 {} {
105        upvar 1 a x1 d x2
106        unset x1 x2
107    }
108    p1 foo bar
109} {b c}
110test upvar-3.2 {unsetting variables with upvar} {
111    proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
112    proc p2 {} {
113        upvar 1 a x1 d x2
114        unset x1 x2
115        set x2 28
116    }
117    p1 foo bar
118} {b c d}
119test upvar-3.3 {unsetting variables with upvar} {
120    set x1 44
121    set x2 55
122    proc p1 {} {p2}
123    proc p2 {} {
124        upvar 2 x1 a
125        upvar #0 x2 b
126        unset a b
127    }
128    p1
129    list [info exists x1] [info exists x2]
130} {0 0}
131test upvar-3.4 {unsetting variables with upvar} {
132    set x1 44
133    set x2 55
134    proc p1 {} {
135        upvar x1 a x2 b
136        unset a b
137        set b 118
138    }
139    p1
140    list [info exists x1] [catch {set x2} msg] $msg
141} {0 0 118}
142test upvar-3.5 {unsetting array elements with upvar} {
143    proc p1 {} {
144        set a(0) zeroth
145        set a(1) first
146        set a(2) second
147        p2
148        array names a
149    }
150    proc p2 {} {upvar a(0) x; unset x}
151    p1
152} {1 2}
153test upvar-3.6 {unsetting then resetting array elements with upvar} {
154    proc p1 {} {
155        set a(0) zeroth
156        set a(1) first
157        set a(2) second
158        p2
159        list [array names a] [catch {set a(0)} msg] $msg
160    }
161    proc p2 {} {upvar a(0) x; unset x; set x 12345}
162    p1
163} {{0 1 2} 0 12345}
164
165test upvar-4.1 {nested upvars} {
166    set x1 88
167    proc p1 {a b} {set c 22; set d 33; p2}
168    proc p2 {} {global x1; upvar c x2; p3}
169    proc p3 {} {
170        upvar x1 a x2 b
171        list $a $b
172    }
173    p1 14 15
174} {88 22}
175test upvar-4.2 {nested upvars} {
176    set x1 88
177    proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
178    proc p2 {} {global x1; upvar c x2; p3}
179    proc p3 {} {
180        upvar x1 a x2 b
181        set a foo
182        set b bar
183    }
184    list [p1 14 15] $x1
185} {{14 15 bar 33} foo}
186
187proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
188test upvar-5.1 {traces involving upvars} {
189    proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
190    proc p2 {} {upvar c x1; set x1 22}
191    set x ---
192    p1 foo bar
193    set x
194} {{x1 {} w} x1}
195test upvar-5.2 {traces involving upvars} {
196    proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
197    proc p2 {} {upvar c x1; set x1}
198    set x ---
199    p1 foo bar
200    set x
201} {{x1 {} r} x1}
202test upvar-5.3 {traces involving upvars} {
203    proc p1 {a b} {set c 22; set d 33; trace var c rwu tproc; p2}
204    proc p2 {} {upvar c x1; unset x1}
205    set x ---
206    p1 foo bar
207    set x
208} {{x1 {} u} x1}
209
210test upvar-6.1 {retargeting an upvar} {
211    proc p1 {} {
212        set a(0) zeroth
213        set a(1) first
214        set a(2) second
215        p2
216    }
217    proc p2 {} {
218        upvar a x
219        set result {}
220        foreach i [array names x] {
221            upvar a($i) x
222            lappend result $x
223        }
224        lsort $result
225    }
226    p1
227} {first second zeroth}
228test upvar-6.2 {retargeting an upvar} {
229    set x 44
230    set y abcde
231    proc p1 {} {
232        global x
233        set result $x
234        upvar y x
235        lappend result $x
236    }
237    p1
238} {44 abcde}
239test upvar-6.3 {retargeting an upvar} {
240    set x 44
241    set y abcde
242    proc p1 {} {
243        upvar y x
244        lappend result $x
245        global x
246        lappend result $x
247    }
248    p1
249} {abcde 44}
250
251test upvar-7.1 {upvar to same level} {
252    set x 44
253    set y 55
254    catch {unset uv}
255    upvar #0 x uv
256    set uv abc
257    upvar 0 y uv
258    set uv xyzzy
259    list $x $y
260} {abc xyzzy}
261test upvar-7.2 {upvar to same level} {
262    set x 1234
263    set y 4567
264    proc p1 {x y} {
265        upvar 0 x uv
266        set uv $y
267        return "$x $y"
268    }
269    p1 44 89
270} {89 89}
271test upvar-7.3 {upvar to same level} {
272    set x 1234
273    set y 4567
274    proc p1 {x y} {
275        upvar #1 x uv
276        set uv $y
277        return "$x $y"
278    }
279    p1 xyz abc
280} {abc abc}
281test upvar-7.4 {upvar to same level: tricky problems when deleting variable table} {
282    proc tt {} {upvar #1 toto loc;  return $loc}
283    list [catch tt msg] $msg
284} {1 {can't read "loc": no such variable}}
285test upvar-7.5 {potential memory leak when deleting variable table} {
286    proc leak {} {
287        array set foo {1 2 3 4}
288        upvar 0 foo(1) bar
289    }
290    leak
291} {}
292
293test upvar-8.1 {errors in upvar command} {
294    list [catch upvar msg] $msg
295} {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
296test upvar-8.2 {errors in upvar command} {
297    list [catch {upvar 1} msg] $msg
298} {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
299test upvar-8.3 {errors in upvar command} {
300    proc p1 {} {upvar a b c}
301    list [catch p1 msg] $msg
302} {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
303test upvar-8.4 {errors in upvar command} {
304    proc p1 {} {upvar 0 b b}
305    list [catch p1 msg] $msg
306} {1 {can't upvar from variable to itself}}
307test upvar-8.5 {errors in upvar command} {
308    proc p1 {} {upvar 0 a b; upvar 0 b a}
309    list [catch p1 msg] $msg
310} {1 {can't upvar from variable to itself}}
311test upvar-8.6 {errors in upvar command} {
312    proc p1 {} {set a 33; upvar b a}
313    list [catch p1 msg] $msg
314} {1 {variable "a" already exists}}
315test upvar-8.7 {errors in upvar command} {
316    proc p1 {} {trace variable a w foo; upvar b a}
317    list [catch p1 msg] $msg
318} {1 {variable "a" has traces: can't use for upvar}}
319test upvar-8.8 {create nested array with upvar} -body {
320    proc p1 {} {upvar x(a) b; set b(2) 44}
321    catch {unset x}
322    list [catch p1 msg] $msg
323} -cleanup {
324    unset x
325} -result {1 {can't set "b(2)": variable isn't array}}
326test upvar-8.9 {upvar won't create namespace variable that refers to procedure variable} {
327    catch {namespace delete {*}[namespace children :: test_ns_*]}
328    catch {rename MakeLink ""}
329    namespace eval ::test_ns_1 {}
330    proc MakeLink {a} {
331        namespace eval ::test_ns_1 {
332            upvar a a
333        }
334        unset ::test_ns_1::a
335    }
336    list [catch {MakeLink 1} msg] $msg
337} {1 {bad variable name "a": upvar won't create namespace variable that refers to procedure variable}}
338test upvar-8.10 {upvar will create element alias for new array element} {
339    catch {unset upvarArray}
340    array set upvarArray {}
341    catch {upvar 0 upvarArray(elem) upvarArrayElemAlias}
342} {0}
343test upvar-8.11 {upvar will not create a variable that looks like an array} -body {
344    catch {unset upvarArray}
345    array set upvarArray {}
346    upvar 0 upvarArray(elem) upvarArrayElemAlias(elem)
347} -returnCodes 1 -match glob -result *
348
349test upvar-9.1 {Tcl_UpVar2 procedure} testupvar {
350    list [catch {testupvar xyz a {} x global} msg] $msg
351} {1 {bad level "xyz"}}
352test upvar-9.2 {Tcl_UpVar2 procedure} testupvar {
353    catch {unset a}
354    catch {unset x}
355    set a 44
356    list [catch "testupvar #0 a 1 x global" msg] $msg
357} {1 {can't access "a(1)": variable isn't array}}
358test upvar-9.3 {Tcl_UpVar2 procedure} testupvar {
359    proc foo {} {
360        testupvar 1 a {} x local
361        set x
362    }
363    catch {unset a}
364    catch {unset x}
365    set a 44
366    foo
367} {44}
368test upvar-9.4 {Tcl_UpVar2 procedure} testupvar {
369    proc foo {} {
370        testupvar 1 a {} _up_ global
371        list [catch {set x} msg] $msg
372    }
373    catch {unset a}
374    catch {unset _up_}
375    set a 44
376    concat [foo] $_up_
377} {1 {can't read "x": no such variable} 44}
378test upvar-9.5 {Tcl_UpVar2 procedure} testupvar {
379    proc foo {} {
380        testupvar 1 a b x local
381        set x
382    }
383    catch {unset a}
384    catch {unset x}
385    set a(b) 1234
386    foo
387} {1234}
388test upvar-9.6 {Tcl_UpVar procedure} testupvar {
389    proc foo {} {
390        testupvar 1 a x local
391        set x
392    }
393    catch {unset a}
394    catch {unset x}
395    set a xyzzy
396    foo
397} {xyzzy}
398test upvar-9.7 {Tcl_UpVar procedure} testupvar {
399    proc foo {} {
400        testupvar #0 a(b) x local
401        set x
402    }
403    catch {unset a}
404    catch {unset x}
405    set a(b) 1234
406    foo
407} {1234}
408catch {unset a}
409
410
411#
412# Tests for 'namespace upvar'. As the implementation is essentially the same as
413# for 'upvar', we only test that the variables are linked correctly. Ie, we
414# assume that the behaviour of variables once the link is established has
415# already been tested above.
416#
417#
418
419# Clear out any namespaces called test_ns_*
420catch {namespace delete {*}[namespace children :: test_ns_*]}
421
422namespace eval test_ns_0 {
423    variable x test_ns_0
424}
425
426set x test_global
427
428test upvar-NS-1.1 {nsupvar links to correct variable} \
429    -body {
430        namespace eval test_ns_1 {
431            namespace upvar ::test_ns_0 x w
432            set w
433        }
434    } \
435    -result {test_ns_0} \
436    -cleanup {namespace delete test_ns_1}
437
438test upvar-NS-1.2 {nsupvar links to correct variable} \
439    -body {
440        namespace eval test_ns_1 {
441            proc a {} {
442                namespace upvar ::test_ns_0 x w
443                set w
444            }
445            return [a]
446        }
447    } \
448    -result {test_ns_0} \
449    -cleanup {namespace delete test_ns_1}
450
451test upvar-NS-1.3 {nsupvar links to correct variable} \
452    -body {
453        namespace eval test_ns_1 {
454            namespace upvar test_ns_0 x w
455            set w
456        }
457    } \
458    -result {namespace "test_ns_0" not found in "::test_ns_1"} \
459    -returnCodes error \
460    -cleanup {namespace delete test_ns_1}
461
462test upvar-NS-1.4 {nsupvar links to correct variable} \
463    -body {
464        namespace eval test_ns_1 {
465            proc a {} {
466                namespace upvar test_ns_0 x w
467                set w
468            }
469            return [a]
470        }
471    } \
472    -result {namespace "test_ns_0" not found in "::test_ns_1"} \
473    -returnCodes error \
474    -cleanup {namespace delete test_ns_1}
475
476test upvar-NS-1.5 {nsupvar links to correct variable} \
477    -body {
478        namespace eval test_ns_1 {
479            namespace eval test_ns_0 {}
480            namespace upvar test_ns_0 x w
481            set w
482        }
483    } \
484    -result {can't read "w": no such variable} \
485    -returnCodes error \
486    -cleanup {namespace delete test_ns_1}
487
488test upvar-NS-1.6 {nsupvar links to correct variable} \
489    -body {
490        namespace eval test_ns_1 {
491            namespace eval test_ns_0 {}
492            proc a {} {
493                namespace upvar test_ns_0 x w
494                set w
495            }
496            return [a]
497        }
498    } \
499    -result {can't read "w": no such variable} \
500    -returnCodes error \
501    -cleanup {namespace delete test_ns_1}
502
503test upvar-NS-1.7 {nsupvar links to correct variable} \
504    -body {
505        namespace eval test_ns_1 {
506            namespace eval test_ns_0 {
507                variable x test_ns_1::test_ns_0
508            }
509            namespace upvar test_ns_0 x w
510            set w
511        }
512    } \
513    -result {test_ns_1::test_ns_0} \
514    -cleanup {namespace delete test_ns_1}
515
516test upvar-NS-1.8 {nsupvar links to correct variable} \
517    -body {
518        namespace eval test_ns_1 {
519            namespace eval test_ns_0 {
520                variable x test_ns_1::test_ns_0
521            }
522            proc a {} {
523                namespace upvar test_ns_0 x w
524                set w
525            }
526            return [a]
527        }
528    } \
529    -result {test_ns_1::test_ns_0} \
530    -cleanup {namespace delete test_ns_1}
531
532test upvar-NS-1.9 {nsupvar links to correct variable} \
533    -body {
534        namespace eval test_ns_1 {
535            variable x test_ns_1
536            proc a {} {
537                namespace upvar test_ns_0 x w
538                set w
539            }
540            return [a]
541        }
542    } \
543    -result {namespace "test_ns_0" not found in "::test_ns_1"} \
544    -returnCodes error \
545    -cleanup {namespace delete test_ns_1}
546
547
548# cleanup
549::tcltest::cleanupTests
550return
Note: See TracBrowser for help on using the repository browser.