1 |
578 |
markom |
#
|
2 |
|
|
# Tests for inheritance and scope handling
|
3 |
|
|
# ----------------------------------------------------------------------
|
4 |
|
|
# AUTHOR: Michael J. McLennan
|
5 |
|
|
# Bell Labs Innovations for Lucent Technologies
|
6 |
|
|
# mmclennan@lucent.com
|
7 |
|
|
# http://www.tcltk.com/itcl
|
8 |
|
|
#
|
9 |
|
|
# RCS: $Id: inherit.test,v 1.1.1.1 2002-01-16 10:24:47 markom Exp $
|
10 |
|
|
# ----------------------------------------------------------------------
|
11 |
|
|
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
|
12 |
|
|
# ======================================================================
|
13 |
|
|
# See the file "license.terms" for information on usage and
|
14 |
|
|
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
15 |
|
|
|
16 |
|
|
# ----------------------------------------------------------------------
|
17 |
|
|
# MULTIPLE BASE-CLASS ERROR DETECTION
|
18 |
|
|
# ----------------------------------------------------------------------
|
19 |
|
|
test {Cannot inherit from the same base class more than once} {
|
20 |
|
|
catch "VirtualErr" errmsg
|
21 |
|
|
set errmsg
|
22 |
|
|
} {
|
23 |
|
|
[string match {*class "::VirtualErr" inherits base class "::Foo" more than once:
|
24 |
|
|
VirtualErr->Mongrel->FooBar->Foo
|
25 |
|
|
VirtualErr->Foo
|
26 |
|
|
VirtualErr->BarFoo->Foo} $result]
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
# ----------------------------------------------------------------------
|
30 |
|
|
# CONSTRUCTION
|
31 |
|
|
# ----------------------------------------------------------------------
|
32 |
|
|
test {Constructors should be invoked implicitly} {
|
33 |
|
|
set WATCH ""
|
34 |
|
|
concat [Mongrel m] / $WATCH
|
35 |
|
|
} {
|
36 |
|
|
$result == "m / ::Geek ::Bar ::Foo ::FooBar ::Mongrel"
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
test {Initialization of shadowed variables works properly} {
|
40 |
|
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
41 |
|
|
} {
|
42 |
|
|
$result == "nonnull / "
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
# ----------------------------------------------------------------------
|
46 |
|
|
# PUBLIC VARIABLES
|
47 |
|
|
# ----------------------------------------------------------------------
|
48 |
|
|
test {Inherited "config" method works on derived classes} {
|
49 |
|
|
m config -blit xyz -Foo::blit pdq
|
50 |
|
|
} {
|
51 |
|
|
$result == "Mongrel::blit Foo::blit"
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
test {Inherited "config" method works on derived classes} {
|
55 |
|
|
m config -blit xyz -Foo::blit pdq
|
56 |
|
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
57 |
|
|
} {
|
58 |
|
|
$result == "xyz / pdq"
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
test {Inherited "config" method works on derived classes} {
|
62 |
|
|
m config -tag #0000
|
63 |
|
|
} {
|
64 |
|
|
$result == "Mongrel::tag"
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
# ----------------------------------------------------------------------
|
68 |
|
|
# INHERITANCE INFO
|
69 |
|
|
# ----------------------------------------------------------------------
|
70 |
|
|
test {Info: class} {
|
71 |
|
|
m info class
|
72 |
|
|
} {
|
73 |
|
|
$result == "::Mongrel"
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
test {Info: inherit} {
|
77 |
|
|
m info inherit
|
78 |
|
|
} {
|
79 |
|
|
$result == "::FooBar ::Geek"
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
test {Info: heritage} {
|
83 |
|
|
m info heritage
|
84 |
|
|
} {
|
85 |
|
|
$result == "::Mongrel ::FooBar ::Foo ::Bar ::Geek"
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
test {Built-in "isa" method} {
|
89 |
|
|
set status 1
|
90 |
|
|
foreach c [m info heritage] {
|
91 |
|
|
set status [expr {$status && [m isa $c]}]
|
92 |
|
|
}
|
93 |
|
|
set status
|
94 |
|
|
} {
|
95 |
|
|
$result == 1
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
test {Built-in "isa" method} {
|
99 |
|
|
itcl_class Watermelon {}
|
100 |
|
|
m isa Watermelon
|
101 |
|
|
} {
|
102 |
|
|
$result == 0
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
# ----------------------------------------------------------------------
|
106 |
|
|
# SCOPE MANIPULATION
|
107 |
|
|
# ----------------------------------------------------------------------
|
108 |
|
|
test {commands normally execute in the scope of their class} {
|
109 |
|
|
m Foo::do {namespace current}
|
110 |
|
|
} {
|
111 |
|
|
$result == "Foo says '::Foo'"
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
test {"virtual" command moves scope to most specific class} {
|
115 |
|
|
m Foo::do {virtual namespace current}
|
116 |
|
|
} {
|
117 |
|
|
$result == "Foo says '::Mongrel'"
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
test {"previous" command moves scope upward in hierarchy} {
|
121 |
|
|
m do {virtual previous namespace current}
|
122 |
|
|
} {
|
123 |
|
|
$result == "Foo says '::FooBar'"
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
test {"previous" command can be chained} {
|
127 |
|
|
m do {virtual previous previous namespace current}
|
128 |
|
|
} {
|
129 |
|
|
$result == "Foo says '::Foo'"
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
# ----------------------------------------------------------------------
|
133 |
|
|
# METHOD INVOCATION
|
134 |
|
|
# ----------------------------------------------------------------------
|
135 |
|
|
test {Simple method names are assigned based on heritage} {
|
136 |
|
|
m do {concat "$this ([virtual info class]) at scope [namespace current]"}
|
137 |
|
|
} {
|
138 |
|
|
$result == "Foo says '::m (Mongrel) at scope ::Foo'"
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
test {Explicit scoping can be used to reach shadowed members} {
|
142 |
|
|
m Geek::do {concat "$this ([virtual info class]) at scope [namespace current]"}
|
143 |
|
|
} {
|
144 |
|
|
$result == "Geek says '::m (Mongrel) at scope ::Geek'"
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
test {Methods execute in local scope of class, e.g., Foo::do} {
|
148 |
|
|
m config -blit abc -Foo::blit def
|
149 |
|
|
m Foo::do {set blit xyz}
|
150 |
|
|
concat [m info public blit -value] / [m info public Foo::blit -value]
|
151 |
|
|
} {
|
152 |
|
|
$result == "abc / xyz"
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
# ----------------------------------------------------------------------
|
156 |
|
|
# DESTRUCTION
|
157 |
|
|
# ----------------------------------------------------------------------
|
158 |
|
|
test {Destructors should be invoked implicitly} {
|
159 |
|
|
set WATCH ""
|
160 |
|
|
concat [m delete] / $WATCH
|
161 |
|
|
} {
|
162 |
|
|
$result == "/ ::Mongrel ::FooBar ::Foo ::Bar ::Geek"
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
# ----------------------------------------------------------------------
|
166 |
|
|
# OBJECT INFO
|
167 |
|
|
# ----------------------------------------------------------------------
|
168 |
|
|
foreach obj [itcl_info objects] {
|
169 |
|
|
$obj delete
|
170 |
|
|
}
|
171 |
|
|
Mongrel m
|
172 |
|
|
FooBar fb
|
173 |
|
|
Foo f
|
174 |
|
|
Geek g
|
175 |
|
|
|
176 |
|
|
test {Object queries can be restricted by object name} {
|
177 |
|
|
itcl_info objects f*
|
178 |
|
|
} {
|
179 |
|
|
[test_cmp_lists $result {f fb}]
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
test {Object queries can be restricted to specific classes} {
|
183 |
|
|
itcl_info objects -class Foo
|
184 |
|
|
} {
|
185 |
|
|
$result == "f"
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
test {Object queries can be restricted by object heritage} {
|
189 |
|
|
itcl_info objects -isa Foo
|
190 |
|
|
} {
|
191 |
|
|
[test_cmp_lists $result {m f fb}]
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
test {Object queries can be restricted by object name / specific classes} {
|
195 |
|
|
itcl_info objects f* -class Foo
|
196 |
|
|
} {
|
197 |
|
|
$result == "f"
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
test {Object queries can be restricted by object name / object heritage} {
|
201 |
|
|
itcl_info objects f* -isa Foo
|
202 |
|
|
} {
|
203 |
|
|
[test_cmp_lists $result {f fb}]
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
# ----------------------------------------------------------------------
|
207 |
|
|
# ERROR HANDLING ACROSS CLASS BOUNDARIES
|
208 |
|
|
# ----------------------------------------------------------------------
|
209 |
|
|
Mongrel m1
|
210 |
|
|
FooBar fb2
|
211 |
|
|
|
212 |
|
|
test {Errors and detected and reported across class boundaries} {
|
213 |
|
|
set status [catch {m1 do {fb2 do {error "test"}}} mesg]
|
214 |
|
|
format "$mesg $status"
|
215 |
|
|
} {
|
216 |
|
|
$result == "test 1"
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
test {Stack trace unwinds properly across class boundaries} {
|
220 |
|
|
catch {m1 do {fb2 do {error "test"}}} mesg
|
221 |
|
|
format "$errorInfo"
|
222 |
|
|
} {
|
223 |
|
|
$result == {test
|
224 |
|
|
while executing
|
225 |
|
|
"error "test""
|
226 |
|
|
("eval" body line 1)
|
227 |
|
|
invoked from within
|
228 |
|
|
"eval $cmds"
|
229 |
|
|
invoked from within
|
230 |
|
|
"return "Foo says '[eval $cmds]..."
|
231 |
|
|
(object "::fb2" method "::Foo::do" body line 2)
|
232 |
|
|
invoked from within
|
233 |
|
|
"fb2 do {error "test"}"
|
234 |
|
|
("eval" body line 1)
|
235 |
|
|
invoked from within
|
236 |
|
|
"eval $cmds"
|
237 |
|
|
invoked from within
|
238 |
|
|
"return "Foo says '[eval $cmds]..."
|
239 |
|
|
(object "::m1" method "::Foo::do" body line 2)
|
240 |
|
|
invoked from within
|
241 |
|
|
"m1 do {fb2 do {error "test"}}"}
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
test {Stack trace unwinds properly across class boundaries} {
|
245 |
|
|
catch {m1 do {fb2 do {error "test" "some error"}}} mesg
|
246 |
|
|
format "$errorInfo"
|
247 |
|
|
} {
|
248 |
|
|
$result == {some error
|
249 |
|
|
("eval" body line 1)
|
250 |
|
|
invoked from within
|
251 |
|
|
"eval $cmds"
|
252 |
|
|
invoked from within
|
253 |
|
|
"return "Foo says '[eval $cmds]..."
|
254 |
|
|
(object "::fb2" method "::Foo::do" body line 2)
|
255 |
|
|
invoked from within
|
256 |
|
|
"fb2 do {error "test" "some error"}"
|
257 |
|
|
("eval" body line 1)
|
258 |
|
|
invoked from within
|
259 |
|
|
"eval $cmds"
|
260 |
|
|
invoked from within
|
261 |
|
|
"return "Foo says '[eval $cmds]..."
|
262 |
|
|
(object "::m1" method "::Foo::do" body line 2)
|
263 |
|
|
invoked from within
|
264 |
|
|
"m1 do {fb2 do {error "test" "some error"}}"}
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
test {Error codes are preserved across class boundaries} {
|
268 |
|
|
catch {m1 do {fb2 do {error "test" "some error" CODE-BLUE}}} mesg
|
269 |
|
|
format "$errorCode"
|
270 |
|
|
} {
|
271 |
|
|
$result == "CODE-BLUE"
|
272 |
|
|
}
|