#
|
#
|
# Basic tests for class definition and method/proc access
|
# Basic tests for class definition and method/proc access
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# AUTHOR: Michael J. McLennan
|
# AUTHOR: Michael J. McLennan
|
# Bell Labs Innovations for Lucent Technologies
|
# Bell Labs Innovations for Lucent Technologies
|
# mmclennan@lucent.com
|
# mmclennan@lucent.com
|
# http://www.tcltk.com/itcl
|
# http://www.tcltk.com/itcl
|
#
|
#
|
# RCS: $Id: basic.test,v 1.1.1.1 2002-01-16 10:24:47 markom Exp $
|
# RCS: $Id: basic.test,v 1.1.1.1 2002-01-16 10:24:47 markom Exp $
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
|
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
|
# ======================================================================
|
# ======================================================================
|
# See the file "license.terms" for information on usage and
|
# See the file "license.terms" for information on usage and
|
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
|
|
if {[string compare test [info procs test]] == 1} then {source defs}
|
if {[string compare test [info procs test]] == 1} then {source defs}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# Simple class definition
|
# Simple class definition
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test basic-1.1 {define a simple class} {
|
test basic-1.1 {define a simple class} {
|
itcl::class Counter {
|
itcl::class Counter {
|
constructor {args} {
|
constructor {args} {
|
incr num
|
incr num
|
eval configure $args
|
eval configure $args
|
}
|
}
|
destructor {
|
destructor {
|
incr num -1
|
incr num -1
|
}
|
}
|
|
|
method ++ {} {
|
method ++ {} {
|
return [incr val $by]
|
return [incr val $by]
|
}
|
}
|
proc num {} {
|
proc num {} {
|
return $num
|
return $num
|
}
|
}
|
public variable by 1
|
public variable by 1
|
protected variable val 0
|
protected variable val 0
|
private common num 0
|
private common num 0
|
}
|
}
|
} ""
|
} ""
|
|
|
test basic-1.2 {class is now defined} {
|
test basic-1.2 {class is now defined} {
|
find classes Counter
|
find classes Counter
|
} {Counter}
|
} {Counter}
|
|
|
test basic-1.3 {access command exists with class name} {
|
test basic-1.3 {access command exists with class name} {
|
namespace which -command Counter
|
namespace which -command Counter
|
} {::Counter}
|
} {::Counter}
|
|
|
test basic-1.4 {create a simple object} {
|
test basic-1.4 {create a simple object} {
|
Counter x
|
Counter x
|
} {x}
|
} {x}
|
|
|
test basic-1.5a {object names cannot be duplicated} {
|
test basic-1.5a {object names cannot be duplicated} {
|
list [catch "Counter x" msg] $msg
|
list [catch "Counter x" msg] $msg
|
} {1 {command "x" already exists in namespace "::"}}
|
} {1 {command "x" already exists in namespace "::"}}
|
|
|
test basic-1.5b {built-in commands cannot be clobbered} {
|
test basic-1.5b {built-in commands cannot be clobbered} {
|
list [catch "Counter info" msg] $msg
|
list [catch "Counter info" msg] $msg
|
} {1 {command "info" already exists in namespace "::"}}
|
} {1 {command "info" already exists in namespace "::"}}
|
|
|
test basic-1.6 {objects have an access command} {
|
test basic-1.6 {objects have an access command} {
|
namespace which -command x
|
namespace which -command x
|
} {::x}
|
} {::x}
|
|
|
test basic-1.7a {objects are added to the master list} {
|
test basic-1.7a {objects are added to the master list} {
|
find objects x
|
find objects x
|
} {x}
|
} {x}
|
|
|
test basic-1.7b {objects are added to the master list} {
|
test basic-1.7b {objects are added to the master list} {
|
find objects -class Counter x
|
find objects -class Counter x
|
} {x}
|
} {x}
|
|
|
test basic-1.8 {objects can be deleted} {
|
test basic-1.8 {objects can be deleted} {
|
list [delete object x] [namespace which -command x]
|
list [delete object x] [namespace which -command x]
|
} {{} {}}
|
} {{} {}}
|
|
|
test basic-1.9 {objects can be recreated with the same name} {
|
test basic-1.9 {objects can be recreated with the same name} {
|
Counter x
|
Counter x
|
} {x}
|
} {x}
|
|
|
test basic-1.10 {objects can be destroyed by deleting their access command} {
|
test basic-1.10 {objects can be destroyed by deleting their access command} {
|
rename ::x ""
|
rename ::x ""
|
find objects x
|
find objects x
|
} {}
|
} {}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# #auto names
|
# #auto names
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test basic-2.1 {create an object with an automatic name} {
|
test basic-2.1 {create an object with an automatic name} {
|
Counter #auto
|
Counter #auto
|
} {counter0}
|
} {counter0}
|
|
|
test basic-2.2 {bury "#auto" within object name} {
|
test basic-2.2 {bury "#auto" within object name} {
|
Counter x#autoy
|
Counter x#autoy
|
} {xcounter1y}
|
} {xcounter1y}
|
|
|
test basic-2.3 {bury "#auto" within object name} {
|
test basic-2.3 {bury "#auto" within object name} {
|
Counter a#aut#autob
|
Counter a#aut#autob
|
} {a#autcounter2b}
|
} {a#autcounter2b}
|
|
|
test basic-2.4 {"#auto" is smart enough to skip names that are taken} {
|
test basic-2.4 {"#auto" is smart enough to skip names that are taken} {
|
Counter counter3
|
Counter counter3
|
Counter #auto
|
Counter #auto
|
} {counter4}
|
} {counter4}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# Simple object use
|
# Simple object use
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test basic-3.1 {object access command works} {
|
test basic-3.1 {object access command works} {
|
Counter c
|
Counter c
|
list [c ++] [c ++] [c ++]
|
list [c ++] [c ++] [c ++]
|
} {1 2 3}
|
} {1 2 3}
|
|
|
test basic-3.2 {errors produce usage info} {
|
test basic-3.2 {errors produce usage info} {
|
list [catch "c xyzzy" msg] $msg
|
list [catch "c xyzzy" msg] $msg
|
} {1 {bad option "xyzzy": should be one of...
|
} {1 {bad option "xyzzy": should be one of...
|
c ++
|
c ++
|
c cget -option
|
c cget -option
|
c configure ?-option? ?value -option value...?
|
c configure ?-option? ?value -option value...?
|
c isa className}}
|
c isa className}}
|
|
|
test basic-3.3 {built-in configure can query public variables} {
|
test basic-3.3 {built-in configure can query public variables} {
|
c configure
|
c configure
|
} {{-by 1 1}}
|
} {{-by 1 1}}
|
|
|
test basic-3.4 {built-in configure can query one public variable} {
|
test basic-3.4 {built-in configure can query one public variable} {
|
c configure -by
|
c configure -by
|
} {-by 1 1}
|
} {-by 1 1}
|
|
|
test basic-3.5 {built-in configure can set public variable} {
|
test basic-3.5 {built-in configure can set public variable} {
|
list [c configure -by 2] [c cget -by]
|
list [c configure -by 2] [c cget -by]
|
} {{} 2}
|
} {{} 2}
|
|
|
test basic-3.6 {configure actually changes public variable} {
|
test basic-3.6 {configure actually changes public variable} {
|
list [c ++] [c ++]
|
list [c ++] [c ++]
|
} {5 7}
|
} {5 7}
|
|
|
test basic-3.7 {class procs can be accessed} {
|
test basic-3.7 {class procs can be accessed} {
|
Counter::num
|
Counter::num
|
} {6}
|
} {6}
|
|
|
test basic-3.8 {obsolete syntax is no longer allowed} {
|
test basic-3.8 {obsolete syntax is no longer allowed} {
|
list [catch "Counter :: num" msg] $msg
|
list [catch "Counter :: num" msg] $msg
|
} {1 {syntax "class :: proc" is an anachronism
|
} {1 {syntax "class :: proc" is an anachronism
|
[incr Tcl] no longer supports this syntax.
|
[incr Tcl] no longer supports this syntax.
|
Instead, remove the spaces from your procedure invocations:
|
Instead, remove the spaces from your procedure invocations:
|
Counter::num ?args?}}
|
Counter::num ?args?}}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# Classes can be destroyed and redefined
|
# Classes can be destroyed and redefined
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test basic-4.1 {classes can be destroyed} {
|
test basic-4.1 {classes can be destroyed} {
|
list [delete class Counter] \
|
list [delete class Counter] \
|
[find classes Counter] \
|
[find classes Counter] \
|
[namespace children :: Counter] \
|
[namespace children :: Counter] \
|
[namespace which -command Counter]
|
[namespace which -command Counter]
|
} {{} {} {} {}}
|
} {{} {} {} {}}
|
|
|
test basic-4.2 {classes can be redefined} {
|
test basic-4.2 {classes can be redefined} {
|
itcl::class Counter {
|
itcl::class Counter {
|
method ++ {} {
|
method ++ {} {
|
return [incr val $by]
|
return [incr val $by]
|
}
|
}
|
public variable by 1
|
public variable by 1
|
protected variable val 0
|
protected variable val 0
|
}
|
}
|
} {}
|
} {}
|
|
|
test basic-4.3 {the redefined class is actually different} {
|
test basic-4.3 {the redefined class is actually different} {
|
list [catch "Counter::num" msg] $msg
|
list [catch "Counter::num" msg] $msg
|
} {1 {invalid command name "Counter::num"}}
|
} {1 {invalid command name "Counter::num"}}
|
|
|
test basic-4.4 {objects can be created from the new class} {
|
test basic-4.4 {objects can be created from the new class} {
|
list [Counter #auto] [Counter #auto]
|
list [Counter #auto] [Counter #auto]
|
} {counter0 counter1}
|
} {counter0 counter1}
|
|
|
test basic-4.5 {when a class is destroyed, its objects are deleted} {
|
test basic-4.5 {when a class is destroyed, its objects are deleted} {
|
list [lsort [find objects counter*]] \
|
list [lsort [find objects counter*]] \
|
[delete class Counter] \
|
[delete class Counter] \
|
[lsort [find objects counter*]]
|
[lsort [find objects counter*]]
|
} {{counter0 counter1} {} {}}
|
} {{counter0 counter1} {} {}}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# Namespace variables
|
# Namespace variables
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test basic-5.1 {define a simple class with variables in the namespace} {
|
test basic-5.1 {define a simple class with variables in the namespace} {
|
itcl::class test_globals {
|
itcl::class test_globals {
|
common g1 "global1"
|
common g1 "global1"
|
proc getval {name} {
|
proc getval {name} {
|
variable $name
|
variable $name
|
return [set [namespace tail $name]]
|
return [set [namespace tail $name]]
|
}
|
}
|
proc setval {name val} {
|
proc setval {name val} {
|
variable $name
|
variable $name
|
return [set [namespace tail $name] $val]
|
return [set [namespace tail $name] $val]
|
}
|
}
|
method do {args} {
|
method do {args} {
|
return [eval $args]
|
return [eval $args]
|
}
|
}
|
}
|
}
|
namespace eval test_globals {
|
namespace eval test_globals {
|
variable g2 "global2"
|
variable g2 "global2"
|
}
|
}
|
} ""
|
} ""
|
|
|
test basic-5.2 {create an object for the tests} {
|
test basic-5.2 {create an object for the tests} {
|
test_globals #auto
|
test_globals #auto
|
} {test_globals0}
|
} {test_globals0}
|
|
|
test basic-5.3 {common variables live in the namespace} {
|
test basic-5.3 {common variables live in the namespace} {
|
lsort [info vars ::test_globals::*]
|
lsort [info vars ::test_globals::*]
|
} {::test_globals::g1 ::test_globals::g2}
|
} {::test_globals::g1 ::test_globals::g2}
|
|
|
test basic-5.4 {common variables can be referenced transparently} {
|
test basic-5.4 {common variables can be referenced transparently} {
|
list [catch {test_globals0 do set g1} msg] $msg
|
list [catch {test_globals0 do set g1} msg] $msg
|
} {0 global1}
|
} {0 global1}
|
|
|
test basic-5.5 {namespace variables require a declaration} {
|
test basic-5.5 {namespace variables require a declaration} {
|
list [catch {test_globals0 do set g2} msg] $msg
|
list [catch {test_globals0 do set g2} msg] $msg
|
} {1 {can't read "g2": no such variable}}
|
} {1 {can't read "g2": no such variable}}
|
|
|
test basic-5.6a {variable accesses variables within namespace} {
|
test basic-5.6a {variable accesses variables within namespace} {
|
list [catch {test_globals::getval g1} msg] $msg
|
list [catch {test_globals::getval g1} msg] $msg
|
} {0 global1}
|
} {0 global1}
|
|
|
test basic-5.6a {variable accesses variables within namespace} {
|
test basic-5.6a {variable accesses variables within namespace} {
|
list [catch {test_globals::getval g2} msg] $msg
|
list [catch {test_globals::getval g2} msg] $msg
|
} {0 global2}
|
} {0 global2}
|
|
|
test basic-5.7 {variable command will not find vars in other namespaces} {
|
test basic-5.7 {variable command will not find vars in other namespaces} {
|
set ::test_global_0 "g0"
|
set ::test_global_0 "g0"
|
list [catch {test_globals::getval test_global_0} msg] $msg \
|
list [catch {test_globals::getval test_global_0} msg] $msg \
|
[catch {test_globals::getval ::test_global_0} msg] $msg \
|
[catch {test_globals::getval ::test_global_0} msg] $msg \
|
} {1 {can't read "test_global_0": no such variable} 0 g0}
|
} {1 {can't read "test_global_0": no such variable} 0 g0}
|
|
|
test basic-5.8 {to create globals in a namespace, use the full path} {
|
test basic-5.8 {to create globals in a namespace, use the full path} {
|
test_globals::setval ::test_global_1 g1
|
test_globals::setval ::test_global_1 g1
|
namespace eval :: {lsort [info globals test_global_*]}
|
namespace eval :: {lsort [info globals test_global_*]}
|
} {test_global_0 test_global_1}
|
} {test_global_0 test_global_1}
|
|
|
test basic-5.9 {variable names can have ":" in them} {
|
test basic-5.9 {variable names can have ":" in them} {
|
test_globals::setval ::test:global:2 g2
|
test_globals::setval ::test:global:2 g2
|
namespace eval :: {info globals test:global:2}
|
namespace eval :: {info globals test:global:2}
|
} {test:global:2}
|
} {test:global:2}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# Array variables
|
# Array variables
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test basic-6.1 {set up a class definition with array variables} {
|
test basic-6.1 {set up a class definition with array variables} {
|
proc test_arrays_get {name} {
|
proc test_arrays_get {name} {
|
upvar $name x
|
upvar $name x
|
set rlist {}
|
set rlist {}
|
foreach index [lsort [array names x]] {
|
foreach index [lsort [array names x]] {
|
lappend rlist [list $index $x($index)]
|
lappend rlist [list $index $x($index)]
|
}
|
}
|
return $rlist
|
return $rlist
|
}
|
}
|
itcl::class test_arrays {
|
itcl::class test_arrays {
|
variable nums
|
variable nums
|
common undefined
|
common undefined
|
|
|
common colors
|
common colors
|
set colors(red) #ff0000
|
set colors(red) #ff0000
|
set colors(green) #00ff00
|
set colors(green) #00ff00
|
set colors(blue) #0000ff
|
set colors(blue) #0000ff
|
|
|
constructor {} {
|
constructor {} {
|
set nums(one) 1
|
set nums(one) 1
|
set nums(two) 2
|
set nums(two) 2
|
set nums(three) 3
|
set nums(three) 3
|
|
|
set undefined(a) A
|
set undefined(a) A
|
set undefined(b) B
|
set undefined(b) B
|
}
|
}
|
method do {args} {
|
method do {args} {
|
return [eval $args]
|
return [eval $args]
|
}
|
}
|
}
|
}
|
test_arrays #auto
|
test_arrays #auto
|
} {test_arrays0}
|
} {test_arrays0}
|
|
|
test basic-6.2 {test array access for instance variables} {
|
test basic-6.2 {test array access for instance variables} {
|
lsort [test_arrays0 do array get nums]
|
lsort [test_arrays0 do array get nums]
|
} {1 2 3 one three two}
|
} {1 2 3 one three two}
|
|
|
test basic-6.3 {test array access for commons} {
|
test basic-6.3 {test array access for commons} {
|
lsort [test_arrays0 do array get colors]
|
lsort [test_arrays0 do array get colors]
|
} {#0000ff #00ff00 #ff0000 blue green red}
|
} {#0000ff #00ff00 #ff0000 blue green red}
|
|
|
test basic-6.4 {test array access for instance variables via "upvar"} {
|
test basic-6.4 {test array access for instance variables via "upvar"} {
|
test_arrays0 do test_arrays_get nums
|
test_arrays0 do test_arrays_get nums
|
} {{one 1} {three 3} {two 2}}
|
} {{one 1} {three 3} {two 2}}
|
|
|
test basic-6.5 {test array access for commons via "upvar"} {
|
test basic-6.5 {test array access for commons via "upvar"} {
|
test_arrays0 do test_arrays_get colors
|
test_arrays0 do test_arrays_get colors
|
} {{blue #0000ff} {green #00ff00} {red #ff0000}}
|
} {{blue #0000ff} {green #00ff00} {red #ff0000}}
|
|
|
test basic-6.6a {test array access for commons defined in constructor} {
|
test basic-6.6a {test array access for commons defined in constructor} {
|
lsort [test_arrays0 do array get undefined]
|
lsort [test_arrays0 do array get undefined]
|
} {A B a b}
|
} {A B a b}
|
|
|
test basic-6.6b {test array access for commons defined in constructor} {
|
test basic-6.6b {test array access for commons defined in constructor} {
|
test_arrays0 do test_arrays_get undefined
|
test_arrays0 do test_arrays_get undefined
|
} {{a A} {b B}}
|
} {{a A} {b B}}
|
|
|
test basic-6.6c {test array access for commons defined in constructor} {
|
test basic-6.6c {test array access for commons defined in constructor} {
|
list [test_arrays0 do set undefined(a)] [test_arrays0 do set undefined(b)]
|
list [test_arrays0 do set undefined(a)] [test_arrays0 do set undefined(b)]
|
} {A B}
|
} {A B}
|
|
|
test basic-6.7 {common variables can be unset} {
|
test basic-6.7 {common variables can be unset} {
|
test_arrays0 do unset undefined
|
test_arrays0 do unset undefined
|
test_arrays0 do array names undefined
|
test_arrays0 do array names undefined
|
} {}
|
} {}
|
|
|
test basic-6.8 {common variables can be redefined} {
|
test basic-6.8 {common variables can be redefined} {
|
test_arrays0 do set undefined "scalar"
|
test_arrays0 do set undefined "scalar"
|
} {scalar}
|
} {scalar}
|
|
|