Index: tests/varresolutiontest.xotcl =================================================================== diff -u -rf9807b1cea03590c9573b5a521760538d53ee90f -re61fc14f5c25172a1d1f93bea03be54a772fb4b5 --- tests/varresolutiontest.xotcl (.../varresolutiontest.xotcl) (revision f9807b1cea03590c9573b5a521760538d53ee90f) +++ tests/varresolutiontest.xotcl (.../varresolutiontest.xotcl) (revision e61fc14f5c25172a1d1f93bea03be54a772fb4b5) @@ -1,9 +1,8 @@ # testing var resolution for namespace-shadowed objects -package require XOTcl; xotcl::use xotcl1 +package require XOTcl; xotcl::use xotcl2 package require xotcl::test - proc ? {cmd expected {msg ""}} { set count 1 if {$msg ne ""} { @@ -15,13 +14,21 @@ $t run } +::xotcl::alias ::xotcl2::Object eval -objscope ::eval +::xotcl::alias ::xotcl2::Object array -objscope ::array +::xotcl::alias ::xotcl2::Object lappend -objscope ::lappend +::xotcl::alias ::xotcl2::Object incr -objscope ::incr +::xotcl::alias ::xotcl2::Object set -objscope ::set +::xotcl::alias ::xotcl2::Object unset -objscope ::unset + ########################################### # Basic tests for var resolution under # per-object namespaces ... ########################################### set ::globalVar 1 -Object o -requireNamespace +Object create o +o requireNamespace ? {o info vars} "" ? {info exists ::globalVar} 1 ? {set ::globalVar} 1 @@ -40,22 +47,24 @@ ########################################### # scopes ########################################### - -Object o -eval { - my requireNamespace - global z - my instvar y +Object create o +o requireNamespace +o eval { + # TODO: the next three lines don't seem to work as expected + #my requireNamespace + #global z + #::xotcl::importvar [self] y set x 1 - set y 2 - set z 3 + set .y 2 + set ::z 3 set [self]::X 4 } set ::o::Y 5 ? {info exists ::z} 1 ? {set ::z} 3 -? {lsort [o info vars]} {X Y y} -? {o exists x} 0 +? {lsort [o info vars]} {X Y x y} +? {o exists x} 1 ? {o exists y} 1 ? {o exists z} 0 ? {o exists X} 1 @@ -69,7 +78,8 @@ # mix & match namespace and object interfaces ########################################### -Object o -requireNamespace +Object create o +o requireNamespace o set x 1 ? {namespace eval ::o set x} 1 @@ -89,7 +99,8 @@ # array-specific tests ########################################### -Object o -requireNamespace +Object create o +o requireNamespace ? {o array exists a} 0 ? {namespace eval ::o array exists a} 0 @@ -113,8 +124,10 @@ # tests on namespace-qualified var names ########################################### -Object o -requireNamespace -Object o::oo -requireNamespace +Object create o +o requireNamespace +Object create o::oo +o::oo requireNamespace ? {::o set ::x 1} 1 ? {info exists ::x} [set ::x] @@ -142,7 +155,7 @@ # the tests below fail. We could consider # to require namespaces on the fly in the future -Object o +Object create o #? {::o set ::o::x 1} 1 #? {o exists x} [::o set ::o::x] #? {namespace eval ::o unset x} "" @@ -159,7 +172,7 @@ # tests for the compiled var resolver on Object ############################################### -Object o +Object create o o method foo {x} {set .y 2; return ${.x},${.y}} o method bar {} {return ${.x},${.y}} o set x 1 @@ -168,7 +181,7 @@ ? {o info vars} "x y" # recreate object, check var caching; # we have to recreate bar, so no problem -Object o +Object create o o set x 1 o method bar {} {return ${.x},${.y}} ? {catch {o bar}} "1" "compiled var y should not exist" @@ -248,7 +261,7 @@ ############################################### # tests for the var resolver ############################################### -Class C +Class create C C method bar0 {} {return ${.x}} C method bar1 {} {set a ${.x}; return [info exists .x],[info exists .y]} C method bar2 {} {return [info exists .x],[info exists .y]} @@ -270,12 +283,12 @@ ############################################### # first tests for the cmd resolver ############################################### -Class C +Class create C C method bar {args} { #puts stderr "[self] bar called with [list $args]" return $args } -C instforward test %self bar +C forward test %self bar C method foo {} { # this works lappend .r [.bar x 1] @@ -307,7 +320,7 @@ ::C create ::c namespace eval ::c {} ? {namespace exists ::c} 1 -? {::xotcl::Object isobject ::c} 1 +? {::xotcl::is ::c object} 1 ? {::c info hasnamespace} 0 ? {::c Set w 2; expr {[::c Set w] == $::w}} 0 @@ -318,4 +331,138 @@ ::c destroy ::C destroy unset ::w -unset ::tmpArray \ No newline at end of file +unset ::tmpArray + +################################################## +# Testing aliases for eval with and without flags +# +# -objscope, +# -nonleaf +# +# with a required namespace and without +################################################## + +::xotcl::alias ::xotcl2::Object eval -objscope ::eval +::xotcl::alias ::xotcl2::Object softeval -nonleaf ::eval +::xotcl::alias ::xotcl2::Object softeval2 ::eval + +Object create o { + set xxx 1 + set .x 1 +} +? {o exists x} 1 +? {o exists xxx} 0 + +o eval { + set aaa 1 + set .a 1 +} +? {o exists a} 1 +? {o exists aaa} 1 + +o softeval { + set bbb 1 + set .b 1 +} +? {o exists b} 1 +? {o exists bbb} 1 + +# softeval2 should not set variables +o softeval2 { + set zzz 1 + set .z 1 +} +? {o exists z} 0 +? {o exists zzz} 0 + +? {lsort [o info vars]} "a aaa b bbb x" + +o requireNamespace + +o eval { + set ccc 1 + set .c 1 +} +? {o exists c} 1 +? {o exists ccc} 1 + +o softeval { + set ddd 1 + set .d 1 +} +? {o exists d} 1 +? {o exists ddd} 1 + +# softeval2 should not set variables +o softeval2 { + set zzz 1 + set .z 1 +} +? {o exists z} 0 +? {o exists zzz} 0 +? {lsort [o info vars]} "a aaa b bbb c ccc d ddd x" + + +################################################## +# The same as above, but with some global vars. +# The global vars should not influence the behavior. +################################################## +foreach var {.x x xxx .a a aaa .b b bbb .c c ccc .d d ddd .z z zzz} {set $var 1} + +Object create o { + set xxx 1 + set .x 1 +} + +? {o exists x} 1 +# TODO: this should be +#? {o exists xxx} 0 +#? {lsort [o info vars]} "x" +? {o exists xxx} 1 +? {lsort [o info vars]} "x xxx" + +o eval { + set aaa 1 + set .a 1 +} +? {o exists a} 1 +? {o exists aaa} 1 + +o softeval { + set bbb 1 + set .b 1 +} +? {o exists b} 1 +? {o exists bbb} 1 + +# softeval2 should not set variables +o softeval2 { + set zzz 1 + set .z 1 +} +? {o exists z} 0 +? {o exists zzz} 0 + +o requireNamespace + +o eval { + set ccc 1 + set .c 1 +} +? {o exists c} 1 +? {o exists ccc} 1 + +o softeval { + set ddd 1 + set .d 1 +} +? {o exists d} 1 +? {o exists ddd} 1 + +# softeval2 should not set variables +o softeval2 { + set zzz 1 + set .z 1 +} +? {o exists z} 0 +? {o exists zzz} 0