2
# Tests for inheritance and scope handling
3
# ----------------------------------------------------------------------
4
# AUTHOR: Michael J. McLennan
5
# Bell Labs Innovations for Lucent Technologies
7
# http://www.tcltk.com/itcl
8
# ----------------------------------------------------------------------
9
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
10
# ======================================================================
11
# See the file "license.terms" for information on usage and
12
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14
package require tcltest 2.1
15
namespace import ::tcltest::test
16
::tcltest::loadTestedCommands
19
# ----------------------------------------------------------------------
20
# Test construction/destruction with inheritance
21
# ----------------------------------------------------------------------
22
test inherit-1.1 {define classes with constructors/destructors} {
23
variable ::test_cd_watch ""
24
itcl::class test_cd_foo {
26
global ::test_cd_watch
27
lappend test_cd_watch "foo: $x $y"
30
global ::test_cd_watch
31
lappend test_cd_watch "foo destruct"
34
itcl::class test_cd_bar {
36
global ::test_cd_watch
37
lappend test_cd_watch "bar: $args"
40
global ::test_cd_watch
41
lappend test_cd_watch "bar destruct"
44
itcl::class test_cd_foobar {
45
inherit test_cd_foo test_cd_bar
46
constructor {x y args} {
47
test_cd_foo::constructor $x $y
49
global ::test_cd_watch
50
lappend test_cd_watch "foobar: $x $y ($args)"
53
global ::test_cd_watch
54
lappend test_cd_watch "foobar destruct"
57
itcl::class test_cd_geek {
59
global ::test_cd_watch
60
lappend test_cd_watch "geek"
63
global ::test_cd_watch
64
lappend test_cd_watch "geek destruct"
67
itcl::class test_cd_mongrel {
68
inherit test_cd_foobar test_cd_geek
70
eval test_cd_foobar::constructor 1 2 fred $x
72
global ::test_cd_watch
73
lappend test_cd_watch "mongrel: $x"
76
global ::test_cd_watch
77
lappend test_cd_watch "mongrel destruct"
80
itcl::class test_cd_none {
81
inherit test_cd_bar test_cd_geek
83
itcl::class test_cd_skip {
86
global ::test_cd_watch
87
lappend test_cd_watch "skip"
90
global ::test_cd_watch
91
lappend test_cd_watch "skip destruct"
96
test inherit-1.2 {constructors should be invoked in the proper order} {
97
set ::test_cd_watch ""
98
list [test_cd_mongrel #auto bob] [set ::test_cd_watch]
99
} {test_cd_mongrel0 {{foo: 1 2} {bar: } {foobar: 1 2 (fred bob)} geek {mongrel: bob}}}
101
test inherit-1.3 {destructors should be invoked in the proper order} {
102
set ::test_cd_watch ""
103
list [itcl::delete object test_cd_mongrel0] [set ::test_cd_watch]
104
} {{} {{mongrel destruct} {foobar destruct} {foo destruct} {bar destruct} {geek destruct}}}
106
test inherit-1.4 {constructors are optional} {
107
set ::test_cd_watch ""
108
list [test_cd_none #auto] [set ::test_cd_watch]
109
} {test_cd_none0 {geek {bar: }}}
111
test inherit-1.5 {destructors are optional} {
112
set ::test_cd_watch ""
113
list [itcl::delete object test_cd_none0] [set ::test_cd_watch]
114
} {{} {{bar destruct} {geek destruct}}}
116
test inherit-1.6 {construction ok if constructors are missing} {
117
set ::test_cd_watch ""
118
list [test_cd_skip #auto] [set ::test_cd_watch]
119
} {test_cd_skip0 {geek {bar: } skip}}
121
test inherit-1.7 {destruction ok if destructors are missing} {
122
set ::test_cd_watch ""
123
list [itcl::delete object test_cd_skip0] [set ::test_cd_watch]
124
} {{} {{skip destruct} {bar destruct} {geek destruct}}}
127
test inherit-1.8 {errors during construction are cleaned up and reported} knownBug {
128
global errorInfo test_cd_watch
130
itcl::body test_cd_bar::constructor {args} {error "bar: failed"}
131
list [catch {test_cd_mongrel #auto bob} msg] $msg \
132
$errorInfo $test_cd_watch
133
} {1 {bar: failed} {bar: failed
135
"error "bar: failed""
136
while constructing object "::test_cd_mongrel1" in ::test_cd_bar::constructor (body line 1)
137
while constructing object "::test_cd_mongrel1" in ::test_cd_foobar::constructor (body line 1)
139
"test_cd_foobar::constructor 1 2 fred bob"
142
"eval test_cd_foobar::constructor 1 2 fred $x"
143
while constructing object "::test_cd_mongrel1" in ::test_cd_mongrel::constructor (body line 2)
145
"::itcl::parser::handleClass test_cd_mongrel ::test_cd_mongrel #auto bob"
147
"test_cd_mongrel #auto bob"} {{foo: 1 2} {mongrel destruct} {foobar destruct} {foo destruct} {bar destruct} {geek destruct}}}
149
test inherit-1.9 {errors during destruction prevent object delete} {
150
global errorInfo test_cd_watch
151
itcl::body test_cd_bar::constructor {args} {return "bar: $args"}
152
itcl::body test_cd_bar::destructor {} {error "bar: failed"}
153
test_cd_mongrel mongrel1 ted
155
list [catch {itcl::delete object mongrel1} msg] $msg \
156
$errorInfo $test_cd_watch [itcl::find objects mongrel*]
157
} {1 {bar: failed} {bar: failed
159
"error "bar: failed""
160
while deleting object "::mongrel1" in ::test_cd_bar::destructor (body line 1)
162
"itcl::delete object mongrel1"} {{mongrel destruct} {foobar destruct} {foo destruct}} mongrel1}
164
test inherit-1.10 {errors during destruction prevent class delete} {
165
list [catch {itcl::delete class test_cd_foo} msg] $msg
168
eval namespace delete [itcl::find classes test_cd_*]
170
# ----------------------------------------------------------------------
171
# Test data member access and scoping
172
# ----------------------------------------------------------------------
173
test inherit-2.1 {define classes with data members} {
174
itcl::class test_cd_foo {
175
protected variable x "foo-x"
176
method do {args} {eval $args}
178
itcl::class test_cd_bar {
179
protected variable x "bar-x"
180
method do {args} {eval $args}
182
itcl::class test_cd_foobar {
183
inherit test_cd_foo test_cd_bar
184
method do {args} {eval $args}
186
itcl::class test_cd_geek {
187
method do {args} {eval $args}
189
itcl::class test_cd_mongrel {
190
inherit test_cd_foobar test_cd_geek
191
protected variable x "mongrel-x"
192
method do {args} {eval $args}
196
test inherit-2.2 {"info" provides access to shadowed data members} {
197
test_cd_mongrel #auto
198
list [lsort [test_cd_mongrel0 info variable]] \
199
[test_cd_mongrel0 info variable test_cd_foo::x] \
200
[test_cd_mongrel0 info variable test_cd_bar::x] \
201
[test_cd_mongrel0 info variable test_cd_mongrel::x] \
202
[test_cd_mongrel0 info variable x]
203
} {{::test_cd_bar::x ::test_cd_foo::x ::test_cd_mongrel::this ::test_cd_mongrel::x} {protected variable ::test_cd_foo::x foo-x foo-x} {protected variable ::test_cd_bar::x bar-x bar-x} {protected variable ::test_cd_mongrel::x mongrel-x mongrel-x} {protected variable ::test_cd_mongrel::x mongrel-x mongrel-x}}
205
test inherit-2.3 {variable resolution works properly in methods} {
206
list [test_cd_mongrel0 test_cd_foo::do set x] \
207
[test_cd_mongrel0 test_cd_bar::do set x] \
208
[test_cd_mongrel0 test_cd_foobar::do set x] \
209
[test_cd_mongrel0 test_cd_mongrel::do set x]
210
} {foo-x bar-x foo-x mongrel-x}
212
test inherit-2.4 {methods have access to shadowed data members} {
213
list [test_cd_mongrel0 test_cd_foobar::do set x] \
214
[test_cd_mongrel0 test_cd_foobar::do set test_cd_foo::x] \
215
[test_cd_mongrel0 test_cd_foobar::do set test_cd_bar::x] \
216
[test_cd_mongrel0 test_cd_mongrel::do set test_cd_foo::x] \
217
[test_cd_mongrel0 test_cd_mongrel::do set test_cd_bar::x]
218
} {foo-x foo-x bar-x foo-x bar-x}
220
eval namespace delete [itcl::find classes test_cd_*]
222
# ----------------------------------------------------------------------
223
# Test public variables and "configure" method
224
# ----------------------------------------------------------------------
225
test inherit-3.1 {define classes with public variables} {
226
variable ::test_cd_watch ""
227
itcl::class test_cd_foo {
228
public variable x "foo-x" {
230
lappend test_cd_watch "foo: $x in scope [namespace current]"
232
method do {args} {eval $args}
234
itcl::class test_cd_bar {
235
public variable x "bar-x" {
237
lappend test_cd_watch "bar: $x in scope [namespace current]"
239
method do {args} {eval $args}
241
itcl::class test_cd_foobar {
242
inherit test_cd_foo test_cd_bar
243
method do {args} {eval $args}
245
itcl::class test_cd_geek {
246
method do {args} {eval $args}
248
itcl::class test_cd_mongrel {
249
inherit test_cd_foobar test_cd_geek
250
public variable x "mongrel-x" {
252
lappend test_cd_watch "mongrel: $x in scope [namespace current]"
254
method do {args} {eval $args}
258
test inherit-3.2 {create an object with public variables} {
259
test_cd_mongrel #auto
262
test inherit-3.3 {"configure" lists all public variables} {
263
lsort [test_cd_mongrel0 configure]
264
} {{-test_cd_bar::x bar-x bar-x} {-test_cd_foo::x foo-x foo-x} {-x mongrel-x mongrel-x}}
266
test inherit-3.4 {"configure" treats simple names as "most specific"} {
267
lsort [test_cd_mongrel0 configure -x]
268
} {-x mongrel-x mongrel-x}
270
test inherit-3.5 {"configure" treats simple names as "most specific"} {
271
set ::test_cd_watch ""
272
list [test_cd_mongrel0 configure -x hello] \
273
[set ::test_cd_watch]
274
} {{} {{mongrel: hello in scope ::test_cd_mongrel}}}
276
test inherit-3.6 {"configure" allows access to shadowed options} {
277
set ::test_cd_watch ""
278
list [test_cd_mongrel0 configure -test_cd_foo::x hello] \
279
[test_cd_mongrel0 configure -test_cd_bar::x there] \
280
[set ::test_cd_watch]
281
} {{} {} {{foo: hello in scope ::test_cd_foo} {bar: there in scope ::test_cd_bar}}}
283
test inherit-3.7 {"configure" will change several variables at once} {
284
set ::test_cd_watch ""
285
list [test_cd_mongrel0 configure -x one \
286
-test_cd_foo::x two \
287
-test_cd_bar::x three] \
288
[set ::test_cd_watch]
289
} {{} {{mongrel: one in scope ::test_cd_mongrel} {foo: two in scope ::test_cd_foo} {bar: three in scope ::test_cd_bar}}}
291
test inherit-3.8 {"cget" does proper name resolution} {
292
list [test_cd_mongrel0 cget -x] \
293
[test_cd_mongrel0 cget -test_cd_foo::x] \
294
[test_cd_mongrel0 cget -test_cd_bar::x] \
295
[test_cd_mongrel0 cget -test_cd_mongrel::x]
296
} {one two three one}
298
eval namespace delete [itcl::find classes test_cd_*]
300
# ----------------------------------------------------------------------
301
# Test inheritance info
302
# ----------------------------------------------------------------------
303
test inherit-4.1 {define classes for inheritance info} {
304
itcl::class test_cd_foo {
305
method do {args} {eval $args}
307
itcl::class test_cd_bar {
308
method do {args} {eval $args}
310
itcl::class test_cd_foobar {
311
inherit test_cd_foo test_cd_bar
312
method do {args} {eval $args}
314
itcl::class test_cd_geek {
315
method do {args} {eval $args}
317
itcl::class test_cd_mongrel {
318
inherit test_cd_foobar test_cd_geek
319
method do {args} {eval $args}
323
test inherit-4.2 {create an object for inheritance tests} {
324
test_cd_mongrel #auto
327
test inherit-4.3 {"info class" should be virtual} {
328
list [test_cd_mongrel0 info class] \
329
[test_cd_mongrel0 test_cd_foo::do info class] \
330
[test_cd_mongrel0 test_cd_geek::do info class]
331
} {::test_cd_mongrel ::test_cd_mongrel ::test_cd_mongrel}
333
test inherit-4.4 {"info inherit" depends on class scope} {
334
list [test_cd_mongrel0 info inherit] \
335
[test_cd_mongrel0 test_cd_foo::do info inherit] \
336
[test_cd_mongrel0 test_cd_foobar::do info inherit]
337
} {{::test_cd_foobar ::test_cd_geek} {} {::test_cd_foo ::test_cd_bar}}
339
test inherit-4.5 {"info heritage" depends on class scope} {
340
list [test_cd_mongrel0 info heritage] \
341
[test_cd_mongrel0 test_cd_foo::do info heritage] \
342
[test_cd_mongrel0 test_cd_foobar::do info heritage]
343
} {{::test_cd_mongrel ::test_cd_foobar ::test_cd_foo ::test_cd_bar ::test_cd_geek} ::test_cd_foo {::test_cd_foobar ::test_cd_foo ::test_cd_bar}}
345
test inherit-4.6 {built-in "isa" method works} {
347
foreach c [test_cd_mongrel0 info heritage] {
348
lappend status [test_cd_mongrel0 isa $c]
353
test inherit-4.7 {built-in "isa" method works within methods} {
355
foreach c [test_cd_mongrel0 info heritage] {
356
lappend status [test_cd_mongrel0 test_cd_foo::do isa $c]
361
test inherit-4.8 {built-in "isa" method recognizes bad classes} {
362
itcl::class test_cd_other {}
363
test_cd_mongrel0 isa test_cd_other
366
test inherit-4.9 {built-in "isa" method recognizes bad classes} {
367
list [catch {test_cd_mongrel0 isa test_cd_bogus} msg] $msg
368
} {1 {class "test_cd_bogus" not found in context "::test_cd_foo"}}
370
eval namespace delete [itcl::find classes test_cd_*]
372
# ----------------------------------------------------------------------
373
# Test "find objects"
374
# ----------------------------------------------------------------------
375
test inherit-5.1 {define classes for inheritance info} {
376
itcl::class test_cd_foo {
378
itcl::class test_cd_bar {
380
itcl::class test_cd_foobar {
381
inherit test_cd_foo test_cd_bar
383
itcl::class test_cd_geek {
385
itcl::class test_cd_mongrel {
386
inherit test_cd_foobar test_cd_geek
390
test inherit-5.2 {create objects for info tests} {
391
list [test_cd_foo #auto] [test_cd_foo #auto] \
392
[test_cd_foobar #auto] \
393
[test_cd_geek #auto] \
394
[test_cd_mongrel #auto]
395
} {test_cd_foo0 test_cd_foo1 test_cd_foobar0 test_cd_geek0 test_cd_mongrel0}
397
test inherit-5.3 {find objects: -class qualifier} {
398
lsort [itcl::find objects -class test_cd_foo]
399
} {test_cd_foo0 test_cd_foo1}
401
test inherit-5.4 {find objects: -class qualifier} {
402
lsort [itcl::find objects -class test_cd_mongrel]
405
test inherit-5.5 {find objects: -isa qualifier} {
406
lsort [itcl::find objects -isa test_cd_foo]
407
} {test_cd_foo0 test_cd_foo1 test_cd_foobar0 test_cd_mongrel0}
409
test inherit-5.6 {find objects: -isa qualifier} {
410
lsort [itcl::find objects -isa test_cd_mongrel]
413
test inherit-5.7 {find objects: name qualifier} {
414
lsort [itcl::find objects test_cd_foo*]
415
} {test_cd_foo0 test_cd_foo1 test_cd_foobar0}
417
test inherit-5.8 {find objects: -class and -isa qualifiers} {
418
lsort [itcl::find objects -isa test_cd_foo -class test_cd_foobar]
421
test inherit-5.9 {find objects: -isa and name qualifiers} {
422
lsort [itcl::find objects -isa test_cd_foo *0]
423
} {test_cd_foo0 test_cd_foobar0 test_cd_mongrel0}
425
test inherit-5.10 {find objects: usage errors} {
426
list [catch {itcl::find objects -xyzzy value} msg] $msg
427
} {1 {wrong # args: should be "itcl::find objects ?-class className? ?-isa className? ?pattern?"}}
429
eval namespace delete [itcl::find classes test_cd_*]
431
# ----------------------------------------------------------------------
432
# Test method scoping and execution
433
# ----------------------------------------------------------------------
434
test inherit-6.1 {define classes for scope tests} {
435
itcl::class test_cd_foo {
436
method check {} {return "foo"}
437
method do {args} {return "foo says: [eval $args]"}
439
itcl::class test_cd_bar {
440
method check {} {return "bar"}
441
method do {args} {return "bar says: [eval $args]"}
443
itcl::class test_cd_foobar {
444
inherit test_cd_foo test_cd_bar
445
method check {} {return "foobar"}
446
method do {args} {return "foobar says: [eval $args]"}
448
itcl::class test_cd_geek {
449
method check {} {return "geek"}
450
method do {args} {return "geek says: [eval $args]"}
452
itcl::class test_cd_mongrel {
453
inherit test_cd_foobar test_cd_geek
454
method check {} {return "mongrel"}
455
method do {args} {return "mongrel says: [eval $args]"}
459
test inherit-6.2 {create objects for scoping tests} {
460
list [test_cd_mongrel #auto] [test_cd_foobar #auto]
461
} {test_cd_mongrel0 test_cd_foobar0}
463
test inherit-6.3 {methods are "virtual" outside of the class} {
464
test_cd_mongrel0 check
467
test inherit-6.4 {specific methods can be accessed by name} {
468
test_cd_mongrel0 test_cd_foo::check
471
test inherit-6.5 {methods are "virtual" within a class too} {
472
test_cd_mongrel0 test_cd_foobar::do check
473
} {foobar says: mongrel}
475
test inherit-6.6 {methods are executed where they were defined} {
476
list [test_cd_mongrel0 test_cd_foo::do namespace current] \
477
[test_cd_mongrel0 test_cd_foobar::do namespace current] \
478
[test_cd_mongrel0 do namespace current] \
479
} {{foo says: ::test_cd_foo} {foobar says: ::test_cd_foobar} {mongrel says: ::test_cd_mongrel}}
481
test inherit-6.7 {"virtual" command no longer exists} {
483
test_cd_mongrel0 test_cd_foobar::do virtual namespace current
485
} {1 {invalid command name "virtual"}}
487
test inherit-6.8 {"previous" command no longer exists} {
489
test_cd_mongrel0 test_cd_foobar::do previous check
491
} {1 {invalid command name "previous"}}
493
test inherit-6.9 {errors are detected and reported across class boundaries} {
495
# NOTE: For tcl8.2.3 and earlier the stack trace will have
496
# 'invoked from within "eval $args"' for the first eval
497
# statement. For later versions, it does not. Use
498
# string match to reduce the sensitivity to that.
501
test_cd_mongrel0 do test_cd_foobar0 do error "test" "some error"
502
} msg] $msg [string match {some error
503
("eval" body line 1)*
504
(object "::test_cd_foobar0" method "::test_cd_foobar::do" body line 1)
506
"test_cd_foobar0 do error test {some error}"
510
(object "::test_cd_mongrel0" method "::test_cd_mongrel::do" body line 1)
512
"test_cd_mongrel0 do test_cd_foobar0 do error "test" "some error""} [set ::errorInfo]]
515
test inherit-6.10 {errors codes are preserved across class boundaries} {
517
test_cd_mongrel0 do test_cd_foobar0 do error "test" "problem" CODE-BLUE
518
} msg] $msg [set ::errorCode]
521
test inherit-6.11 {multi-value error codes are preserved across class boundaries} {
523
test_cd_mongrel0 do test_cd_foobar0 do error "test" "problem" "CODE BLUE 123"
524
} msg] $msg [set ::errorCode]
525
} {1 test {CODE BLUE 123}}
527
eval namespace delete [itcl::find classes test_cd_*]
529
# ----------------------------------------------------------------------
530
# Test inheritance errors
531
# ----------------------------------------------------------------------
532
test inherit-7.1 {cannot inherit from non-existant class} {
535
inherit non_existant_class_xyzzy
538
} {1 {cannot inherit from "non_existant_class_xyzzy" (class "non_existant_class_xyzzy" not found in context "::")}}
540
test inherit-7.2 {cannot inherit from procs} {
541
proc inherit_test_proc {x y} {
542
error "never call this"
546
inherit inherit_test_proc
549
} {1 {cannot inherit from "inherit_test_proc" (class "inherit_test_proc" not found in context "::")}}
551
test inherit-7.3 {cannot inherit from yourself} {
557
} {1 {class "bogus" cannot inherit from itself}}
559
test inherit-7.4 {cannot have more than one inherit statement} {
561
itcl::class test_inherit_base1 { }
562
itcl::class test_inherit_base2 { }
564
inherit test_inherit_base1
565
inherit test_inherit_base2
568
} {1 {inheritance "test_inherit_base1 " already defined for class "::bogus"}}
570
::itcl::delete class test_inherit_base1 test_inherit_base2
572
# ----------------------------------------------------------------------
573
# Multiple base class error detection
574
# ----------------------------------------------------------------------
575
test inherit-8.1 {cannot inherit from the same base class more than once} {
576
itcl::class test_mi_base {}
577
itcl::class test_mi_foo {inherit test_mi_base}
578
itcl::class test_mi_bar {inherit test_mi_base}
580
itcl::class test_mi_foobar {inherit test_mi_foo test_mi_bar}
582
} {1 {class "::test_mi_foobar" inherits base class "::test_mi_base" more than once:
583
test_mi_foobar->test_mi_foo->test_mi_base
584
test_mi_foobar->test_mi_bar->test_mi_base}}
586
itcl::delete class test_mi_base
588
::tcltest::cleanupTests