# -*- Tcl -*- package req nx package require nx::test namespace import ::nx::* # # The first test set checks just the basic behavior: # Test case cget-simple { nx::Class create Person { :property famnam:required :property {age:integer,required 0} :property {friends:0..n ""} :property sex # Create an instance of the class :create p1 -famnam hugo -age 25 } # # first, check basic provided values and defaults # ? {p1 cget -age} 25 ? {p1 cget -famnam} hugo ? {p1 cget -friends} "" # # a method property ? {p1 cget -class} ::Person # # error handling: # - wrong # args # - wrong parameter # - parameter without a value # ? {p1 cget} {wrong # of arguments: should be "cget /name/"} ? {p1 cget -foo} {cannot lookup parameter value for -foo} ? {p1 cget foo} {cannot lookup parameter value for foo} ? {p1 cget -sex} {can't read "sex": no such variable} # # Reconfigure the object # ? {p1 configure -famnam joe -age 27} "" # # check the new values # ? {p1 cget -age} 27 ? {p1 cget -famnam} joe # # configure without arguments # ? {p1 configure} "?-sex /value/? -famnam /value/ ?-age /integer/? ?-friends /value .../? ?-volatile? ?-noinit? ?-object-mixin /mixinreg .../? ?-class /class/? ?-object-filter /filterreg .../? ?/__initcmd/?" } # # The second test set checks redirection of configure / cget to slot # methods "assign" and "get". # Test parameter count 1 Test case cget-via-slot { nx::Class create C { # Define a property with a "get" method :property bar1 { :public object method get { object property} { incr ::count(cget) nsf::var::set $object $property } } # Define a property with a "get" and "assign" method :property bar2 { :public object method get { object property} { incr ::count(cget) nsf::var::set $object $property } :public object method assign { object property value } { incr ::count(assign) nsf::var::set $object $property $value } } # Create an instance of the class :create p1 } # # configure without arguments # ? {p1 configure} "?-bar1 /value/? ?-bar2 /value/? ?-volatile? ?-noinit? ?-object-mixin /mixinreg .../? ?-class /class/? ?-object-filter /filterreg .../? ?/__initcmd/?" # # test gettin/setting via slots # # just a getter: # array unset ::count ? {p1 configure -bar1 100} "" ? {array get ::count} "" ? {p1 cget -bar1} 100 ? {array get ::count} "cget 1" # a getter and a setter: # array unset ::count ? {p1 configure -bar2 100} "" ? {array get ::count} "assign 1" ? {p1 cget -bar2} 100 ? {array get ::count} "assign 1 cget 1" } # # The third test set checks method binding to parameter: # All cmds are supposed to return resonable values. # Test case cget-parameter-methods { nx::Class create C { :property {foo:alias,method=m0 {1 2 3}} :property {{bar:forward,method=%self m1 a b c %method} bar1} :public method m0 {args} {set :m0 $args; return $args} :public method m1 {args} {set :m1 $args; return $args} :create c1 } # # class-level lookup # ? {C info lookup parameter list} \ "-superclass -mixin -filter -volatile -noinit -object-mixin -class -object-filter __initcmd" ? {C cget -superclass} "::nx::Object" ? {C cget -object-mixin} "" ? {C cget -mixin} "" ? {C cget -filter} "" ? {C cget -volatile} 0 ? {C cget -noinit} "" ? {C cget -class} "::nx::Class" # # object-level lookup # ? {c1 info lookup parameter list} \ "-foo -bar -volatile -noinit -object-mixin -class -object-filter __initcmd" # # query all properties from base classes # ? {c1 cget -volatile} 0 ? {c1 cget -noinit} "" #? {c1 cget -mixin} "" ? {c1 cget -object-mixin} "" ? {c1 cget -class} ::C #? {c1 cget -filter} "" ? {c1 cget -object-filter} "" # # query alias and forward # ? {c1 eval {set :m0}} "{1 2 3}" ? {c1 eval {set :m1}} {a b c bar bar1} ? {c1 cget -foo} "" ? {c1 cget -bar} "a b c bar" } # # The fourth test set checks performance of "cget" and "configure". # nx::Test parameter count 10000 Test case cget-performance { nx::Class create Person { :property famnam:required :property -accessor public {age:integer,required 0} :property {friends:0..n ""} :property sex # Define a property with a "get" and "assign" method :property bar { :public object method get { object property } { nsf::var::set $object $property } :public object method assign { object property value } { nsf::var::set $object $property $value } } # Create an instance of the class :create p1 -famnam hugo -age 25 -bar 101 } # # read properties # - built-in accessor # - cget # - dispatch of cget method with full path # - cget via slot method ? {p1 age} 25 ? {p1 cget -age} 25 ? {p1 ::nsf::methods::object::cget -age} 25 ? {p1 cget -bar} 101 # # write properties: # - built-in accessor # - configure # - configure via slot method ? {p1 age 27} 27 ? {p1 configure -age 27} "" ? {p1 configure -bar 102} "" }