#
|
#
|
# Tests for inheritance and scope handling
|
# Tests for inheritance and scope handling
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# 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: inherit.test,v 1.1.1.1 2002-01-16 10:24:47 markom Exp $
|
# RCS: $Id: inherit.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.
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# MULTIPLE BASE-CLASS ERROR DETECTION
|
# MULTIPLE BASE-CLASS ERROR DETECTION
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {Cannot inherit from the same base class more than once} {
|
test {Cannot inherit from the same base class more than once} {
|
catch "VirtualErr" errmsg
|
catch "VirtualErr" errmsg
|
set errmsg
|
set errmsg
|
} {
|
} {
|
[string match {*class "::VirtualErr" inherits base class "::Foo" more than once:
|
[string match {*class "::VirtualErr" inherits base class "::Foo" more than once:
|
VirtualErr->Mongrel->FooBar->Foo
|
VirtualErr->Mongrel->FooBar->Foo
|
VirtualErr->Foo
|
VirtualErr->Foo
|
VirtualErr->BarFoo->Foo} $result]
|
VirtualErr->BarFoo->Foo} $result]
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# CONSTRUCTION
|
# CONSTRUCTION
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {Constructors should be invoked implicitly} {
|
test {Constructors should be invoked implicitly} {
|
set WATCH ""
|
set WATCH ""
|
concat [Mongrel m] / $WATCH
|
concat [Mongrel m] / $WATCH
|
} {
|
} {
|
$result == "m / ::Geek ::Bar ::Foo ::FooBar ::Mongrel"
|
$result == "m / ::Geek ::Bar ::Foo ::FooBar ::Mongrel"
|
}
|
}
|
|
|
test {Initialization of shadowed variables works properly} {
|
test {Initialization of shadowed variables works properly} {
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
} {
|
} {
|
$result == "nonnull / "
|
$result == "nonnull / "
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# PUBLIC VARIABLES
|
# PUBLIC VARIABLES
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {Inherited "config" method works on derived classes} {
|
test {Inherited "config" method works on derived classes} {
|
m config -blit xyz -Foo::blit pdq
|
m config -blit xyz -Foo::blit pdq
|
} {
|
} {
|
$result == "Mongrel::blit Foo::blit"
|
$result == "Mongrel::blit Foo::blit"
|
}
|
}
|
|
|
test {Inherited "config" method works on derived classes} {
|
test {Inherited "config" method works on derived classes} {
|
m config -blit xyz -Foo::blit pdq
|
m config -blit xyz -Foo::blit pdq
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
} {
|
} {
|
$result == "xyz / pdq"
|
$result == "xyz / pdq"
|
}
|
}
|
|
|
test {Inherited "config" method works on derived classes} {
|
test {Inherited "config" method works on derived classes} {
|
m config -tag #0000
|
m config -tag #0000
|
} {
|
} {
|
$result == "Mongrel::tag"
|
$result == "Mongrel::tag"
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# INHERITANCE INFO
|
# INHERITANCE INFO
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {Info: class} {
|
test {Info: class} {
|
m info class
|
m info class
|
} {
|
} {
|
$result == "::Mongrel"
|
$result == "::Mongrel"
|
}
|
}
|
|
|
test {Info: inherit} {
|
test {Info: inherit} {
|
m info inherit
|
m info inherit
|
} {
|
} {
|
$result == "::FooBar ::Geek"
|
$result == "::FooBar ::Geek"
|
}
|
}
|
|
|
test {Info: heritage} {
|
test {Info: heritage} {
|
m info heritage
|
m info heritage
|
} {
|
} {
|
$result == "::Mongrel ::FooBar ::Foo ::Bar ::Geek"
|
$result == "::Mongrel ::FooBar ::Foo ::Bar ::Geek"
|
}
|
}
|
|
|
test {Built-in "isa" method} {
|
test {Built-in "isa" method} {
|
set status 1
|
set status 1
|
foreach c [m info heritage] {
|
foreach c [m info heritage] {
|
set status [expr {$status && [m isa $c]}]
|
set status [expr {$status && [m isa $c]}]
|
}
|
}
|
set status
|
set status
|
} {
|
} {
|
$result == 1
|
$result == 1
|
}
|
}
|
|
|
test {Built-in "isa" method} {
|
test {Built-in "isa" method} {
|
itcl_class Watermelon {}
|
itcl_class Watermelon {}
|
m isa Watermelon
|
m isa Watermelon
|
} {
|
} {
|
$result == 0
|
$result == 0
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# SCOPE MANIPULATION
|
# SCOPE MANIPULATION
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {commands normally execute in the scope of their class} {
|
test {commands normally execute in the scope of their class} {
|
m Foo::do {namespace current}
|
m Foo::do {namespace current}
|
} {
|
} {
|
$result == "Foo says '::Foo'"
|
$result == "Foo says '::Foo'"
|
}
|
}
|
|
|
test {"virtual" command moves scope to most specific class} {
|
test {"virtual" command moves scope to most specific class} {
|
m Foo::do {virtual namespace current}
|
m Foo::do {virtual namespace current}
|
} {
|
} {
|
$result == "Foo says '::Mongrel'"
|
$result == "Foo says '::Mongrel'"
|
}
|
}
|
|
|
test {"previous" command moves scope upward in hierarchy} {
|
test {"previous" command moves scope upward in hierarchy} {
|
m do {virtual previous namespace current}
|
m do {virtual previous namespace current}
|
} {
|
} {
|
$result == "Foo says '::FooBar'"
|
$result == "Foo says '::FooBar'"
|
}
|
}
|
|
|
test {"previous" command can be chained} {
|
test {"previous" command can be chained} {
|
m do {virtual previous previous namespace current}
|
m do {virtual previous previous namespace current}
|
} {
|
} {
|
$result == "Foo says '::Foo'"
|
$result == "Foo says '::Foo'"
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# METHOD INVOCATION
|
# METHOD INVOCATION
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {Simple method names are assigned based on heritage} {
|
test {Simple method names are assigned based on heritage} {
|
m do {concat "$this ([virtual info class]) at scope [namespace current]"}
|
m do {concat "$this ([virtual info class]) at scope [namespace current]"}
|
} {
|
} {
|
$result == "Foo says '::m (Mongrel) at scope ::Foo'"
|
$result == "Foo says '::m (Mongrel) at scope ::Foo'"
|
}
|
}
|
|
|
test {Explicit scoping can be used to reach shadowed members} {
|
test {Explicit scoping can be used to reach shadowed members} {
|
m Geek::do {concat "$this ([virtual info class]) at scope [namespace current]"}
|
m Geek::do {concat "$this ([virtual info class]) at scope [namespace current]"}
|
} {
|
} {
|
$result == "Geek says '::m (Mongrel) at scope ::Geek'"
|
$result == "Geek says '::m (Mongrel) at scope ::Geek'"
|
}
|
}
|
|
|
test {Methods execute in local scope of class, e.g., Foo::do} {
|
test {Methods execute in local scope of class, e.g., Foo::do} {
|
m config -blit abc -Foo::blit def
|
m config -blit abc -Foo::blit def
|
m Foo::do {set blit xyz}
|
m Foo::do {set blit xyz}
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
} {
|
} {
|
$result == "abc / xyz"
|
$result == "abc / xyz"
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# DESTRUCTION
|
# DESTRUCTION
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
test {Destructors should be invoked implicitly} {
|
test {Destructors should be invoked implicitly} {
|
set WATCH ""
|
set WATCH ""
|
concat [m delete] / $WATCH
|
concat [m delete] / $WATCH
|
} {
|
} {
|
$result == "/ ::Mongrel ::FooBar ::Foo ::Bar ::Geek"
|
$result == "/ ::Mongrel ::FooBar ::Foo ::Bar ::Geek"
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# OBJECT INFO
|
# OBJECT INFO
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
foreach obj [itcl_info objects] {
|
foreach obj [itcl_info objects] {
|
$obj delete
|
$obj delete
|
}
|
}
|
Mongrel m
|
Mongrel m
|
FooBar fb
|
FooBar fb
|
Foo f
|
Foo f
|
Geek g
|
Geek g
|
|
|
test {Object queries can be restricted by object name} {
|
test {Object queries can be restricted by object name} {
|
itcl_info objects f*
|
itcl_info objects f*
|
} {
|
} {
|
[test_cmp_lists $result {f fb}]
|
[test_cmp_lists $result {f fb}]
|
}
|
}
|
|
|
test {Object queries can be restricted to specific classes} {
|
test {Object queries can be restricted to specific classes} {
|
itcl_info objects -class Foo
|
itcl_info objects -class Foo
|
} {
|
} {
|
$result == "f"
|
$result == "f"
|
}
|
}
|
|
|
test {Object queries can be restricted by object heritage} {
|
test {Object queries can be restricted by object heritage} {
|
itcl_info objects -isa Foo
|
itcl_info objects -isa Foo
|
} {
|
} {
|
[test_cmp_lists $result {m f fb}]
|
[test_cmp_lists $result {m f fb}]
|
}
|
}
|
|
|
test {Object queries can be restricted by object name / specific classes} {
|
test {Object queries can be restricted by object name / specific classes} {
|
itcl_info objects f* -class Foo
|
itcl_info objects f* -class Foo
|
} {
|
} {
|
$result == "f"
|
$result == "f"
|
}
|
}
|
|
|
test {Object queries can be restricted by object name / object heritage} {
|
test {Object queries can be restricted by object name / object heritage} {
|
itcl_info objects f* -isa Foo
|
itcl_info objects f* -isa Foo
|
} {
|
} {
|
[test_cmp_lists $result {f fb}]
|
[test_cmp_lists $result {f fb}]
|
}
|
}
|
|
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
# ERROR HANDLING ACROSS CLASS BOUNDARIES
|
# ERROR HANDLING ACROSS CLASS BOUNDARIES
|
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
Mongrel m1
|
Mongrel m1
|
FooBar fb2
|
FooBar fb2
|
|
|
test {Errors and detected and reported across class boundaries} {
|
test {Errors and detected and reported across class boundaries} {
|
set status [catch {m1 do {fb2 do {error "test"}}} mesg]
|
set status [catch {m1 do {fb2 do {error "test"}}} mesg]
|
format "$mesg $status"
|
format "$mesg $status"
|
} {
|
} {
|
$result == "test 1"
|
$result == "test 1"
|
}
|
}
|
|
|
test {Stack trace unwinds properly across class boundaries} {
|
test {Stack trace unwinds properly across class boundaries} {
|
catch {m1 do {fb2 do {error "test"}}} mesg
|
catch {m1 do {fb2 do {error "test"}}} mesg
|
format "$errorInfo"
|
format "$errorInfo"
|
} {
|
} {
|
$result == {test
|
$result == {test
|
while executing
|
while executing
|
"error "test""
|
"error "test""
|
("eval" body line 1)
|
("eval" body line 1)
|
invoked from within
|
invoked from within
|
"eval $cmds"
|
"eval $cmds"
|
invoked from within
|
invoked from within
|
"return "Foo says '[eval $cmds]..."
|
"return "Foo says '[eval $cmds]..."
|
(object "::fb2" method "::Foo::do" body line 2)
|
(object "::fb2" method "::Foo::do" body line 2)
|
invoked from within
|
invoked from within
|
"fb2 do {error "test"}"
|
"fb2 do {error "test"}"
|
("eval" body line 1)
|
("eval" body line 1)
|
invoked from within
|
invoked from within
|
"eval $cmds"
|
"eval $cmds"
|
invoked from within
|
invoked from within
|
"return "Foo says '[eval $cmds]..."
|
"return "Foo says '[eval $cmds]..."
|
(object "::m1" method "::Foo::do" body line 2)
|
(object "::m1" method "::Foo::do" body line 2)
|
invoked from within
|
invoked from within
|
"m1 do {fb2 do {error "test"}}"}
|
"m1 do {fb2 do {error "test"}}"}
|
}
|
}
|
|
|
test {Stack trace unwinds properly across class boundaries} {
|
test {Stack trace unwinds properly across class boundaries} {
|
catch {m1 do {fb2 do {error "test" "some error"}}} mesg
|
catch {m1 do {fb2 do {error "test" "some error"}}} mesg
|
format "$errorInfo"
|
format "$errorInfo"
|
} {
|
} {
|
$result == {some error
|
$result == {some error
|
("eval" body line 1)
|
("eval" body line 1)
|
invoked from within
|
invoked from within
|
"eval $cmds"
|
"eval $cmds"
|
invoked from within
|
invoked from within
|
"return "Foo says '[eval $cmds]..."
|
"return "Foo says '[eval $cmds]..."
|
(object "::fb2" method "::Foo::do" body line 2)
|
(object "::fb2" method "::Foo::do" body line 2)
|
invoked from within
|
invoked from within
|
"fb2 do {error "test" "some error"}"
|
"fb2 do {error "test" "some error"}"
|
("eval" body line 1)
|
("eval" body line 1)
|
invoked from within
|
invoked from within
|
"eval $cmds"
|
"eval $cmds"
|
invoked from within
|
invoked from within
|
"return "Foo says '[eval $cmds]..."
|
"return "Foo says '[eval $cmds]..."
|
(object "::m1" method "::Foo::do" body line 2)
|
(object "::m1" method "::Foo::do" body line 2)
|
invoked from within
|
invoked from within
|
"m1 do {fb2 do {error "test" "some error"}}"}
|
"m1 do {fb2 do {error "test" "some error"}}"}
|
}
|
}
|
|
|
test {Error codes are preserved across class boundaries} {
|
test {Error codes are preserved across class boundaries} {
|
catch {m1 do {fb2 do {error "test" "some error" CODE-BLUE}}} mesg
|
catch {m1 do {fb2 do {error "test" "some error" CODE-BLUE}}} mesg
|
format "$errorCode"
|
format "$errorCode"
|
} {
|
} {
|
$result == "CODE-BLUE"
|
$result == "CODE-BLUE"
|
}
|
}
|
|
|