Debian Codegraph @

tests/oo.test

raw
1 # This file contains a collection of tests for Tcl's built-in object system.
2 # Sourcing this file into Tcl runs the tests and generates output for errors.
3 # No output means no errors were found.
4 #
5 # Copyright (c) 2006-2013 Donal K. Fellows
6 #
7 # See the file "license.terms" for information on usage and redistribution of
8 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
9
10 package require TclOO 1.1.0
11 if {"::tcltest" ni [namespace children]} {
12 package require tcltest 2.5
13 namespace import -force ::tcltest::*
14 }
15
16 # A helper for intercepting background errors
17 proc ::bgerrorIntercept {varName body} {
18 set old [interp bgerror {}]
19 interp bgerror {} [list apply {{var msg args} {
20 upvar #0 $var v
21 lappend v $msg
22 }} $varName]
23 try {
24 uplevel 1 $body
25 } finally {
26 interp bgerror {} $old
27 }
28 }
29
30 # The foundational objects oo::object and oo::class are sensitive to reference
31 # counting errors and are deallocated only when an interp is deleted, so in
32 # this test suite, interp creation and interp deletion are often used in
33 # leaktests in order to leverage this sensitivity.
34
35 testConstraint memory [llength [info commands memory]]
36 if {[testConstraint memory]} {
37 proc getbytes {} {
38 set lines [split [memory info] \n]
39 return [lindex $lines 3 3]
40 }
41 proc leaktest {script {iterations 3}} {
42 set end [getbytes]
43 for {set i 0} {$i < $iterations} {incr i} {
44 uplevel 1 $script
45 set tmp $end
46 set end [getbytes]
47 }
48 return [expr {$end - $tmp}]
49 }
50 }
51
52 test oo-0.1 {basic test of OO's ability to clean up its initial state} {
53 interp create t
54 t eval {
55 package require TclOO
56 }
57 interp delete t
58 } {}
59 test oo-0.2 {basic test of OO's ability to clean up its initial state} {
60 set i [interp create]
61 interp eval $i {
62 package require TclOO
63 namespace delete ::
64 }
65 interp delete $i
66 } {}
67 test oo-0.3 {basic test of OO's ability to clean up its initial state} -body {
68 leaktest {
69 [oo::object new] destroy
70 }
71 } -constraints memory -result 0
72 test oo-0.4 {basic test of OO's ability to clean up its initial state} -body {
73 leaktest {
74 oo::class create foo
75 foo new
76 foo destroy
77 }
78 } -constraints memory -result 0
79 test oo-0.5.1 {testing object foundation cleanup} memory {
80 leaktest {
81 interp create foo
82 interp delete foo
83 }
84 } 0
85 test oo-0.5.2 {testing literal leak on interp delete} memory {
86 leaktest {
87 interp create foo
88 foo eval {oo::object new}
89 interp delete foo
90 }
91 } 0
92 test oo-0.6 {cleaning the core class pair; way #1} -setup {
93 interp create t
94 } -body {
95 t eval {
96 package require TclOO
97 namespace path oo
98 list [catch {class destroy} m] $m [catch {object destroy} m] $m
99 }
100 } -cleanup {
101 interp delete t
102 } -result {0 {} 1 {invalid command name "object"}}
103 test oo-0.7 {cleaning the core class pair; way #2} -setup {
104 interp create t
105 } -body {
106 t eval {
107 package require TclOO
108 namespace path oo
109 list [catch {object destroy} m] $m [catch {class destroy} m] $m
110 }
111 } -cleanup {
112 interp delete t
113 } -result {0 {} 1 {invalid command name "class"}}
114 test oo-0.8 {leak in variable management} -setup {
115 oo::class create foo
116 } -constraints memory -body {
117 oo::define foo {
118 constructor {} {
119 variable v 0
120 }
121 }
122 leaktest {[foo new] destroy}
123 } -cleanup {
124 foo destroy
125 } -result 0
126 test oo-0.9 {various types of presence of the TclOO package} {
127 list [lsearch -nocase -all -inline [package names] tcloo] \
128 [package present TclOO] [expr {$::oo::patchlevel in [package versions TclOO]}]
129 } [list TclOO $::oo::patchlevel 1]
130
131 test oo-1.1 {basic test of OO functionality: no classes} {
132 set result {}
133 lappend result [oo::object create foo]
134 lappend result [oo::objdefine foo {
135 method bar args {
136 global result
137 lappend result {*}$args
138 return [llength $args]
139 }
140 }]
141 lappend result [foo bar a b c]
142 lappend result [foo destroy] [info commands foo]
143 } {::foo {} a b c 3 {} {}}
144 test oo-1.2 {basic test of OO functionality: no classes} -body {
145 oo::define oo::object method missingArgs
146 } -returnCodes 1 -result "wrong # args: should be \"oo::define oo::object method name args body\""
147 test oo-1.3 {basic test of OO functionality: no classes} {
148 catch {oo::define oo::object method missingArgs}
149 set errorInfo
150 } "wrong # args: should be \"oo::define oo::object method name args body\"
151 while executing
152 \"oo::define oo::object method missingArgs\""
153 test oo-1.4 {basic test of OO functionality} -body {
154 oo::object create {}
155 } -returnCodes 1 -result {object name must not be empty}
156 test oo-1.4.1 {fully-qualified nested name} -body {
157 oo::object create ::one::two::three
158 } -result {::one::two::three}
159 test oo-1.4.2 {automatic command name has same name as namespace} -body {
160 set obj [oo::object new]
161 expr {[info object namespace $obj] == $obj}
162 } -result 1
163 test oo-1.5 {basic test of OO functionality} -body {
164 oo::object doesnotexist
165 } -returnCodes 1 -result {unknown method "doesnotexist": must be create, destroy or new}
166 test oo-1.5.1 {basic test of OO functionality} -setup {
167 oo::object create aninstance
168 } -returnCodes error -body {
169 aninstance
170 } -cleanup {
171 rename aninstance {}
172 } -result {wrong # args: should be "aninstance method ?arg ...?"}
173 test oo-1.6 {basic test of OO functionality} -setup {
174 oo::object create aninstance
175 } -body {
176 oo::objdefine aninstance unexport destroy
177 aninstance doesnotexist
178 } -cleanup {
179 rename aninstance {}
180 } -returnCodes 1 -result {object "::aninstance" has no visible methods}
181 test oo-1.7 {basic test of OO functionality} -setup {
182 oo::object create aninstance
183 } -body {
184 oo::objdefine aninstance {
185 # Do not do this in real code! Ever! This is *not* supported!
186 ::oo::define::method ha ha ha
187 }
188 } -returnCodes error -cleanup {
189 aninstance destroy
190 } -result {attempt to misuse API}
191 test oo-1.8 {basic test of OO functionality} -setup {
192 oo::object create obj
193 set result {}
194 } -cleanup {
195 obj destroy
196 } -body {
197 oo::objdefine obj method foo {} {return bar}
198 lappend result [obj foo]
199 oo::objdefine obj method foo {} {}
200 lappend result [obj foo]
201 } -result {bar {}}
202 test oo-1.9 {basic test of OO functionality} -setup {
203 oo::object create a
204 oo::object create b
205 } -cleanup {
206 catch {a destroy}
207 b destroy
208 } -body {
209 oo::objdefine a method foo {} { return A }
210 oo::objdefine b method foo {} { return B }
211 apply {{} {
212 set m foo
213 return [a $m],[a destroy],[b $m]
214 }}
215 } -result A,,B
216 test oo-1.10 {basic test of OO functionality} -body {
217 namespace eval foo {
218 namespace eval bar {
219 oo::object create o
220 namespace export o
221 }
222 namespace import bar::o
223 }
224 list [info object isa object foo::bar::o] [info object isa object foo::o]
225 } -cleanup {
226 namespace delete foo
227 } -result {1 1}
228 test oo-1.11 {basic test of OO functionality: abbreviating} -setup {
229 oo::class create c
230 } -cleanup {
231 c destroy
232 } -body {
233 oo::define c super oo::class
234 info class super c
235 } -result ::oo::class
236 test oo-1.12 {basic test of OO functionality: abbreviating} -setup {
237 oo::class create c
238 } -cleanup {
239 c destroy
240 } -body {
241 oo::define c {super oo::class}
242 info class super c
243 } -result ::oo::class
244 test oo-1.13 {basic test of OO functionality: abbreviating} -setup {
245 oo::class create c
246 } -cleanup {
247 c destroy
248 } -body {
249 oo::define c self {forw a b}
250 info object forw c a
251 } -result b
252 test oo-1.14 {basic test of OO functionality: abbreviating} -setup {
253 oo::class create c
254 } -cleanup {
255 c destroy
256 } -body {
257 oo::define c self forw a b
258 info object forw c a
259 } -result b
260 test oo-1.15 {basic test of OO functionality: abbreviating} -setup {
261 oo::object create o
262 } -cleanup {
263 o destroy
264 } -body {
265 oo::objdefine o {forw a b}
266 info object forw o a
267 } -result b
268 test oo-1.16 {basic test of OO functionality: abbreviating} -setup {
269 oo::object create o
270 } -cleanup {
271 o destroy
272 } -body {
273 oo::objdefine o forw a b
274 info object forw o a
275 } -result b
276 test oo-1.17 {basic test of OO functionality: Bug 2481109} -body {
277 namespace eval ::foo {oo::object create lreplace}
278 } -cleanup {
279 namespace delete ::foo
280 } -result ::foo::lreplace
281 # Check for Bug 2519474; problem in tclNamesp.c, but tested here...
282 test oo-1.18 {OO: create object in NS with same name as global cmd} -setup {
283 proc test-oo-1.18 {} return
284 oo::class create A
285 oo::class create B {superclass A}
286 } -body {
287 oo::define B constructor {} {A create test-oo-1.18}
288 B create C
289 } -cleanup {
290 rename test-oo-1.18 {}
291 A destroy
292 } -result ::C
293 test oo-1.18.1 {no memory leak: superclass} -setup {
294 } -constraints memory -body {
295
296 leaktest {
297 interp create t
298 t eval {
299 oo::class create A {
300 superclass oo::class
301 }
302 }
303 interp delete t
304 }
305 } -cleanup {
306 } -result 0
307 test oo-1.18.2 {Bug 75b8433707: memory leak in oo-1.18} -setup {
308 proc test-oo-1.18 {} return
309 } -constraints memory -body {
310 leaktest {
311 oo::class create A
312 oo::class create B {superclass A}
313 oo::define B constructor {} {A create test-oo-1.18}
314 B create C
315 A destroy
316 }
317 } -cleanup {
318 rename test-oo-1.18 {}
319 } -result 0
320 test oo-1.18.3 {Bug 21c144f0f5} -setup {
321 interp create child
322 } -body {
323 child eval {
324 oo::define [oo::class create foo] superclass oo::class
325 oo::class destroy
326 }
327 } -cleanup {
328 interp delete child
329 }
330 test oo-1.18.4 {correct handling of cleanup in superclass set error} -setup {
331 interp create child
332 } -body {
333 child eval {
334 oo::class create A
335 oo::class create B {
336 superclass oo::class
337 constructor {} {
338 next {superclass A}
339 next {superclass -append A}
340 }
341 }
342 [B create C] create d
343 }
344 } -returnCodes error -cleanup {
345 interp delete child
346 } -result {class should only be a direct superclass once}
347 test oo-1.18.5 {correct handling of cleanup in superclass set error} -setup {
348 interp create child
349 } -body {
350 child eval {
351 oo::class create A
352 oo::class create B {
353 superclass oo::class
354 constructor {c} {
355 next {superclass A}
356 next [list superclass -append {*}$c]
357 }
358 }
359 [B create C {B C}] create d
360 }
361 } -returnCodes error -cleanup {
362 interp delete child
363 } -result {attempt to form circular dependency graph}
364 test oo-1.19 {basic test of OO functionality: teardown order} -body {
365 oo::object create o
366 namespace delete [info object namespace o]
367 o destroy
368 # Crashes on error
369 } -returnCodes error -result {invalid command name "o"}
370 test oo-1.20 {basic test of OO functionality: my teardown post rename} -body {
371 oo::object create obj
372 rename [info object namespace obj]::my ::AGlobalName
373 obj destroy
374 info commands ::AGlobalName
375 } -result {}
376 test oo-1.21 {basic test of OO functionality: default relations} -setup {
377 set fresh [interp create]
378 } -body {
379 lmap x [$fresh eval {
380 foreach cmd {instances subclasses mixins superclass} {
381 foreach initial {object class Slot} {
382 lappend x [info class $cmd ::oo::$initial]
383 }
384 }
385 foreach initial {object class Slot} {
386 lappend x [info object class ::oo::$initial]
387 }
388 return $x
389 }] {lsort $x}
390 } -cleanup {
391 interp delete $fresh
392 } -result {{} {::oo::Slot ::oo::class ::oo::object} {::oo::define::filter ::oo::define::mixin ::oo::define::superclass ::oo::define::variable ::oo::objdefine::filter ::oo::objdefine::mixin ::oo::objdefine::variable} {::oo::Slot ::oo::class} {} {} {} {} {} {} ::oo::object ::oo::object ::oo::class ::oo::class ::oo::class}
393 test oo-1.22 {basic test of OO functionality: nested ownership destruction order} -setup {
394 oo::class create parent
395 } -body {
396 oo::class create abc {
397 superclass parent
398 variable n
399 constructor {} {set n 0}
400 method make {i} {set n $i; [self class] create xyz}
401 destructor {lappend ::deathOrder $n}
402 }
403 apply {n {
404 set ::deathOrder {}
405 # Make some "nested" objects
406 set base [abc new]
407 for {set i 1; set obj $base} {$i < $n} {incr i} {
408 set obj [$obj make $i]
409 }
410 # Kill them all in one go; should come apart in right order!
411 $base destroy
412 return $::deathOrder
413 }} 5
414 } -cleanup {
415 parent destroy
416 } -result {1 2 3 4 0}
417 test oo-1.23 {basic test of OO functionality: deep nested ownership} -setup {
418 oo::class create parent
419 } -constraints knownBug -body {
420 oo::class create abc {
421 superclass parent
422 method make {} {[self class] create xyz}
423 destructor {incr ::count}
424 }
425 apply {n {
426 set ::count 0
427 # Make a lot of "nested" objects
428 set base [abc new]
429 for {set i 1; set obj $base} {$i < $n} {incr i} {
430 set obj [$obj make]
431 }
432 # Kill them all in one go; should not crash!
433 $base destroy
434 return [expr {$n - $::count}]
435 }} 10000
436 } -cleanup {
437 parent destroy
438 } -result 0
439 test oo-1.24 {basic test of OO functionality: deep nested ownership} -setup {
440 oo::class create parent
441 } -constraints knownBug -body {
442 oo::class create abc {
443 superclass parent
444 self method make {} {oo::copy [self] xyz}
445 }
446 apply {n {
447 # Make a lot of "nested" objects
448 set base abc
449 lappend lst $base [info object namespace $base]
450 for {set i 1; set obj $base} {$i < $n} {incr i} {
451 set obj [$obj make]
452 lappend lst $obj [info object namespace $obj]
453 }
454 # Kill them all in one go; should not crash!
455 $base destroy
456 # How many classes still there (cnt must remain 0)
457 set cnt 0
458 foreach {obj ns} $lst {
459 if {[namespace which -command $obj] ne "" || [namespace exists $ns]} {
460 incr cnt
461 }
462 }
463 return $cnt
464 }} 10000
465 } -cleanup {
466 parent destroy
467 } -result 0
468
469 test oo-1.25 {basic test of OO functionality: touch method after instance deletion, bug [0b809cd3fc8b6e5e]} -body {
470 set ::result {}
471 # test for eval and deletion of coro, in both cases the coroutine shall be deleted
472 foreach v {"eval" "del"} {
473 # 1st (deleted class)
474 oo::class create A
475 oo::define A method retard-it {} {yield}
476 coroutine tcoro [A new] retard-it
477 trace add command tcoro delete {apply {{args} {lappend ::result D}}}
478 A destroy
479 if {$v eq "eval"} { tcoro } else { rename tcoro {} }
480 # 2nd (deleted object of class)
481 oo::class create A
482 oo::define A method retard-it {} {yield}
483 set obj [A new]
484 coroutine tcoro $obj retard-it
485 trace add command tcoro delete {apply {{args} {lappend ::result D}}}
486 $obj destroy
487 if {$v eq "eval"} { tcoro } else { rename tcoro {} }
488 A destroy
489 # 3rd (deleted object)
490 set obj [oo::object new]
491 oo::objdefine $obj method retard-it {} {yield}
492 coroutine tcoro $obj retard-it
493 trace add command tcoro delete {apply {{args} {lappend ::result D}}}
494 $obj destroy
495 if {$v eq "eval"} { tcoro } else { rename tcoro {} }
496 }
497 set ::result
498 } -result [lrepeat 6 D]
499
500 test oo-2.1 {basic test of OO functionality: constructor} -setup {
501 # This is a bit complex because it needs to run in a sub-interp as
502 # we're modifying the root object class's constructor
503 interp create subinterp
504 subinterp eval {
505 package require TclOO
506 }
507 } -body {
508 subinterp eval {
509 oo::define oo::object constructor {} {
510 lappend ::result [info level 0]
511 }
512 lappend result 1
513 lappend result 2 [oo::object create foo]
514 }
515 } -cleanup {
516 interp delete subinterp
517 } -result {1 {oo::object create foo} 2 ::foo}
518 test oo-2.2 {basic test of OO functionality: constructor} {
519 oo::class create testClass {
520 constructor {} {
521 global result
522 lappend result "[self]->construct"
523 }
524 method bar {} {
525 global result
526 lappend result "[self]->bar"
527 }
528 }
529 set result {}
530 [testClass create foo] bar
531 testClass destroy
532 return $result
533 } {::foo->construct ::foo->bar}
534 test oo-2.4 {OO constructor - Bug 2531577} -setup {
535 oo::class create foo
536 } -body {
537 oo::define foo constructor {} return
538 [foo new] destroy
539 oo::define foo constructor {} {}
540 llength [info command [foo new]]
541 } -cleanup {
542 foo destroy
543 } -result 1
544 test oo-2.5 {OO constructor - Bug 2531577} -setup {
545 oo::class create foo
546 set result {}
547 } -body {
548 oo::define foo constructor {} {error x}
549 lappend result [catch {foo new}]
550 oo::define foo constructor {} {}
551 lappend result [llength [info command [foo new]]]
552 } -cleanup {
553 foo destroy
554 } -result {1 1}
555 test oo-2.6 {OO constructor and tailcall - Bug 2414858} -setup {
556 oo::class create foo
557 } -body {
558 oo::define foo {
559 constructor {} { tailcall my bar }
560 method bar {} { return bad }
561 }
562 namespace tail [foo create good]
563 } -cleanup {
564 foo destroy
565 } -result good
566 test oo-2.7 {construction, method calls and ensembles - Bug 3514761} -setup {
567 namespace eval k {}
568 } -body {
569 namespace eval k {
570 oo::class create s {
571 constructor {j} {
572 # nothing
573 }
574 }
575 namespace export s
576 namespace ensemble create
577 }
578 k s create X
579 } -returnCodes error -cleanup {
580 namespace delete k
581 } -result {wrong # args: should be "k s create X j"}
582 test oo-2.8 {construction, method calls and ensembles - Bug 3514761} -setup {
583 namespace eval k {}
584 } -body {
585 namespace eval k {
586 oo::class create s {
587 constructor {j} {
588 # nothing
589 }
590 }
591 oo::class create t {
592 superclass s
593 constructor args {
594 k next {*}$args
595 }
596 }
597 interp alias {} ::k::next {} ::oo::Helpers::next
598 namespace export t next
599 namespace ensemble create
600 }
601 k t create X
602 } -returnCodes error -cleanup {
603 namespace delete k
604 } -result {wrong # args: should be "k next j"}
605 test oo-2.9 {construction failures and self creation} -setup {
606 set ::result {}
607 oo::class create Root
608 } -body {
609 oo::class create A {
610 superclass Root
611 constructor {} {
612 lappend ::result "in A"
613 error "failure in A"
614 }
615 destructor {lappend ::result [self]}
616 }
617 oo::class create B {
618 superclass Root
619 constructor {} {
620 lappend ::result "in B [self]"
621 error "failure in B"
622 }
623 destructor {lappend ::result [self]}
624 }
625 lappend ::result [catch {A create a} msg] $msg
626 lappend ::result [catch {B create b} msg] $msg
627 } -cleanup {
628 Root destroy
629 } -result {{in A} ::a 1 {failure in A} {in B ::b} ::b 1 {failure in B}}
630
631 test oo-3.1 {basic test of OO functionality: destructor} -setup {
632 # This is a bit complex because it needs to run in a sub-interp as we're
633 # modifying the root object class's constructor
634 interp create subinterp
635 subinterp eval {
636 package require TclOO
637 }
638 } -body {
639 subinterp eval {
640 oo::define oo::object destructor {
641 lappend ::result died
642 }
643 lappend result 1 [oo::object create foo]
644 lappend result 2 [rename foo {}]
645 oo::define oo::object destructor {}
646 return $result
647 }
648 } -cleanup {
649 interp delete subinterp
650 } -result {1 ::foo died 2 {}}
651 test oo-3.2 {basic test of OO functionality: destructor} -setup {
652 # This is a bit complex because it needs to run in a sub-interp as
653 # we're modifying the root object class's constructor
654 interp create subinterp
655 subinterp eval {
656 package require TclOO
657 }
658 } -body {
659 subinterp eval {
660 oo::define oo::object destructor {
661 lappend ::result died
662 }
663 lappend result 1 [oo::object create foo]
664 lappend result 2 [rename foo {}]
665 }
666 } -cleanup {
667 interp delete subinterp
668 } -result {1 ::foo died 2 {}}
669 test oo-3.3 {basic test of OO functionality: destructor} -setup {
670 oo::class create foo
671 set result {}
672 } -cleanup {
673 foo destroy
674 } -body {
675 oo::define foo {
676 constructor {} {lappend ::result made}
677 destructor {lappend ::result died}
678 }
679 namespace delete [info object namespace [foo new]]
680 return $result
681 } -result {made died}
682 test oo-3.4 {basic test of OO functionality: my exists in destructor} -setup {
683 oo::class create cls
684 set result {}
685 } -cleanup {
686 cls destroy
687 } -body {
688 oo::define cls {
689 variable state
690 constructor {} {
691 proc localcmdexists {} {}
692 set state ok
693 }
694 forward Report lappend ::result
695 destructor {
696 objmy Report [catch {set state} msg] $msg
697 objmy Report [namespace which -var state]
698 objmy Report [info commands localcmdexists]
699 }
700 }
701 cls create obj
702 rename [info object namespace obj]::my ::objmy
703 obj destroy
704 lappend result [info commands ::objmy]
705 } -match glob -result {0 ok *::state localcmdexists {}}
706 test oo-3.4a {basic test of OO functionality: my exists in destructor} -setup {
707 oo::class create cls
708 set result {}
709 } -cleanup {
710 cls destroy
711 } -body {
712 oo::define cls {
713 variable state
714 constructor {} {
715 proc localcmdexists {} {}
716 set state ok
717 }
718 forward Report lappend ::result
719 destructor {
720 objmy Report [catch {set state} msg] $msg
721 objmy Report [namespace which -var state]
722 objmy Report [info commands localcmdexists]
723 }
724 }
725 cls create obj
726 rename [info object namespace obj]::my ::objmy
727 rename obj {}
728 lappend result [info commands ::objmy]
729 } -match glob -result {0 ok *::state localcmdexists {}}
730 test oo-3.5 {basic test of OO functionality: destructor: evil case for Itcl} -setup {
731 oo::class create cls
732 set result {}
733 } -cleanup {
734 cls destroy
735 } -body {
736 oo::define cls {
737 variable state
738 constructor {} {
739 proc localcmdexists {} {}
740 set state ok
741 }
742 forward Report lappend ::result
743 destructor {
744 objmy Report [catch {set state} msg] $msg
745 objmy Report [namespace which -var state]
746 objmy Report [info commands localcmdexists]
747 }
748 }
749 cls create obj
750 rename [info object namespace obj]::my ::objmy
751 namespace delete [info object namespace obj]
752 lappend result [info commands ::objmy]
753 } -match glob -result {0 ok *::state localcmdexists {}}
754 test oo-3.5a {basic test of OO functionality: destructor: evil case for Itcl} -setup {
755 oo::class create cls
756 set result {}
757 } -cleanup {
758 cls destroy
759 } -body {
760 oo::define cls {
761 variable state result
762 constructor {} {
763 proc localcmdexists {} {}
764 set state ok
765 my eval {upvar 0 ::result result}
766 }
767 method nuke {} {
768 namespace delete [namespace current]
769 return $result
770 }
771 destructor {
772 lappend result [self] $state [info commands localcmdexists]
773 }
774 }
775 cls create obj
776 namespace delete [info object namespace obj]
777 [cls create obj2] nuke
778 } -match glob -result {::obj ok localcmdexists ::obj2 ok localcmdexists}
779 test oo-3.6 {basic test of OO functionality: errors in destructor} -setup {
780 oo::class create cls
781 } -cleanup {
782 cls destroy
783 } -body {
784 oo::define cls destructor {error foo}
785 list [catch {[cls create obj] destroy} msg] $msg [info commands obj]
786 } -result {1 foo {}}
787 test oo-3.7 {basic test of OO functionality: errors in destructor} -setup {
788 oo::class create cls
789 } -cleanup {
790 cls destroy
791 } -body {
792 oo::define cls destructor {error foo}
793 bgerrorIntercept result {
794 set result [cls create obj]
795 lappend result [rename obj {}]
796 update idletasks
797 lappend result [info commands obj]
798 }
799 } -result {::obj {} foo {}}
800 test oo-3.8 {basic test of OO functionality: errors in destructor} -setup {
801 oo::class create cls
802 } -cleanup {
803 cls destroy
804 } -body {
805 oo::define cls destructor {error foo}
806 bgerrorIntercept result {
807 set result [cls create obj]
808 lappend result [namespace delete [info object namespace obj]]
809 update idletasks
810 lappend result [info commands obj]
811 }
812 } -result {::obj {} foo {}}
813 test oo-3.9 {Bug 2944404: deleting the object in the destructor} -setup {
814 oo::class create cls
815 set result {}
816 } -body {
817 oo::define cls {
818 destructor {
819 lappend ::result in destructor
820 [self] destroy
821 }
822 }
823 # This used to crash
824 [cls new] destroy
825 return $result
826 } -cleanup {
827 cls destroy
828 } -result {in destructor}
829 test oo-3.10 {Bug 3d96b7076e: killing the object's class in a method call} -setup {
830 oo::class create Super
831 } -body {
832 # Only reliably failed in a memdebug build
833 oo::class create Cls {
834 superclass Super
835 method mthd {} {
836 [self class] destroy
837 return ok
838 }
839 }
840 [Cls new] mthd
841 } -cleanup {
842 Super destroy
843 } -result ok
844 test oo-3.11 {Bug 3d96b7076e: killing the object's class in a method call} -setup {
845 oo::class create Super
846 oo::class create Sub {
847 superclass Super
848 }
849 } -body {
850 # Only reliably failed in a memdebug build
851 oo::class create Cls {
852 superclass Super
853 method mthd {} {
854 oo::objdefine [self] class Sub
855 Cls destroy
856 return ok
857 }
858 }
859 [Cls new] mthd
860 } -cleanup {
861 Super destroy
862 } -result ok
863 test oo-3.12 {Bug 3d96b7076e: killing the object's class in a method call} -setup {
864 oo::class create Super
865 } -body {
866 # Only reliably failed in a memdebug build
867 oo::class create Cls {
868 superclass Super
869 method mthd {} {
870 [self class] destroy
871 return ok
872 }
873 }
874 set o [Super new]
875 oo::objdefine $o mixin Cls
876 $o mthd
877 } -cleanup {
878 Super destroy
879 } -result ok
880
881 test oo-4.1 {basic test of OO functionality: export} {
882 set o [oo::object new]
883 set result {}
884 oo::objdefine $o method Foo {} {lappend ::result Foo; return}
885 lappend result [catch {$o Foo} msg] $msg
886 oo::objdefine $o export Foo
887 lappend result [$o Foo] [$o destroy]
888 } {1 {unknown method "Foo": must be destroy} Foo {} {}}
889 test oo-4.2 {basic test of OO functionality: unexport} {
890 set o [oo::object new]
891 set result {}
892 oo::objdefine $o method foo {} {lappend ::result foo; return}
893 lappend result [$o foo]
894 oo::objdefine $o unexport foo
895 lappend result [catch {$o foo} msg] $msg [$o destroy]
896 } {foo {} 1 {unknown method "foo": must be destroy} {}}
897 test oo-4.3 {exporting and error messages, Bug 1824958} -setup {
898 oo::class create testClass
899 } -cleanup {
900 testClass destroy
901 } -body {
902 oo::define testClass self export Bad
903 testClass Bad
904 } -returnCodes 1 -result {unknown method "Bad": must be create, destroy or new}
905 test oo-4.4 {exporting a class method from an object} -setup {
906 oo::class create testClass
907 testClass create testObject
908 } -cleanup {
909 testClass destroy
910 } -body {
911 oo::define testClass method Good {} { return ok }
912 oo::objdefine testObject export Good
913 testObject Good
914 } -result ok
915 test oo-4.5 {export creates proper method entries} -setup {
916 oo::class create testClass
917 } -body {
918 oo::define testClass {
919 export foo
920 method foo {} {return ok}
921 }
922 [testClass new] foo
923 } -cleanup {
924 testClass destroy
925 } -result ok
926 test oo-4.6 {export creates proper method entries} -setup {
927 oo::class create testClass
928 } -body {
929 oo::define testClass {
930 unexport foo
931 method foo {} {return ok}
932 }
933 [testClass new] foo
934 } -cleanup {
935 testClass destroy
936 } -result ok
937
938 test oo-5.1 {OO: manipulation of classes as objects} -setup {
939 set obj [oo::object new]
940 } -body {
941 oo::objdefine oo::object method foo {} { return "in object" }
942 catch {$obj foo} result
943 list [catch {$obj foo} result] $result [oo::object foo]
944 } -cleanup {
945 oo::objdefine oo::object deletemethod foo
946 $obj destroy
947 } -result {1 {unknown method "foo": must be destroy} {in object}}
948 test oo-5.2 {OO: manipulation of classes as objects} -setup {
949 set obj [oo::object new]
950 } -body {
951 oo::define oo::object self method foo {} { return "in object" }
952 catch {$obj foo} result
953 list [catch {$obj foo} result] $result [oo::object foo]
954 } -cleanup {
955 oo::objdefine oo::object deletemethod foo
956 $obj destroy
957 } -result {1 {unknown method "foo": must be destroy} {in object}}
958 test oo-5.3 {OO: manipulation of classes as objects} -setup {
959 set obj [oo::object new]
960 } -body {
961 oo::objdefine oo::object {
962 method foo {} { return "in object" }
963 }
964 catch {$obj foo} result
965 list [catch {$obj foo} result] $result [oo::object foo]
966 } -cleanup {
967 oo::objdefine oo::object deletemethod foo
968 $obj destroy
969 } -result {1 {unknown method "foo": must be destroy} {in object}}
970 test oo-5.4 {OO: manipulation of classes as objects} -setup {
971 set obj [oo::object new]
972 } -body {
973 oo::define oo::object {
974 self method foo {} { return "in object" }
975 }
976 catch {$obj foo} result
977 list [catch {$obj foo} result] $result [oo::object foo]
978 } -cleanup {
979 oo::objdefine oo::object deletemethod foo
980 $obj destroy
981 } -result {1 {unknown method "foo": must be destroy} {in object}}
982 test oo-5.5 {OO: manipulation of classes as objects} -setup {
983 set obj [oo::object new]
984 } -body {
985 oo::define oo::object {
986 self {
987 method foo {} { return "in object" }
988 }
989 }
990 catch {$obj foo} result
991 list [catch {$obj foo} result] $result [oo::object foo]
992 } -cleanup {
993 oo::objdefine oo::object deletemethod foo
994 $obj destroy
995 } -result {1 {unknown method "foo": must be destroy} {in object}}
996
997 test oo-6.1 {OO: forward} {
998 oo::object create foo
999 oo::objdefine foo {
1000 forward a lappend
1001 forward b lappend result
1002 }
1003 set result {}
1004 foo a result 1
1005 foo b 2
1006 foo destroy
1007 return $result
1008 } {1 2}
1009 test oo-6.2 {OO: forward resolution scope} -setup {
1010 oo::class create fooClass
1011 } -body {
1012 proc foo {} {return bad}
1013 oo::define fooClass {
1014 constructor {} {
1015 proc foo {} {return good}
1016 }
1017 forward bar foo
1018 }
1019 [fooClass new] bar
1020 } -cleanup {
1021 fooClass destroy
1022 rename foo {}
1023 } -result good
1024 test oo-6.3 {OO: forward resolution scope} -setup {
1025 oo::class create fooClass
1026 } -body {
1027 proc foo {} {return bad}
1028 oo::define fooClass {
1029 constructor {} {
1030 proc foo {} {return good}
1031 }
1032 }
1033 oo::define fooClass forward bar foo
1034 [fooClass new] bar
1035 } -cleanup {
1036 fooClass destroy
1037 rename foo {}
1038 } -result good
1039 test oo-6.4 {OO: forward resolution scope} -setup {
1040 oo::class create fooClass
1041 } -body {
1042 proc foo {} {return good}
1043 oo::define fooClass {
1044 constructor {} {
1045 proc foo {} {return bad}
1046 }
1047 forward bar ::foo
1048 }
1049 [fooClass new] bar
1050 } -cleanup {
1051 fooClass destroy
1052 rename foo {}
1053 } -result good
1054 test oo-6.5 {OO: forward resolution scope} -setup {
1055 oo::class create fooClass
1056 namespace eval foo {}
1057 } -body {
1058 proc foo::foo {} {return good}
1059 oo::define fooClass {
1060 constructor {} {
1061 proc foo {} {return bad}
1062 }
1063 forward bar foo::foo
1064 }
1065 [fooClass new] bar
1066 } -cleanup {
1067 fooClass destroy
1068 namespace delete foo
1069 } -result good
1070 test oo-6.6 {OO: forward resolution scope} -setup {
1071 oo::class create fooClass
1072 namespace eval foo {}
1073 } -body {
1074 proc foo::foo {} {return bad}
1075 oo::define fooClass {
1076 constructor {} {
1077 namespace eval foo {
1078 proc foo {} {return good}
1079 }
1080 }
1081 forward bar foo::foo
1082 }
1083 [fooClass new] bar
1084 } -cleanup {
1085 fooClass destroy
1086 namespace delete foo
1087 } -result good
1088 test oo-6.7 {OO: forward resolution scope is per-object} -setup {
1089 oo::class create fooClass
1090 } -body {
1091 oo::define fooClass {
1092 constructor {} {
1093 proc curns {} {namespace current}
1094 }
1095 forward ns curns
1096 }
1097 expr {[[fooClass new] ns] ne [[fooClass new] ns]}
1098 } -cleanup {
1099 fooClass destroy
1100 } -result 1
1101 test oo-6.8 {Bug 3400658: forwarding and wrongargs rewriting} -setup {
1102 oo::class create fooClass
1103 } -body {
1104 oo::define fooClass {
1105 forward test my handler
1106 method handler {a b c} {}
1107 }
1108 fooClass create ::foo
1109 foo test
1110 } -returnCodes error -cleanup {
1111 fooClass destroy
1112 } -result {wrong # args: should be "foo test a b c"}
1113 test oo-6.9 {Bug 3400658: forwarding and wrongargs rewriting} -setup {
1114 oo::class create fooClass
1115 } -body {
1116 oo::define fooClass {
1117 forward test my handler
1118 method handler {a b c} {list $a,$b,$c}
1119 }
1120 fooClass create ::foo
1121 foo test 1 2 3
1122 } -cleanup {
1123 fooClass destroy
1124 } -result 1,2,3
1125 test oo-6.10 {Bug 3400658: forwarding and wrongargs rewriting} -setup {
1126 oo::class create fooClass
1127 } -body {
1128 oo::define fooClass {
1129 forward test my handler
1130 method handler {a b c} {list $a,$b,$c}
1131 }
1132 fooClass create ::foo
1133 foo test 1 2
1134 } -returnCodes error -cleanup {
1135 fooClass destroy
1136 } -result {wrong # args: should be "foo test a b c"}
1137 test oo-6.11 {Bug 3400658: forwarding and wrongargs rewriting} -setup {
1138 oo::object create foo
1139 } -body {
1140 oo::objdefine foo {
1141 forward test my handler
1142 method handler {a b c} {}
1143 }
1144 foo test
1145 } -returnCodes error -cleanup {
1146 foo destroy
1147 } -result {wrong # args: should be "foo test a b c"}
1148 test oo-6.12 {Bug 3400658: forwarding and wrongargs rewriting} -setup {
1149 oo::object create foo
1150 } -body {
1151 oo::objdefine foo {
1152 forward test my handler
1153 method handler {a b c} {list $a,$b,$c}
1154 }
1155 foo test 1 2 3
1156 } -cleanup {
1157 foo destroy
1158 } -result 1,2,3
1159 test oo-6.13 {Bug 3400658: forwarding and wrongargs rewriting} -setup {
1160 oo::object create foo
1161 } -body {
1162 oo::objdefine foo {
1163 forward test my handler
1164 method handler {a b c} {list $a,$b,$c}
1165 }
1166 foo test 1 2
1167 } -returnCodes error -cleanup {
1168 foo destroy
1169 } -result {wrong # args: should be "foo test a b c"}
1170 test oo-6.14 {Bug 3400658: forwarding and wrongargs rewriting - multistep} -setup {
1171 oo::class create fooClass
1172 } -body {
1173 oo::define fooClass {
1174 forward test my handler1 p
1175 forward handler1 my handler q
1176 method handler {a b c} {}
1177 }
1178 fooClass create ::foo
1179 foo test
1180 } -returnCodes error -cleanup {
1181 fooClass destroy
1182 } -result {wrong # args: should be "foo test c"}
1183 test oo-6.15 {Bug 3400658: forwarding and wrongargs rewriting - multistep} -setup {
1184 oo::class create fooClass
1185 } -body {
1186 oo::define fooClass {
1187 forward test my handler1 p
1188 forward handler1 my handler q
1189 method handler {a b c} {list $a,$b,$c}
1190 }
1191 fooClass create ::foo
1192 foo test 1
1193 } -cleanup {
1194 fooClass destroy
1195 } -result q,p,1
1196 test oo-6.16 {Bug 3400658: forwarding and wrongargs rewriting - via alias} -setup {
1197 oo::class create fooClass
1198 } -body {
1199 oo::define fooClass {
1200 forward test handler1 foo bar
1201 forward handler2 my handler x
1202 method handler {a b c d} {list $a,$b,$c,$d}
1203 export eval
1204 }
1205 fooClass create ::foo
1206 foo eval {
1207 interp alias {} [namespace current]::handler1 \
1208 {} [namespace current]::my handler2
1209 }
1210 foo test 1 2 3
1211 } -returnCodes error -cleanup {
1212 fooClass destroy
1213 } -result {wrong # args: should be "foo test d"}
1214 test oo-6.17 {Bug 3400658: forwarding and wrongargs rewriting - via ensemble} -setup {
1215 oo::class create fooClass
1216 } -body {
1217 oo::define fooClass {
1218 forward test handler1 foo bar boo
1219 forward handler2 my handler
1220 method handler {a b c d} {list $a,$b,$c,$d}
1221 export eval
1222 }
1223 fooClass create ::foo
1224 foo eval {
1225 namespace ensemble create \
1226 -command [namespace current]::handler1 -parameters {p q} \
1227 -map [list boo [list [namespace current]::my handler2]]
1228 }
1229 foo test 1 2 3
1230 } -returnCodes error -cleanup {
1231 fooClass destroy
1232 } -result {wrong # args: should be "foo test c d"}
1233 test oo-6.18 {Bug 3408830: more forwarding cases} -setup {
1234 oo::class create fooClass
1235 } -body {
1236 oo::define fooClass {
1237 forward len string length
1238 }
1239 [fooClass create foo] len a b
1240 } -returnCodes error -cleanup {
1241 fooClass destroy
1242 } -result {wrong # args: should be "::foo len string"}
1243 test oo-6.19 {Bug 3610404: forwarding resolution + traces} -setup {
1244 oo::object create foo
1245 unset -nocomplain ::result
1246 set ::result {}
1247 } -body {
1248 proc ::my {method} {lappend ::result global}
1249 oo::objdefine foo {
1250 method target {} {lappend ::result instance}
1251 forward bar my target
1252 method bump {} {
1253 set ns [info object namespace ::foo]
1254 rename ${ns}::my ${ns}::
1255 rename ${ns}:: ${ns}::my
1256 }
1257 }
1258 proc harness {} {
1259 foo target
1260 foo bar
1261 foo target
1262 }
1263 trace add execution harness enterstep {apply {{cmd args} {foo bump}}}
1264 foo target
1265 foo bar
1266 foo bump
1267 foo bar
1268 harness
1269 } -cleanup {
1270 catch {rename harness {}}
1271 catch {rename ::my {}}
1272 foo destroy
1273 } -result {instance instance instance instance instance instance}
1274 test oo-6.20 {Bug 3610404: forwarding resolution + traces} -setup {
1275 oo::class create fooClass
1276 fooClass create foo
1277 unset -nocomplain ::result
1278 set ::result {}
1279 } -body {
1280 proc ::my {method} {lappend ::result global}
1281 oo::define fooClass {
1282 method target {} {lappend ::result class}
1283 forward bar my target
1284 method bump {} {
1285 set ns [info object namespace [self]]
1286 rename ${ns}::my ${ns}::
1287 rename ${ns}:: ${ns}::my
1288 }
1289 }
1290 proc harness {} {
1291 foo target
1292 foo bar
1293 foo target
1294 }
1295 trace add execution harness enterstep {apply {{cmd args} {foo bump}}}
1296 foo target
1297 foo bar
1298 foo bump
1299 foo bar
1300 harness
1301 } -cleanup {
1302 catch {rename harness {}}
1303 catch {rename ::my {}}
1304 fooClass destroy
1305 } -result {class class class class class class}
1306
1307 test oo-7.1 {OO: inheritance 101} -setup {
1308 oo::class create superClass
1309 oo::class create subClass
1310 subClass create instance
1311 } -body {
1312 oo::define superClass method doit x {lappend ::result $x}
1313 oo::define subClass superclass superClass
1314 set result [list [catch {subClass doit bad} msg] $msg]
1315 instance doit ok
1316 return $result
1317 } -cleanup {
1318 subClass destroy
1319 superClass destroy
1320 } -result {1 {unknown method "doit": must be create, destroy or new} ok}
1321 test oo-7.2 {OO: inheritance 101} -setup {
1322 oo::class create superClass
1323 oo::class create subClass
1324 subClass create instance
1325 } -body {
1326 oo::define superClass method doit x {
1327 lappend ::result |$x|
1328 }
1329 oo::define subClass superclass superClass
1330 oo::objdefine instance method doit x {
1331 lappend ::result =$x=
1332 next [incr x]
1333 }
1334 set result {}
1335 instance doit 1
1336 return $result
1337 } -cleanup {
1338 subClass destroy
1339 superClass destroy
1340 } -result {=1= |2|}
1341 test oo-7.3 {OO: inheritance 101} -setup {
1342 oo::class create superClass
1343 oo::class create subClass
1344 subClass create instance
1345 } -body {
1346 oo::define superClass method doit x {
1347 lappend ::result |$x|
1348 }
1349 oo::define subClass {
1350 superclass superClass
1351 method doit x {lappend ::result -$x-; next [incr x]}
1352 }
1353 oo::objdefine instance method doit x {
1354 lappend ::result =$x=;
1355 next [incr x]
1356 }
1357 set result {}
1358 instance doit 1
1359 return $result
1360 } -cleanup {
1361 subClass destroy
1362 superClass destroy
1363 } -result {=1= -2- |3|}
1364 test oo-7.4 {OO: inheritance from oo::class} -body {
1365 oo::class create meta {
1366 superclass oo::class
1367 self {
1368 unexport create new
1369 method make {x {definitions {}}} {
1370 if {![string match ::* $x]} {
1371 set ns [uplevel 1 {::namespace current}]
1372 set x ${ns}::$x
1373 }
1374 set o [my create $x]
1375 lappend ::result "made $o"
1376 oo::define $o $definitions
1377 return $o
1378 }
1379 }
1380 }
1381 set result [list [catch {meta create foo} msg] $msg]
1382 lappend result [meta make classinstance {
1383 lappend ::result "in definition script in [namespace current]"
1384 }]
1385 lappend result [classinstance create instance]
1386 } -cleanup {
1387 catch {classinstance destroy}
1388 catch {meta destroy}
1389 } -result {1 {unknown method "create": must be destroy or make} {made ::classinstance} {in definition script in ::oo::define} ::classinstance ::instance}
1390 test oo-7.5 {OO: inheritance from oo::class in the secondary chain} -body {
1391 oo::class create other
1392 oo::class create meta {
1393 superclass other oo::class
1394 self {
1395 unexport create new
1396 method make {x {definitions {}}} {
1397 if {![string match ::* $x]} {
1398 set ns [uplevel 1 {::namespace current}]
1399 set x ${ns}::$x
1400 }
1401 set o [my create $x]
1402 lappend ::result "made $o"
1403 oo::define $o $definitions
1404 return $o
1405 }
1406 }
1407 }
1408 set result [list [catch {meta create foo} msg] $msg]
1409 lappend result [meta make classinstance {
1410 lappend ::result "in definition script in [namespace current]"
1411 }]
1412 lappend result [classinstance create instance]
1413 } -cleanup {
1414 catch {classinstance destroy}
1415 catch {meta destroy}
1416 catch {other destroy}
1417 } -result {1 {unknown method "create": must be destroy or make} {made ::classinstance} {in definition script in ::oo::define} ::classinstance ::instance}
1418 test oo-7.6 {OO: inheritance 101 - overridden methods should be oblivious} -setup {
1419 oo::class create Aclass
1420 oo::class create Bclass
1421 Bclass create Binstance
1422 } -body {
1423 oo::define Aclass {
1424 method incr {var step} {
1425 upvar 1 $var v
1426 ::incr v $step
1427 }
1428 }
1429 oo::define Bclass {
1430 superclass Aclass
1431 method incr {var {step 1}} {
1432 global result
1433 lappend result $var $step
1434 set r [next $var $step]
1435 lappend result returning:$r
1436 return $r
1437 }
1438 }
1439 set result {}
1440 set x 10
1441 lappend result x=$x
1442 lappend result [Binstance incr x]
1443 lappend result x=$x
1444 } -result {x=10 x 1 returning:11 11 x=11} -cleanup {
1445 unset -nocomplain x
1446 Aclass destroy
1447 }
1448 test oo-7.7 {OO: inheritance and errorInfo} -setup {
1449 oo::class create A
1450 oo::class create B
1451 B create c
1452 } -body {
1453 oo::define A method foo {} {error foo!}
1454 oo::define B {
1455 superclass A
1456 method foo {} { next }
1457 }
1458 oo::objdefine c method foo {} { next }
1459 catch {c ?} msg
1460 set result [list $msg]
1461 catch {c foo} msg
1462 lappend result $msg $errorInfo
1463 } -cleanup {
1464 A destroy
1465 } -result {{unknown method "?": must be destroy or foo} foo! {foo!
1466 while executing
1467 "error foo!"
1468 (class "::A" method "foo" line 1)
1469 invoked from within
1470 "next "
1471 (class "::B" method "foo" line 1)
1472 invoked from within
1473 "next "
1474 (object "::c" method "foo" line 1)
1475 invoked from within
1476 "c foo"}}
1477 test oo-7.8 {OO: next at the end of the method chain} -setup {
1478 set ::result ""
1479 } -cleanup {
1480 foo destroy
1481 } -body {
1482 oo::class create foo {
1483 method bar {} {lappend ::result foo; lappend ::result [next] foo}
1484 }
1485 oo::class create foo2 {
1486 superclass foo
1487 method bar {} {lappend ::result foo2; lappend ::result [next] foo2}
1488 }
1489 lappend result [catch {[foo2 new] bar} msg] $msg
1490 } -result {foo2 foo 1 {no next method implementation}}
1491 test oo-7.9 {OO: defining inheritance in namespaces} -setup {
1492 set ::result {}
1493 oo::class create ::parent
1494 namespace eval ::foo {
1495 oo::class create mixin {superclass ::parent}
1496 }
1497 } -cleanup {
1498 ::parent destroy
1499 namespace delete ::foo
1500 } -body {
1501 namespace eval ::foo {
1502 oo::class create bar {superclass parent}
1503 oo::class create boo
1504 oo::define boo {superclass bar}
1505 oo::define boo {mixin mixin}
1506 oo::class create spong {superclass boo}
1507 return
1508 }
1509 } -result {}
1510 test oo-7.10 {OO: next after object deletion, bug [135804138e]} -setup {
1511 set ::result ""
1512 oo::class create c1 {
1513 method m1 {} {
1514 lappend ::result c1::m1
1515 }
1516 }
1517 oo::class create c2 {
1518 superclass c1
1519 destructor {
1520 lappend ::result c2::destructor
1521 my m1
1522 lappend ::result /c2::destructor
1523 }
1524 method m1 {} {
1525 lappend ::result c2::m1
1526 rename [self] {}
1527 lappend ::result no-self
1528 next
1529 lappend ::result /c2::m1
1530 }
1531 }
1532 } -body {
1533 c2 create o
1534 lappend ::result [catch {o m1} msg] $msg
1535 } -cleanup {
1536 c1 destroy
1537 unset ::result
1538 } -result {c2::m1 c2::destructor c2::m1 no-self c1::m1 /c2::m1 /c2::destructor no-self 1 {no next method implementation}}
1539
1540 test oo-8.1 {OO: global must work in methods} {
1541 oo::object create foo
1542 oo::objdefine foo method bar x {global result; lappend result $x}
1543 set result {}
1544 foo bar this
1545 foo bar is
1546 lappend result a
1547 foo bar test
1548 foo destroy
1549 return $result
1550 } {this is a test}
1551
1552 test oo-9.1 {OO: multiple inheritance} -setup {
1553 oo::class create A
1554 oo::class create B
1555 oo::class create C
1556 oo::class create D
1557 D create foo
1558 } -body {
1559 oo::define A method test {} {lappend ::result A; return ok}
1560 oo::define B {
1561 superclass A
1562 method test {} {lappend ::result B; next}
1563 }
1564 oo::define C {
1565 superclass A
1566 method test {} {lappend ::result C; next}
1567 }
1568 oo::define D {
1569 superclass B C
1570 method test {} {lappend ::result D; next}
1571 }
1572 set result {}
1573 lappend result [foo test]
1574 } -cleanup {
1575 D destroy
1576 C destroy
1577 B destroy
1578 A destroy
1579 } -result {D B C A ok}
1580 test oo-9.2 {OO: multiple inheritance} -setup {
1581 oo::class create A
1582 oo::class create B
1583 oo::class create C
1584 oo::class create D
1585 D create foo
1586 } -body {
1587 oo::define A method test {} {lappend ::result A; return ok}
1588 oo::define B {
1589 superclass A
1590 method test {} {lappend ::result B; next}
1591 }
1592 oo::define C {
1593 superclass A
1594 method test {} {lappend ::result C; next}
1595 }
1596 oo::define D {
1597 superclass B C
1598 method test {} {lappend ::result D; next}
1599 }
1600 set result {}
1601 lappend result [foo test]
1602 } -cleanup {
1603 A destroy
1604 } -result {D B C A ok}
1605
1606 test oo-10.1 {OO: recursive invoke and modify} -setup {
1607 [oo::class create C] create O
1608 } -cleanup {
1609 C destroy
1610 } -body {
1611 oo::define C method foo x {
1612 lappend ::result $x
1613 if {$x} {
1614 [self object] foo [incr x -1]
1615 }
1616 }
1617 oo::objdefine O method foo x {
1618 lappend ::result -$x-
1619 if {$x == 1} {
1620 oo::objdefine O deletemethod foo
1621 }
1622 next $x
1623 }
1624 set result {}
1625 O foo 2
1626 return $result
1627 } -result {-2- 2 -1- 1 0}
1628 test oo-10.2 {OO: recursive invoke and modify} -setup {
1629 oo::object create O
1630 } -cleanup {
1631 O destroy
1632 } -body {
1633 oo::objdefine O method foo {} {
1634 oo::objdefine [self] method foo {} {
1635 error "not called"
1636 }
1637 return [format %s%s call ed]
1638 }
1639 O foo
1640 } -result called
1641 test oo-10.3 {OO: invoke and modify} -setup {
1642 oo::class create A {
1643 method a {} {return A.a}
1644 method b {} {return A.b}
1645 method c {} {return A.c}
1646 }
1647 oo::class create B {
1648 superclass A
1649 method a {} {return [next],B.a}
1650 method b {} {return [next],B.b}
1651 method c {} {return [next],B.c}
1652 }
1653 B create C
1654 set result {}
1655 } -cleanup {
1656 A destroy
1657 } -body {
1658 lappend result [C a] [C b] [C c] -
1659 oo::define B deletemethod b
1660 lappend result [C a] [C b] [C c] -
1661 oo::define B renamemethod a b
1662 lappend result [C a] [C b] [C c] -
1663 oo::define B deletemethod b c
1664 lappend result [C a] [C b] [C c]
1665 } -result {A.a,B.a A.b,B.b A.c,B.c - A.a,B.a A.b A.c,B.c - A.a A.b,B.a A.c,B.c - A.a A.b A.c}
1666 test oo-10.4 {OO: invoke and modify} -setup {
1667 oo::class create A {
1668 method a {} {return A.a}
1669 method b {} {return A.b}
1670 method c {} {return A.c}
1671 }
1672 A create B
1673 oo::objdefine B {
1674 method a {} {return [next],B.a}
1675 method b {} {return [next],B.b}
1676 method c {} {return [next],B.c}
1677 }
1678 set result {}
1679 } -cleanup {
1680 A destroy
1681 } -body {
1682 lappend result [B a] [B b] [B c] -
1683 oo::objdefine B deletemethod b
1684 lappend result [B a] [B b] [B c] -
1685 oo::objdefine B renamemethod a b
1686 lappend result [B a] [B b] [B c] -
1687 oo::objdefine B deletemethod b c
1688 lappend result [B a] [B b] [B c]
1689 } -result {A.a,B.a A.b,B.b A.c,B.c - A.a,B.a A.b A.c,B.c - A.a A.b,B.a A.c,B.c - A.a A.b A.c}
1690
1691 test oo-11.1 {OO: cleanup} {
1692 oo::object create foo
1693 set result [list [catch {oo::object create foo} msg] $msg]
1694 lappend result [foo destroy] [oo::object create foo] [foo destroy]
1695 } {1 {can't create object "foo": command already exists with that name} {} ::foo {}}
1696 test oo-11.2 {OO: cleanup} {
1697 oo::class create bar
1698 bar create foo
1699 set result [list [catch {bar create foo} msg] $msg]
1700 lappend result [bar destroy] [oo::object create foo] [foo destroy]
1701 } {1 {can't create object "foo": command already exists with that name} {} ::foo {}}
1702 test oo-11.3 {OO: cleanup} {
1703 oo::class create bar0
1704 oo::class create bar
1705 oo::define bar superclass bar0
1706 bar create foo
1707 set result [list [catch {bar create foo} msg] $msg]
1708 lappend result [bar0 destroy] [oo::object create foo] [foo destroy]
1709 } {1 {can't create object "foo": command already exists with that name} {} ::foo {}}
1710 test oo-11.4 {OO: cleanup} {
1711 oo::class create bar0
1712 oo::class create bar1
1713 oo::define bar1 superclass bar0
1714 oo::class create bar2
1715 oo::define bar2 {
1716 superclass bar0
1717 destructor {lappend ::result destroyed}
1718 }
1719 oo::class create bar
1720 oo::define bar superclass bar1 bar2
1721 bar create foo
1722 set result [list [catch {bar create foo} msg] $msg]
1723 lappend result [bar0 destroy] [oo::object create foo] [foo destroy] \
1724 [oo::object create bar2] [bar2 destroy]
1725 } {1 {can't create object "foo": command already exists with that name} destroyed {} ::foo {} ::bar2 {}}
1726 test oo-11.5 {OO: cleanup} {
1727 oo::class create obj1
1728
1729 trace add command obj1 delete {apply {{name1 name2 action} {
1730 set namespace [info object namespace $name1]
1731 namespace delete $namespace
1732 }}}
1733
1734 rename obj1 {}
1735 # No segmentation fault
1736 return done
1737 } done
1738
1739 test oo-11.6.1 {
1740 OO: cleanup of when an class is mixed into itself
1741 } -constraints memory -body {
1742 leaktest {
1743 interp create interp1
1744 oo::class create obj1
1745 ::oo::define obj1 {self mixin [uplevel 1 {namespace which obj1}]}
1746 rename obj1 {}
1747 interp delete interp1
1748 }
1749 } -result 0
1750 test oo-11.6.2 {
1751 OO: cleanup ReleaseClassContents() where class is mixed into one of its
1752 instances
1753 } -constraints memory -body {
1754 leaktest {
1755 interp create interp1
1756 interp1 eval {
1757 oo::class create obj1
1758 ::oo::copy obj1 obj2
1759 rename obj2 {}
1760 rename obj1 {}
1761 }
1762 interp delete interp1
1763 }
1764 } -result 0
1765 test oo-11.6.3 {
1766 OO: cleanup ReleaseClassContents() where class is mixed into one of its
1767 instances
1768 } -constraints memory -body {
1769 leaktest {
1770 interp create interp1
1771 interp1 eval {
1772 oo::class create obj1
1773 ::oo::define obj1 {self mixin [uplevel 1 {namespace which obj1}]}
1774
1775 ::oo::copy obj1 obj2
1776 rename obj2 {}
1777 rename obj1 {}
1778 }
1779 interp delete interp1
1780 }
1781 } -result 0
1782 test oo-11.6.4 {
1783 OO: cleanup ReleaseClassContents() where class is mixed into one of its
1784 instances
1785 } -body {
1786 oo::class create obj1
1787 ::oo::define obj1 {self mixin [uplevel 1 {namespace which obj1}]}
1788
1789 ::oo::copy obj1 obj2
1790 ::oo::objdefine obj2 {mixin [uplevel 1 {namespace which obj2}]}
1791
1792 ::oo::copy obj2 obj3
1793 rename obj3 {}
1794 rename obj2 {}
1795
1796 # No segmentation fault
1797 return done
1798 } -result done -cleanup {
1799 rename obj1 {}
1800 }
1801
1802 test oo-11.7 {Bug 154f0982f2: createWithNamespace and an existing namespace} -setup {
1803 oo::class create Aclass {
1804 self export createWithNamespace
1805 method ns {} {namespace current}
1806 }
1807 } -body {
1808 namespace eval test_oo117 {variable name [namespace current]}
1809 list [Aclass createWithNamespace aInstance $test_oo117::name] [aInstance ns]
1810 } -returnCodes error -cleanup {
1811 Aclass destroy
1812 catch {namespace delete test_oo117}
1813 } -result {can't create namespace "::test_oo117": already exists}
1814
1815 test oo-12.1 {OO: filters} {
1816 oo::class create Aclass
1817 Aclass create Aobject
1818 oo::define Aclass {
1819 method concatenate args {
1820 global result
1821 lappend result {*}$args
1822 join $args {}
1823 }
1824 method logFilter args {
1825 global result
1826 lappend result "calling [self object]->[self method] $args"
1827 set r [next {*}$args]
1828 lappend result "result=$r"
1829 return $r
1830 }
1831 }
1832 oo::objdefine Aobject filter logFilter
1833 set result {}
1834 lappend result [Aobject concatenate 1 2 3 4 5]
1835 Aclass destroy
1836 return $result
1837 } {{calling ::Aobject->logFilter 1 2 3 4 5} 1 2 3 4 5 result=12345 12345}
1838 test oo-12.2 {OO: filters} -setup {
1839 oo::class create Aclass
1840 Aclass create Aobject
1841 } -body {
1842 oo::define Aclass {
1843 method concatenate args {
1844 global result
1845 lappend result {*}$args
1846 join $args {}
1847 }
1848 method logFilter args {
1849 global result
1850 lappend result "calling [self object]->[self method] $args"
1851 set r [next {*}$args]
1852 lappend result "result=$r"
1853 return $r
1854 }
1855 }
1856 oo::objdefine Aobject filter logFilter
1857 set result {}
1858 lappend result [Aobject concatenate 1 2 3 4 5] [Aobject destroy]
1859 } -cleanup {
1860 Aclass destroy
1861 } -result {{calling ::Aobject->logFilter 1 2 3 4 5} 1 2 3 4 5 result=12345 {calling ::Aobject->logFilter } result= 12345 {}}
1862 test oo-12.3 {OO: filters} -setup {
1863 oo::class create Aclass
1864 Aclass create Aobject
1865 } -body {
1866 oo::define Aclass {
1867 method concatenate args {
1868 global result
1869 lappend result {*}$args
1870 join $args {}
1871 }
1872 method logFilter args {
1873 global result
1874 lappend result "calling [self object]->[self method] $args"
1875 set r [next {*}$args]
1876 lappend result "result=$r"
1877 return $r
1878 }
1879 filter logFilter
1880 }
1881 set result {}
1882 lappend result [Aobject concatenate 1 2 3 4 5] [Aobject destroy]
1883 } -cleanup {
1884 Aclass destroy
1885 } -result {{calling ::Aobject->logFilter 1 2 3 4 5} 1 2 3 4 5 result=12345 {calling ::Aobject->logFilter } result= 12345 {}}
1886 test oo-12.4 {OO: filters} -setup {
1887 oo::class create Aclass
1888 Aclass create Aobject
1889 } -body {
1890 oo::define Aclass {
1891 method foo {} { return foo }
1892 method Bar {} { return 1 }
1893 method boo {} { if {[my Bar]} { next } { error forbidden } }
1894 filter boo
1895 }
1896 Aobject foo
1897 } -cleanup {
1898 Aclass destroy
1899 } -result foo
1900 test oo-12.5 {OO: filters} -setup {
1901 oo::class create Aclass
1902 Aclass create Aobject
1903 } -body {
1904 oo::define Aclass {
1905 method foo {} { return foo }
1906 method Bar {} { return [my Bar2] }
1907 method Bar2 {} { return 1 }
1908 method boo {} { if {[my Bar]} { next } { error forbidden } }
1909 filter boo
1910 }
1911 Aobject foo
1912 } -cleanup {
1913 Aclass destroy
1914 } -result foo
1915 test oo-12.6 {OO: filters} -setup {
1916 oo::class create Aclass
1917 Aclass create Aobject
1918 } -body {
1919 oo::define Aclass {
1920 method foo {} { return foo }
1921 method Bar {} { return [my Bar2] }
1922 method Bar2 {} { return [my Bar3] }
1923 method Bar3 {} { return 1 }
1924 method boo {} { if {[my Bar]} { next } { error forbidden } }
1925 filter boo
1926 }
1927 Aobject foo
1928 } -cleanup {
1929 Aclass destroy
1930 } -result foo
1931 test oo-12.7 {OO: filters} -setup {
1932 oo::class create Aclass
1933 Aclass create Aobject
1934 } -body {
1935 oo::define Aclass {
1936 method outerfoo {} { return [my InnerFoo] }
1937 method InnerFoo {} { return foo }
1938 method Bar {} { return [my Bar2] }
1939 method Bar2 {} { return [my Bar3] }
1940 method Bar3 {} { return 1 }
1941 method boo {} {
1942 lappend ::log [self target]
1943 if {[my Bar]} { next } else { error forbidden }
1944 }
1945 filter boo
1946 }
1947 set log {}
1948 list [Aobject outerfoo] $log
1949 } -cleanup {
1950 Aclass destroy
1951 } -result {foo {{::Aclass outerfoo} {::Aclass InnerFoo}}}
1952 test oo-12.8 {OO: filters and destructors} -setup {
1953 oo::class create Aclass
1954 Aclass create Aobject
1955 set ::log {}
1956 } -body {
1957 oo::define Aclass {
1958 constructor {} {
1959 lappend ::log "in constructor"
1960 }
1961 destructor {
1962 lappend ::log "in destructor"
1963 }
1964 method bar {} {
1965 lappend ::log "in method"
1966 }
1967 method Boo args {
1968 lappend ::log [self target]
1969 next {*}$args
1970 }
1971 filter Boo
1972 }
1973 set obj [Aclass new]
1974 $obj bar
1975 $obj destroy
1976 return $::log
1977 } -cleanup {
1978 Aclass destroy
1979 } -result {{in constructor} {::Aclass bar} {in method} {::oo::object destroy} {in destructor}}
1980
1981 test oo-13.1 {OO: changing an object's class} {
1982 oo::class create Aclass
1983 oo::define Aclass {method bar {} {lappend ::result "in A [self object]"}}
1984 oo::class create Bclass
1985 oo::define Bclass {method bar {} {lappend ::result "in B [self object]"}}
1986 set result [Aclass create foo]
1987 foo bar
1988 oo::objdefine foo class Bclass
1989 foo bar
1990 Aclass destroy
1991 lappend result [info command foo]
1992 Bclass destroy
1993 return $result
1994 } {::foo {in A ::foo} {in B ::foo} foo}
1995 test oo-13.2 {OO: changing an object's class} -body {
1996 oo::object create foo
1997 oo::objdefine foo class oo::class
1998 } -cleanup {
1999 foo destroy
2000 } -result {}
2001 test oo-13.3 {OO: changing an object's class} -body {
2002 oo::class create foo
2003 oo::objdefine foo class oo::object
2004 } -cleanup {
2005 foo destroy
2006 } -result {}
2007 test oo-13.4 {OO: changing an object's class} -body {
2008 oo::class create foo {
2009 method m {} {
2010 set result [list [self class] [info object class [self]]]
2011 oo::objdefine [self] class ::bar
2012 lappend result [self class] [info object class [self]]
2013 }
2014 }
2015 oo::class create bar
2016 [foo new] m
2017 } -cleanup {
2018 foo destroy
2019 bar destroy
2020 } -result {::foo ::foo ::foo ::bar}
2021 test oo-13.5 {OO: changing an object's class: non-class to class} -setup {
2022 oo::object create fooObj
2023 } -body {
2024 oo::objdefine fooObj {
2025 class oo::class
2026 }
2027 oo::define fooObj {
2028 method x {} {expr {1+2+3}}
2029 }
2030 [fooObj new] x
2031 } -cleanup {
2032 fooObj destroy
2033 } -result 6
2034 test oo-13.6 {OO: changing an object's class: class to non-class} -setup {
2035 oo::class create foo
2036 unset -nocomplain ::result
2037 } -body {
2038 set result dangling
2039 oo::define foo {
2040 method x {} {expr {1+2+3}}
2041 }
2042 oo::class create boo {
2043 superclass foo
2044 destructor {set ::result "ok"}
2045 }
2046 boo new
2047 foo create bar
2048 oo::objdefine foo {
2049 class oo::object
2050 }
2051 list $result [catch {bar x} msg] $msg
2052 } -cleanup {
2053 catch {bar destroy}
2054 foo destroy
2055 } -result {ok 1 {invalid command name "bar"}}
2056 test oo-13.7 {OO: changing an object's class} -setup {
2057 oo::class create foo
2058 oo::class create bar
2059 unset -nocomplain result
2060 } -body {
2061 oo::define bar method x {} {return ok}
2062 oo::define foo {
2063 method x {} {expr {1+2+3}}
2064 self mixin foo
2065 }
2066 lappend result [foo x]
2067 oo::objdefine foo class bar
2068 lappend result [foo x]
2069 } -cleanup {
2070 foo destroy
2071 bar destroy
2072 } -result {6 ok}
2073 test oo-13.8 {OO: changing an object's class to itself} -setup {
2074 oo::class create foo
2075 } -body {
2076 oo::define foo {
2077 method x {} {expr {1+2+3}}
2078 }
2079 oo::objdefine foo class foo
2080 } -cleanup {
2081 foo destroy
2082 } -returnCodes error -result {may not change classes into an instance of themselves}
2083 test oo-13.9 {OO: changing an object's class: roots are special} -setup {
2084 set i [interp create]
2085 } -body {
2086 $i eval {
2087 oo::objdefine oo::object {
2088 class oo::class
2089 }
2090 }
2091 } -cleanup {
2092 interp delete $i
2093 } -returnCodes error -result {may not modify the class of the root object class}
2094 test oo-13.10 {OO: changing an object's class: roots are special} -setup {
2095 set i [interp create]
2096 } -body {
2097 $i eval {
2098 oo::objdefine oo::class {
2099 class oo::object
2100 }
2101 }
2102 } -cleanup {
2103 interp delete $i
2104 } -returnCodes error -result {may not modify the class of the class of classes}
2105 test oo-13.11 {OO: changing an object's class in a tricky place} -setup {
2106 oo::class create cls
2107 unset -nocomplain result
2108 } -body {
2109 set result gorp
2110 list [catch {
2111 oo::define cls {
2112 method x {} {return}
2113 self class oo::object
2114 ::set ::result ok
2115 method y {} {return}; # I'm sorry, Dave. I'm afraid I can't do that.
2116 }
2117 } msg] $msg $result
2118 } -cleanup {
2119 cls destroy
2120 } -result {1 {attempt to misuse API} ok}
2121 # todo: changing a class subtype (metaclass) to another class subtype
2122
2123 test oo-14.1 {OO: mixins} {
2124 oo::class create Aclass
2125 oo::define Aclass method bar {} {lappend ::result "[self object] in bar"}
2126 oo::class create Bclass
2127 oo::define Bclass method boo {} {lappend ::result "[self object] in boo"}
2128 oo::objdefine [Aclass create fooTest] mixin Bclass
2129 oo::objdefine [Aclass create fooTest2] mixin Bclass
2130 set result [list [catch {fooTest ?} msg] $msg]
2131 fooTest bar
2132 fooTest boo
2133 fooTest2 bar
2134 fooTest2 boo
2135 oo::objdefine fooTest2 mixin
2136 lappend result [Bclass destroy] [info command fooTest*] [Aclass destroy]
2137 } {1 {unknown method "?": must be bar, boo or destroy} {::fooTest in bar} {::fooTest in boo} {::fooTest2 in bar} {::fooTest2 in boo} {} fooTest2 {}}
2138 test oo-14.2 {OO: mixins} {
2139 oo::class create Aclass {
2140 method bar {} {return "[self object] in bar"}
2141 }
2142 oo::class create Bclass {
2143 method boo {} {return "[self object] in boo"}
2144 }
2145 oo::define Aclass mixin Bclass
2146 Aclass create fooTest
2147 set result [list [catch {fooTest ?} msg] $msg]
2148 lappend result [catch {fooTest bar} msg] $msg
2149 lappend result [catch {fooTest boo} msg] $msg
2150 lappend result [Bclass destroy] [info commands Aclass]
2151 } {1 {unknown method "?": must be bar, boo or destroy} 0 {::fooTest in bar} 0 {::fooTest in boo} {} {}}
2152 test oo-14.3 {OO and mixins and filters - advanced case} -setup {
2153 oo::class create mix
2154 oo::class create c {
2155 mixin mix
2156 }
2157 c create i
2158 } -body {
2159 oo::define mix {
2160 method foo {} {return >>[next]<<}
2161 filter foo
2162 }
2163 oo::objdefine i method bar {} {return foobar}
2164 i bar
2165 } -cleanup {
2166 mix destroy
2167 if {[info object isa object i]} {
2168 error "mixin deletion failed to destroy dependent instance"
2169 }
2170 } -result >>foobar<<
2171 test oo-14.4 {OO: mixin error case} -setup {
2172 oo::class create c
2173 } -body {
2174 oo::define c mixin c
2175 } -returnCodes error -cleanup {
2176 c destroy
2177 } -result {may not mix a class into itself}
2178 test oo-14.5 {OO and mixins and filters - advanced case} -setup {
2179 oo::class create mix
2180 oo::class create c {
2181 mixin mix
2182 }
2183 c create i
2184 } -body {
2185 oo::define mix {
2186 method foo {} {return >>[next]<<}
2187 filter foo
2188 }
2189 oo::objdefine i method bar {} {return foobar}
2190 i bar
2191 } -cleanup {
2192 c destroy
2193 mix destroy
2194 } -result >>foobar<<
2195 test oo-14.6 {OO and mixins of mixins - Bug 1960703} -setup {
2196 oo::class create parent
2197 } -cleanup {
2198 parent destroy
2199 } -body {
2200 oo::class create A {
2201 superclass parent
2202 method egg {} {
2203 return chicken
2204 }
2205 }
2206 oo::class create B {
2207 superclass parent
2208 mixin A
2209 method bar {} {
2210 # mixin from A
2211 my egg
2212 }
2213 }
2214 oo::class create C {
2215 superclass parent
2216 mixin B
2217 method foo {} {
2218 # mixin from B
2219 my bar
2220 }
2221 }
2222 [C new] foo
2223 } -result chicken
2224 test oo-14.7 {OO and filters from mixins of mixins} -setup {
2225 oo::class create parent
2226 } -cleanup {
2227 parent destroy
2228 } -body {
2229 oo::class create A {
2230 superclass parent
2231 method egg {} {
2232 return chicken
2233 }
2234 filter f
2235 method f args {
2236 set m [lindex [self target] 1]
2237 return "($m) [next {*}$args] ($m)"
2238 }
2239 }
2240 oo::class create B {
2241 superclass parent
2242 mixin A
2243 filter f
2244 method bar {} {
2245 # mixin from A
2246 my egg
2247 }
2248 }
2249 oo::class create C {
2250 superclass parent
2251 mixin B
2252 filter f
2253 method foo {} {
2254 # mixin from B
2255 my bar
2256 }
2257 }
2258 [C new] foo
2259 } -result {(foo) (bar) (egg) chicken (egg) (bar) (foo)}
2260 test oo-14.8 {OO: class mixin order - Bug 1998221} -setup {
2261 set ::result {}
2262 oo::class create parent {
2263 method test {} {}
2264 }
2265 } -cleanup {
2266 parent destroy
2267 } -body {
2268 oo::class create mix {
2269 superclass parent
2270 method test {} {lappend ::result mix; next; return $::result}
2271 }
2272 oo::class create cls {
2273 superclass parent
2274 mixin mix
2275 method test {} {lappend ::result cls; next; return $::result}
2276 }
2277 [cls new] test
2278 } -result {mix cls}
2279
2280 test oo-15.1 {OO: object cloning} {
2281 oo::class create Aclass
2282 oo::define Aclass method test {} {lappend ::result [self object]->test}
2283 Aclass create Ainstance
2284 set result {}
2285 Ainstance test
2286 oo::copy Ainstance Binstance
2287 Binstance test
2288 Ainstance test
2289 Ainstance destroy
2290 namespace eval foo {
2291 oo::copy Binstance Cinstance
2292 Cinstance test
2293 }
2294 Aclass destroy
2295 namespace delete foo
2296 lappend result [info commands Binstance]
2297 } {::Ainstance->test ::Binstance->test ::Ainstance->test ::foo::Cinstance->test {}}
2298 test oo-15.2 {OO: object cloning} {
2299 oo::object create foo
2300 oo::objdefine foo {
2301 method m x {lappend ::result [self object] >$x<}
2302 forward f ::lappend ::result fwd
2303 }
2304 set result {}
2305 foo m 1
2306 foo f 2
2307 lappend result [oo::copy foo bar]
2308 foo m 3
2309 foo f 4
2310 bar m 5
2311 bar f 6
2312 lappend result [foo destroy]
2313 bar m 7
2314 bar f 8
2315 lappend result [bar destroy]
2316 } {::foo >1< fwd 2 ::bar ::foo >3< fwd 4 ::bar >5< fwd 6 {} ::bar >7< fwd 8 {}}
2317 catch {foo destroy}
2318 catch {bar destroy}
2319 test oo-15.3 {OO: class cloning} {
2320 oo::class create foo {
2321 method testme {} {lappend ::result [self class]->[self object]}
2322 }
2323 set result {}
2324 foo create baseline
2325 baseline testme
2326 oo::copy foo bar
2327 baseline testme
2328 bar create tester
2329 tester testme
2330 foo destroy
2331 tester testme
2332 bar destroy
2333 return $result
2334 } {::foo->::baseline ::foo->::baseline ::bar->::tester ::bar->::tester}
2335 test oo-15.4 {OO: object cloning - Bug 3474460} -setup {
2336 oo::class create ArbitraryClass
2337 } -body {
2338 ArbitraryClass create foo
2339 oo::objdefine foo variable a b c
2340 oo::copy foo bar
2341 info object variable bar
2342 } -cleanup {
2343 ArbitraryClass destroy
2344 } -result {a b c}
2345 test oo-15.5 {OO: class cloning - Bug 3474460} -setup {
2346 oo::class create ArbitraryClass
2347 } -body {
2348 oo::class create Foo {
2349 superclass ArbitraryClass
2350 variable a b c
2351 }
2352 oo::copy Foo Bar
2353 info class variable Bar
2354 } -cleanup {
2355 ArbitraryClass destroy
2356 } -result {a b c}
2357 test oo-15.6 {OO: object cloning copies namespace contents} -setup {
2358 oo::class create ArbitraryClass {export eval}
2359 } -body {
2360 ArbitraryClass create a
2361 a eval {proc foo x {
2362 variable y
2363 return [string repeat $x [incr y]]
2364 }}
2365 set result [list [a eval {foo 2}] [a eval {foo 3}]]
2366 oo::copy a b
2367 a eval {rename foo bar}
2368 lappend result [b eval {foo 2}] [b eval {foo 3}] [a eval {bar 4}]
2369 } -cleanup {
2370 ArbitraryClass destroy
2371 } -result {2 33 222 3333 444}
2372 test oo-15.7 {OO: classes can be cloned anonymously} -setup {
2373 oo::class create ArbitraryClassA
2374 oo::class create ArbitraryClassB {superclass ArbitraryClassA}
2375 } -body {
2376 info object isa class [oo::copy ArbitraryClassB]
2377 } -cleanup {
2378 ArbitraryClassA destroy
2379 } -result 1
2380 test oo-15.8 {OO: intercept object cloning} -setup {
2381 oo::class create Foo
2382 set result {}
2383 } -body {
2384 oo::define Foo {
2385 constructor {msg} {
2386 variable v $msg
2387 }
2388 method <cloned> {from} {
2389 next $from
2390 lappend ::result cloned $from [self]
2391 }
2392 method check {} {
2393 variable v
2394 lappend ::result check [self] $v
2395 }
2396 }
2397 Foo create foo ok
2398 oo::copy foo bar
2399 foo check
2400 bar check
2401 } -cleanup {
2402 Foo destroy
2403 } -result {cloned ::foo ::bar check ::foo ok check ::bar ok}
2404 test oo-15.9 {ensemble rewriting must not bleed through oo::copy} -setup {
2405 oo::class create Foo
2406 } -body {
2407 oo::define Foo {
2408 method <cloned> {a b} {}
2409 }
2410 interp alias {} Bar {} oo::copy [Foo create foo]
2411 Bar bar
2412 } -returnCodes error -cleanup {
2413 Foo destroy
2414 } -result {wrong # args: should be "::bar <cloned> a b"}
2415 test oo-15.10 {variable binding must not bleed through oo::copy} -setup {
2416 oo::class create FooClass
2417 set result {}
2418 } -body {
2419 set obj1 [FooClass new]
2420 oo::objdefine $obj1 {
2421 variable var
2422 method m {} {
2423 set var foo
2424 }
2425 method get {} {
2426 return $var
2427 }
2428 export eval
2429 }
2430
2431 $obj1 m
2432 lappend result [$obj1 get]
2433 set obj2 [oo::copy $obj1]
2434 $obj2 eval {
2435 set var bar
2436 }
2437 lappend result [$obj2 get]
2438 $obj1 eval {
2439 set var grill
2440 }
2441 lappend result [$obj1 get] [$obj2 get]
2442 } -cleanup {
2443 FooClass destroy
2444 } -result {foo bar grill bar}
2445 test oo-15.11 {OO: object cloning} -returnCodes error -body {
2446 oo::copy
2447 } -result {wrong # args: should be "oo::copy sourceName ?targetName? ?targetNamespace?"}
2448 test oo-15.12 {OO: object cloning with target NS} -setup {
2449 oo::class create Super
2450 oo::class create Cls {superclass Super}
2451 } -body {
2452 namespace eval ::existing {}
2453 oo::copy Cls {} ::existing
2454 } -returnCodes error -cleanup {
2455 Super destroy
2456 catch {namespace delete ::existing}
2457 } -result {::existing refers to an existing namespace}
2458 test oo-15.13.1 {
2459 OO: object cloning with target NS
2460 Valgrind will report a leak if the reference count of the namespace isn't
2461 properly incremented.
2462 } -setup {
2463 oo::class create Cls {}
2464 } -body {
2465 oo::copy Cls Cls2 ::dupens
2466 return done
2467 } -cleanup {
2468 Cls destroy
2469 Cls2 destroy
2470 } -result done
2471 test oo-15.13.2 {OO: object cloning with target NS} -setup {
2472 oo::class create Super
2473 oo::class create Cls {superclass Super}
2474 } -body {
2475 list [namespace exist ::dupens] [oo::copy Cls Cls2 ::dupens] [namespace exist ::dupens]
2476 } -cleanup {
2477 Super destroy
2478 } -result {0 ::Cls2 1}
2479 test oo-15.14 {OO: object cloning with target NS} -setup {
2480 oo::class create Cls {export eval}
2481 set result {}
2482 } -body {
2483 Cls create obj
2484 obj eval {
2485 proc test-15.14 {} {}
2486 }
2487 lappend result [info commands ::dupens::t*]
2488 oo::copy obj obj2 ::dupens
2489 lappend result [info commands ::dupens::t*]
2490 } -cleanup {
2491 Cls destroy
2492 } -result {{} ::dupens::test-15.14}
2493 test oo-15.15 {method cloning must ensure that there is a string representation of bodies} -setup {
2494 oo::class create cls
2495 } -body {
2496 cls create foo
2497 oo::objdefine foo {
2498 method m1 {} [string map {a b} {return hello}]
2499 }
2500 [oo::copy foo] m1
2501 } -cleanup {
2502 cls destroy
2503 } -result hello
2504
2505 test oo-16.1 {OO: object introspection} -body {
2506 info object
2507 } -returnCodes 1 -result "wrong \# args: should be \"info object subcommand ?arg ...?\""
2508 test oo-16.1.1 {OO: object introspection} -body {
2509 catch {info object} m o
2510 dict get $o -errorinfo
2511 } -result "wrong \# args: should be \"info object subcommand ?arg ...?\"
2512 while executing
2513 \"info object\""
2514 test oo-16.2 {OO: object introspection} -body {
2515 info object class NOTANOBJECT
2516 } -returnCodes 1 -result {NOTANOBJECT does not refer to an object}
2517 test oo-16.3 {OO: object introspection} -body {
2518 info object gorp oo::object
2519 } -returnCodes 1 -result {unknown or ambiguous subcommand "gorp": must be call, class, definition, filters, forward, isa, methods, methodtype, mixins, namespace, variables, or vars}
2520 test oo-16.4 {OO: object introspection} -setup {
2521 oo::class create meta { superclass oo::class }
2522 [meta create instance1] create instance2
2523 } -body {
2524 list [list [info object class oo::object] \
2525 [info object class oo::class] \
2526 [info object class meta] \
2527 [info object class instance1] \
2528 [info object class instance2]] \
2529 [list [info object isa class oo::object] \
2530 [info object isa class meta] \
2531 [info object isa class instance1] \
2532 [info object isa class instance2]] \
2533 [list [info object isa metaclass oo::object] \
2534 [info object isa metaclass oo::class] \
2535 [info object isa metaclass meta] \
2536 [info object isa metaclass instance1] \
2537 [info object isa metaclass instance2]] \
2538 [list [info object isa object oo::object] \
2539 [info object isa object oo::class] \
2540 [info object isa object meta] \
2541 [info object isa object instance1] \
2542 [info object isa object instance2] \
2543 [info object isa object oo::define] \
2544 [info object isa object NOTANOBJECT]]
2545 } -cleanup {
2546 meta destroy
2547 } -result {{::oo::class ::oo::class ::oo::class ::meta ::instance1} {1 1 1 0} {0 1 1 0 0} {1 1 1 1 1 0 0}}
2548 test oo-16.5 {OO: object introspection} {info object methods oo::object} {}
2549 test oo-16.6 {OO: object introspection} {
2550 oo::object create foo
2551 set result [list [info object methods foo]]
2552 oo::objdefine foo method bar {} {...}
2553 lappend result [info object methods foo] [foo destroy]
2554 } {{} bar {}}
2555 test oo-16.7 {OO: object introspection} -setup {
2556 oo::object create foo
2557 } -body {
2558 oo::objdefine foo method bar {a {b c} args} {the body}
2559 set result [info object methods foo]
2560 lappend result [info object methodtype foo bar] \
2561 [info object definition foo bar]
2562 } -cleanup {
2563 foo destroy
2564 } -result {bar method {{a {b c} args} {the body}}}
2565 test oo-16.8 {OO: object introspection} {
2566 oo::object create foo
2567 oo::class create bar
2568 oo::objdefine foo mixin bar
2569 set result [list [info object mixins foo] \
2570 [info object isa mixin foo bar] \
2571 [info object isa mixin foo oo::class]]
2572 foo destroy
2573 bar destroy
2574 return $result
2575 } {::bar 1 0}
2576 test oo-16.9 {OO: object introspection} -body {
2577 oo::class create Ac
2578 oo::class create Bc; oo::define Bc superclass Ac
2579 oo::class create Cc; oo::define Cc superclass Bc
2580 oo::class create Dc; oo::define Dc mixin Cc
2581 Cc create E
2582 Dc create F
2583 list [info object isa typeof E oo::class] \
2584 [info object isa typeof E Ac] \
2585 [info object isa typeof F Bc] \
2586 [info object isa typeof F Cc]
2587 } -cleanup {
2588 catch {Ac destroy}
2589 } -result {0 1 1 1}
2590 test oo-16.10 {OO: object introspection} -setup {
2591 oo::object create foo
2592 } -body {
2593 oo::objdefine foo export eval
2594 foo eval {variable c 3 a 1 b 2 ddd 4 e}
2595 lsort [info object vars foo ?]
2596 } -cleanup {
2597 foo destroy
2598 } -result {a b c}
2599 test oo-16.11 {OO: object introspection} -setup {
2600 oo::class create foo
2601 foo create bar
2602 } -body {
2603 oo::define foo method spong {} {...}
2604 oo::objdefine bar method boo {a {b c} args} {the body}
2605 list [lsort [info object methods bar -all]] [lsort [info object methods bar -all -private]]
2606 } -cleanup {
2607 foo destroy
2608 } -result {{boo destroy spong} {<cloned> boo destroy eval spong unknown variable varname}}
2609 test oo-16.12 {OO: object introspection} -setup {
2610 oo::object create foo
2611 } -cleanup {
2612 rename foo {}
2613 } -body {
2614 oo::objdefine foo unexport {*}[info object methods foo -all]
2615 info object methods foo -all
2616 } -result {}
2617 test oo-16.13 {OO: object introspection} -setup {
2618 oo::object create foo
2619 } -cleanup {
2620 rename foo {}
2621 } -body {
2622 oo::objdefine foo method Bar {} {return "ok in foo"}
2623 [info object namespace foo]::my Bar
2624 } -result "ok in foo"
2625 test oo-16.14 {OO: object introspection: TIP #436} -setup {
2626 oo::class create meta { superclass oo::class }
2627 [meta create instance1] create instance2
2628 } -body {
2629 list class [list [info object isa class NOTANOBJECT] \
2630 [info object isa class list]] \
2631 meta [list [info object isa metaclass NOTANOBJECT] \
2632 [info object isa metaclass list] \
2633 [info object isa metaclass oo::object]] \
2634 type [list [info object isa typeof oo::object NOTANOBJECT] \
2635 [info object isa typeof NOTANOBJECT oo::object] \
2636 [info object isa typeof list NOTANOBJECT] \
2637 [info object isa typeof NOTANOBJECT list] \
2638 [info object isa typeof oo::object list] \
2639 [info object isa typeof list oo::object]] \
2640 mix [list [info object isa mixin oo::object NOTANOBJECT] \
2641 [info object isa mixin NOTANOBJECT oo::object] \
2642 [info object isa mixin list NOTANOBJECT] \
2643 [info object isa mixin NOTANOBJECT list] \
2644 [info object isa mixin oo::object list] \
2645 [info object isa mixin list oo::object]]
2646 } -cleanup {
2647 meta destroy
2648 } -result {class {0 0} meta {0 0 0} type {0 0 0 0 0 0} mix {0 0 0 0 0 0}}
2649
2650 test oo-17.1 {OO: class introspection} -body {
2651 info class
2652 } -returnCodes 1 -result "wrong \# args: should be \"info class subcommand ?arg ...?\""
2653 test oo-17.1.1 {OO: class introspection} -body {
2654 catch {info class} m o
2655 dict get $o -errorinfo
2656 } -result "wrong \# args: should be \"info class subcommand ?arg ...?\"
2657 while executing
2658 \"info class\""
2659 test oo-17.2 {OO: class introspection} -body {
2660 info class superclass NOTANOBJECT
2661 } -returnCodes 1 -result {NOTANOBJECT does not refer to an object}
2662 test oo-17.3 {OO: class introspection} -setup {
2663 oo::object create foo
2664 } -body {
2665 info class superclass foo
2666 } -returnCodes 1 -cleanup {
2667 foo destroy
2668 } -result {"foo" is not a class}
2669 test oo-17.4 {OO: class introspection} -body {
2670 info class gorp oo::object
2671 } -returnCodes 1 -result {unknown or ambiguous subcommand "gorp": must be call, constructor, definition, destructor, filters, forward, instances, methods, methodtype, mixins, subclasses, superclasses, or variables}
2672 test oo-17.5 {OO: class introspection} -setup {
2673 oo::class create testClass
2674 } -body {
2675 testClass create foo
2676 testClass create bar
2677 testClass create spong
2678 lsort [info class instances testClass]
2679 } -cleanup {
2680 testClass destroy
2681 } -result {::bar ::foo ::spong}
2682 test oo-17.6 {OO: class introspection} -setup {
2683 oo::class create foo
2684 } -body {
2685 oo::define foo method bar {a {b c} args} {the body}
2686 set result [info class methods foo]
2687 lappend result [info class methodtype foo bar] \
2688 [info class definition foo bar]
2689 } -cleanup {
2690 foo destroy
2691 } -result {bar method {{a {b c} args} {the body}}}
2692 test oo-17.7 {OO: class introspection} {
2693 info class superclasses oo::class
2694 } ::oo::object
2695 test oo-17.8 {OO: class introspection} -setup {
2696 oo::class create testClass
2697 oo::class create superClass1
2698 oo::class create superClass2
2699 } -body {
2700 oo::define testClass superclass superClass1 superClass2
2701 list [info class superclasses testClass] \
2702 [lsort [info class subclass oo::object ::superClass?]]
2703 } -cleanup {
2704 testClass destroy
2705 superClass1 destroy
2706 superClass2 destroy
2707 } -result {{::superClass1 ::superClass2} {::superClass1 ::superClass2}}
2708 test oo-17.9 {OO: class introspection} -setup {
2709 oo::class create foo
2710 oo::class create subfoo {superclass foo}
2711 } -body {
2712 oo::define foo {
2713 method bar {a {b c} args} {the body}
2714 self {
2715 method bad {} {...}
2716 }
2717 }
2718 oo::define subfoo method boo {a {b c} args} {the body}
2719 list [lsort [info class methods subfoo -all]] \
2720 [lsort [info class methods subfoo -all -private]]
2721 } -cleanup {
2722 foo destroy
2723 } -result {{bar boo destroy} {<cloned> bar boo destroy eval unknown variable varname}}
2724 test oo-17.10 {OO: class introspection} -setup {
2725 oo::class create foo
2726 } -cleanup {
2727 rename foo {}
2728 } -body {
2729 oo::define foo unexport {*}[info class methods foo -all]
2730 info class methods foo -all
2731 } -result {}
2732 set stdmethods {<cloned> destroy eval unknown variable varname}
2733 test oo-17.11 {OO: object method unexport (bug 900cb0284bc)} -setup {
2734 oo::object create o
2735 oo::objdefine o unexport m
2736 } -body {
2737 lsort [info object methods o -all -private]
2738 } -cleanup {
2739 o destroy
2740 } -result $stdmethods
2741 test oo-17.12 {OO: instance method unexport (bug 900cb0284bc)} -setup {
2742 oo::class create c
2743 c create o
2744 oo::objdefine o unexport m
2745 } -body {
2746 lsort [info object methods o -all -private]
2747 } -cleanup {
2748 o destroy
2749 c destroy
2750 } -result $stdmethods
2751 test oo-17.13 {OO: class method unexport (bug 900cb0284bc)} -setup {
2752 oo::class create c
2753 oo::define c unexport m
2754 } -body {
2755 lsort [info class methods c -all -private]
2756 } -cleanup {
2757 c destroy
2758 } -result $stdmethods
2759 test oo-17.14 {OO: instance method unexport (bug 900cb0284bc)} -setup {
2760 oo::class create c
2761 oo::define c unexport m
2762 c create o
2763 } -body {
2764 lsort [info object methods o -all -private]
2765 } -cleanup {
2766 o destroy
2767 c destroy
2768 } -result $stdmethods
2769
2770
2771 test oo-18.1 {OO: define command support} {
2772 list [catch {oo::define oo::object {error foo}} msg] $msg $errorInfo
2773 } {1 foo {foo
2774 while executing
2775 "error foo"
2776 (in definition script for class "::oo::object" line 1)
2777 invoked from within
2778 "oo::define oo::object {error foo}"}}
2779 test oo-18.2 {OO: define command support} {
2780 list [catch {oo::define oo::object error foo} msg] $msg $errorInfo
2781 } {1 foo {foo
2782 while executing
2783 "oo::define oo::object error foo"}}
2784 test oo-18.3 {OO: define command support} {
2785 list [catch {oo::class create foo {error bar}} msg] $msg $errorInfo
2786 } {1 bar {bar
2787 while executing
2788 "error bar"
2789 (in definition script for class "::foo" line 1)
2790 invoked from within
2791 "oo::class create foo {error bar}"}}
2792 test oo-18.3a {OO: define command support} {
2793 list [catch {oo::class create foo {
2794 error bar
2795 }} msg] $msg $errorInfo
2796 } {1 bar {bar
2797 while executing
2798 "error bar"
2799 (in definition script for class "::foo" line 2)
2800 invoked from within
2801 "oo::class create foo {
2802 error bar
2803 }"}}
2804 test oo-18.3b {OO: define command support} {
2805 list [catch {oo::class create foo {
2806 eval eval error bar
2807 }} msg] $msg $errorInfo
2808 } {1 bar {bar
2809 while executing
2810 "error bar"
2811 ("eval" body line 1)
2812 invoked from within
2813 "eval error bar"
2814 ("eval" body line 1)
2815 invoked from within
2816 "eval eval error bar"
2817 (in definition script for class "::foo" line 2)
2818 invoked from within
2819 "oo::class create foo {
2820 eval eval error bar
2821 }"}}
2822 test oo-18.4 {OO: more error traces from the guts} -setup {
2823 oo::object create obj
2824 } -body {
2825 oo::objdefine obj method bar {} {my eval {error foo}}
2826 list [catch {obj bar} msg] $msg $errorInfo
2827 } -cleanup {
2828 obj destroy
2829 } -result {1 foo {foo
2830 while executing
2831 "error foo"
2832 (in "my eval" script line 1)
2833 invoked from within
2834 "my eval {error foo}"
2835 (object "::obj" method "bar" line 1)
2836 invoked from within
2837 "obj bar"}}
2838 test oo-18.5 {OO: more error traces from the guts} -setup {
2839 [oo::class create cls] create obj
2840 set errorInfo {}
2841 } -body {
2842 oo::define cls {
2843 method eval script {next $script}
2844 export eval
2845 }
2846 oo::objdefine obj method bar {} {my eval {error foo}}
2847 set result {}
2848 lappend result [catch {obj bar} msg] $msg $errorInfo
2849 lappend result [catch {obj eval {error bar}} msg] $msg $errorInfo
2850 } -cleanup {
2851 cls destroy
2852 } -result {1 foo {foo
2853 while executing
2854 "error foo"
2855 (in "my eval" script line 1)
2856 invoked from within
2857 "next $script"
2858 (class "::cls" method "eval" line 1)
2859 invoked from within
2860 "my eval {error foo}"
2861 (object "::obj" method "bar" line 1)
2862 invoked from within
2863 "obj bar"} 1 bar {bar
2864 while executing
2865 "error bar"
2866 (in "::obj eval" script line 1)
2867 invoked from within
2868 "next $script"
2869 (class "::cls" method "eval" line 1)
2870 invoked from within
2871 "obj eval {error bar}"}}
2872 test oo-18.6 {class construction reference management and errors} -setup {
2873 oo::class create super_abc
2874 } -body {
2875 catch {
2876 oo::class create abc {
2877 superclass super_abc
2878 ::rename abc ::def
2879 ::error foo
2880 }
2881 } msg opt
2882 dict get $opt -errorinfo
2883 } -cleanup {
2884 super_abc destroy
2885 } -result {foo
2886 while executing
2887 "::error foo"
2888 (in definition script for class "::def" line 4)
2889 invoked from within
2890 "oo::class create abc {
2891 superclass super_abc
2892 ::rename abc ::def
2893 ::error foo
2894 }"}
2895 test oo-18.7 {OO: objdefine command support} -setup {
2896 oo::object create ::inst
2897 } -body {
2898 list [catch {oo::objdefine inst {rename ::inst ::INST;error foo}} msg] $msg $errorInfo
2899 } -cleanup {
2900 catch {::inst destroy}
2901 catch {::INST destroy}
2902 } -result {1 foo {foo
2903 while executing
2904 "error foo"
2905 (in definition script for object "::INST" line 1)
2906 invoked from within
2907 "oo::objdefine inst {rename ::inst ::INST;error foo}"}}
2908 test oo-18.8 {OO: define/self command support} -setup {
2909 oo::class create parent
2910 oo::class create ::foo {superclass parent}
2911 } -body {
2912 catch {oo::define foo {rename ::foo ::bar; self {error foobar}}} msg opt
2913 dict get $opt -errorinfo
2914 } -cleanup {
2915 parent destroy
2916 } -result {foobar
2917 while executing
2918 "error foobar"
2919 (in definition script for class object "::bar" line 1)
2920 invoked from within
2921 "self {error foobar}"
2922 (in definition script for class "::bar" line 1)
2923 invoked from within
2924 "oo::define foo {rename ::foo ::bar; self {error foobar}}"}
2925 test oo-18.9 {OO: define/self command support} -setup {
2926 oo::class create parent
2927 set c [oo::class create now_this_is_a_very_very_long_class_name_indeed {
2928 superclass parent
2929 }]
2930 } -body {
2931 catch {oo::define $c {error err}} msg opt
2932 dict get $opt -errorinfo
2933 } -cleanup {
2934 parent destroy
2935 } -result {err
2936 while executing
2937 "error err"
2938 (in definition script for class "::now_this_is_a_very_very_long..." line 1)
2939 invoked from within
2940 "oo::define $c {error err}"}
2941 test oo-18.10 {OO: define/self command support} -setup {
2942 oo::class create parent
2943 oo::class create ::foo {superclass parent}
2944 } -body {
2945 catch {oo::define foo {self {rename ::foo {}; error foobar}}} msg opt
2946 dict get $opt -errorinfo
2947 } -cleanup {
2948 parent destroy
2949 } -result {foobar
2950 while executing
2951 "error foobar"
2952 (in definition script for class object "::foo" line 1)
2953 invoked from within
2954 "self {rename ::foo {}; error foobar}"
2955 (in definition script for class "::foo" line 1)
2956 invoked from within
2957 "oo::define foo {self {rename ::foo {}; error foobar}}"}
2958 test oo-18.11 {OO: define/self command support} -setup {
2959 oo::class create parent
2960 oo::class create ::foo {superclass parent}
2961 } -body {
2962 catch {oo::define foo {rename ::foo {}; self {error foobar}}} msg opt
2963 dict get $opt -errorinfo
2964 } -cleanup {
2965 parent destroy
2966 } -result {this command cannot be called when the object has been deleted
2967 while executing
2968 "self {error foobar}"
2969 (in definition script for class "::foo" line 1)
2970 invoked from within
2971 "oo::define foo {rename ::foo {}; self {error foobar}}"}
2972 test oo-18.12 {OO: self callable via eval method} -setup {
2973 oo::class create parent {
2974 export eval
2975 }
2976 parent create ::foo
2977 } -body {
2978 foo eval { self }
2979 } -cleanup {
2980 parent destroy
2981 } -result ::foo
2982
2983 test oo-19.1 {OO: varname method} -setup {
2984 oo::object create inst
2985 oo::objdefine inst export eval
2986 set result {}
2987 inst eval { variable x }
2988 } -body {
2989 inst eval {trace add variable x write foo}
2990 set ns [inst eval namespace current]
2991 proc foo args {
2992 global ns result
2993 set context [uplevel 1 namespace current]
2994 lappend result $args [expr {
2995 $ns eq $context ? "ok" : [list $ns ne $context]
2996 }] [expr {
2997 "${ns}::x" eq [uplevel 1 my varname x] ? "ok" : [list ${ns}::x ne [uplevel 1 my varname x]]
2998 }]
2999 }
3000 lappend result [inst eval set x 0]
3001 } -cleanup {
3002 inst destroy
3003 rename foo {}
3004 } -result {{x {} write} ok ok 0}
3005 test oo-19.2 {OO: varname method: Bug 2883857} -setup {
3006 oo::class create SpecialClass
3007 oo::objdefine SpecialClass export createWithNamespace
3008 SpecialClass createWithNamespace inst ::oo_test
3009 oo::objdefine inst export varname eval
3010 } -body {
3011 inst eval { variable x; array set x {y z} }
3012 inst varname x(y)
3013 } -cleanup {
3014 SpecialClass destroy
3015 } -result ::oo_test::x(y)
3016 test oo-19.3 {OO: varname method and variable decl: Bug 3603695} -setup {
3017 oo::class create testClass {
3018 variable foo
3019 export varname
3020 constructor {} {
3021 variable foo x
3022 }
3023 method bar {obj} {
3024 my varname foo
3025 $obj varname foo
3026 }
3027 }
3028 } -body {
3029 testClass create A
3030 testClass create B
3031 lsearch [list [A varname foo] [B varname foo]] [B bar A]
3032 } -cleanup {
3033 testClass destroy
3034 } -result 0
3035 test oo-19.4 {OO: varname ghosts [Bug 74b6110204]} -setup {
3036 oo::class create testClass {
3037 export varname
3038 self export createWithNamespace
3039 }
3040 set obj [testClass createWithNamespace testoo19_4 testoo19_4]
3041 set ns [info object namespace $obj]
3042 } -body {
3043 set v [$obj varname foo]
3044 list [namespace which -variable $v] \
3045 [info exists $v] [namespace which -variable $v]
3046 } -cleanup {
3047 testClass destroy
3048 } -result {::testoo19_4::foo 0 ::testoo19_4::foo}
3049 test oo-19.5 {OO: varname array elements [Bug 2da1cb0c80]} -setup {
3050 set obj [oo::object new]
3051 oo::objdefine $obj export eval varname
3052 } -constraints knownBug -body {
3053 $obj eval {
3054 namespace upvar :: tcl_platform(platform) foo
3055 }
3056 $obj varname foo
3057 } -cleanup {
3058 $obj destroy
3059 } -result ::tcl_platform(platform)
3060 test oo-19.5.1 {OO: varname array elements before Tcl 9 [Bug 2da1cb0c80]} -setup {
3061 oo::class create testClass {
3062 self export createWithNamespace
3063 export eval varname
3064 }
3065 } -body {
3066 set obj [testClass createWithNamespace obj oo-19.5.1]
3067 $obj eval {
3068 namespace upvar :: tcl_platform(platform) foo
3069 }
3070 $obj varname foo
3071 } -cleanup {
3072 testClass destroy
3073 } -result ::oo-19.5.1::foo
3074
3075 test oo-20.1 {OO: variable method} -body {
3076 oo::class create testClass {
3077 constructor {} {
3078 my variable ok
3079 set ok {}
3080 }
3081 }
3082 lsort [info object vars [testClass new]]
3083 } -cleanup {
3084 catch {testClass destroy}
3085 } -result ok
3086 test oo-20.2 {OO: variable method} -body {
3087 oo::class create testClass {
3088 constructor {} {
3089 my variable a b c
3090 set a [set b [set c {}]]
3091 }
3092 }
3093 lsort [info object vars [testClass new]]
3094 } -cleanup {
3095 catch {testClass destroy}
3096 } -result {a b c}
3097 test oo-20.3 {OO: variable method} -body {
3098 oo::class create testClass {
3099 export varname
3100 method bar {} {
3101 my variable a(b)
3102 }
3103 }
3104 testClass create foo
3105 array set [foo varname a] {b c}
3106 foo bar
3107 } -returnCodes 1 -cleanup {
3108 catch {testClass destroy}
3109 } -result {can't define "a(b)": name refers to an element in an array}
3110 test oo-20.4 {OO: variable method} -body {
3111 oo::class create testClass {
3112 export varname
3113 method bar {} {
3114 my variable a(b)
3115 }
3116 }
3117 testClass create foo
3118 set [foo varname a] b
3119 foo bar
3120 } -returnCodes 1 -cleanup {
3121 catch {testClass destroy}
3122 } -result {can't define "a(b)": name refers to an element in an array}
3123 test oo-20.5 {OO: variable method} -body {
3124 oo::class create testClass {
3125 method bar {} {
3126 my variable a::b
3127 }
3128 }
3129 testClass create foo
3130 foo bar
3131 } -returnCodes 1 -cleanup {
3132 catch {testClass destroy}
3133 } -result {variable name "a::b" illegal: must not contain namespace separator}
3134 test oo-20.6 {OO: variable method} -setup {
3135 oo::class create testClass {
3136 export varname
3137 self export eval
3138 }
3139 } -body {
3140 testClass eval variable a 0
3141 oo::objdefine [testClass create foo] method bar {other} {
3142 $other variable a
3143 set a 3
3144 }
3145 oo::objdefine [testClass create boo] export variable
3146 set [foo varname a] 1
3147 set [boo varname a] 2
3148 foo bar boo
3149 list [testClass eval set a] [set [foo varname a]] [set [boo varname a]]
3150 } -cleanup {
3151 testClass destroy
3152 } -result {0 1 3}
3153 test oo-20.7 {OO: variable method} -setup {
3154 oo::class create cls
3155 } -body {
3156 oo::define cls {
3157 method a {} {
3158 my variable d b
3159 lappend b $d
3160 }
3161 method e {} {
3162 my variable b d
3163 return [list $b $d]
3164 }
3165 method f {x y} {
3166 my variable b d
3167 set b $x
3168 set d $y
3169 }
3170 }
3171 cls create obj
3172 obj f p q
3173 obj a
3174 obj a
3175 obj e
3176 } -cleanup {
3177 cls destroy
3178 } -result {{p q q} q}
3179 # oo-20.8 tested explicitly for functionality removed due to [Bug 1959457]
3180 test oo-20.9 {OO: variable method} -setup {
3181 oo::object create obj
3182 } -body {
3183 oo::objdefine obj {
3184 method a {} {
3185 my variable ::b
3186 }
3187 }
3188 obj a
3189 } -returnCodes 1 -cleanup {
3190 obj destroy
3191 } -result {variable name "::b" illegal: must not contain namespace separator}
3192 test oo-20.10 {OO: variable and varname methods refer to same things} -setup {
3193 oo::object create obj
3194 } -body {
3195 oo::objdefine obj {
3196 method a {} {
3197 my variable b
3198 set b [self]
3199 return [my varname b]
3200 }
3201 }
3202 list [set [obj a]] [namespace tail [obj a]]
3203 } -cleanup {
3204 obj destroy
3205 } -result {::obj b}
3206 test oo-20.11 {OO: variable mustn't crash when recursing} -body {
3207 oo::class create A {
3208 constructor {name} {
3209 my variable np_name
3210 set np_name $name
3211 }
3212 method copy {nm} {
3213 set cpy [[info object class [self]] new $nm]
3214 foreach var [info object vars [self]] {
3215 my variable $var
3216 set val [set $var]
3217 if {[string match o_* $var]} {
3218 set objs {}
3219 foreach ref $val {
3220 # call to "copy" crashes
3221 lappend objs [$ref copy {}]
3222 }
3223 $cpy prop $var $objs
3224 } else {
3225 $cpy prop $var $val
3226 }
3227 }
3228 return $cpy
3229 }
3230 method prop {name val} {
3231 my variable $name
3232 set $name $val
3233 }
3234 }
3235 set o1 [A new {}]
3236 set o2 [A new {}]
3237 $o1 prop o_object $o2
3238 $o1 copy aa
3239 } -cleanup {
3240 catch {A destroy}
3241 } -match glob -result *
3242 test oo-20.12 {OO: variable method accept zero args (TIP 323)} -setup {
3243 oo::object create foo
3244 } -cleanup {
3245 foo destroy
3246 } -body {
3247 oo::objdefine foo method demo {} {
3248 my variable
3249 }
3250 foo demo
3251 } -result {}
3252 test oo-20.13 {OO: variable method use in non-methods [Bug 2903811]} -setup {
3253 oo::object create fooObj
3254 oo::objdefine fooObj export variable
3255 } -cleanup {
3256 fooObj destroy
3257 } -body {
3258 apply {{} {fooObj variable x; set x ok; return}}
3259 apply {{} {fooObj variable x; return $x}}
3260 } -result ok
3261 test oo-20.14 {OO: variable method use in non-methods [Bug 2903811]} -setup {
3262 oo::object create fooObj
3263 oo::objdefine fooObj export variable
3264 namespace eval ns1 {}
3265 namespace eval ns2 {}
3266 set x bad
3267 } -cleanup {
3268 fooObj destroy
3269 namespace delete ns1 ns2
3270 unset x
3271 } -body {
3272 namespace eval ns1 {fooObj variable x; set x ok; subst ""}
3273 set x bad
3274 namespace eval ns2 {fooObj variable x; return $x}
3275 } -result ok
3276 test oo-20.15 {OO: variable method use in non-methods [Bug 2903811]} -setup {
3277 oo::object create fooObj
3278 oo::objdefine fooObj export variable varname
3279 } -cleanup {
3280 fooObj destroy
3281 } -body {
3282 apply {{} {fooObj variable x; set x ok; return}}
3283 return [set [fooObj varname x]]
3284 } -result ok
3285 test oo-20.16 {variable method: leak per instance} -setup {
3286 oo::class create foo
3287 } -constraints memory -body {
3288 oo::define foo {
3289 constructor {} {
3290 set [my variable v] 0
3291 }
3292 }
3293 leaktest {[foo new] destroy}
3294 } -cleanup {
3295 foo destroy
3296 } -result 0
3297
3298 test oo-21.1 {OO: inheritance ordering} -setup {
3299 oo::class create A
3300 } -body {
3301 oo::define A method m {} {lappend ::result A}
3302 oo::class create B {
3303 superclass A
3304 method m {} {lappend ::result B;next}
3305 }
3306 oo::class create C {
3307 superclass A
3308 method m {} {lappend ::result C;next}
3309 }
3310 oo::class create D {
3311 superclass B C
3312 method m {} {lappend ::result D;next}
3313 }
3314 D create o
3315 oo::objdefine o method m {} {lappend ::result o;next}
3316 set result {}
3317 o m
3318 return $result
3319 } -cleanup {
3320 A destroy
3321 } -result {o D B C A}
3322 test oo-21.2 {OO: inheritance ordering} -setup {
3323 oo::class create A
3324 } -body {
3325 oo::define A method m {} {lappend ::result A}
3326 oo::class create B {
3327 superclass A
3328 method m {} {lappend ::result B;next}
3329 }
3330 oo::class create C {
3331 superclass A
3332 method m {} {lappend ::result C;next}
3333 }
3334 oo::class create D {
3335 superclass B C
3336 method m {} {lappend ::result D;next}
3337 }
3338 oo::class create Emix {
3339 superclass C
3340 method m {} {lappend ::result Emix;next}
3341 }
3342 oo::class create Fmix {
3343 superclass Emix
3344 method m {} {lappend ::result Fmix;next}
3345 }
3346 D create o
3347 oo::objdefine o {
3348 method m {} {lappend ::result o;next}
3349 mixin Fmix
3350 }
3351 set result {}
3352 o m
3353 return $result
3354 } -cleanup {
3355 A destroy
3356 } -result {Fmix Emix o D B C A}
3357 test oo-21.3 {OO: inheritance ordering} -setup {
3358 oo::class create A
3359 } -body {
3360 oo::define A method m {} {lappend ::result A}
3361 oo::class create B {
3362 superclass A
3363 method m {} {lappend ::result B;next}
3364 method f {} {lappend ::result B-filt;next}
3365 }
3366 oo::class create C {
3367 superclass A
3368 method m {} {lappend ::result C;next}
3369 }
3370 oo::class create D {
3371 superclass B C
3372 method m {} {lappend ::result D;next}
3373 }
3374 oo::class create Emix {
3375 superclass C
3376 method m {} {lappend ::result Emix;next}
3377 method f {} {lappend ::result Emix-filt;next}
3378 }
3379 oo::class create Fmix {
3380 superclass Emix
3381 method m {} {lappend ::result Fmix;next}
3382 }
3383 D create o
3384 oo::objdefine o {
3385 method m {} {lappend ::result o;next}
3386 mixin Fmix
3387 filter f
3388 }
3389 set result {}
3390 o m
3391 return $result
3392 } -cleanup {
3393 A destroy
3394 } -result {Emix-filt B-filt Fmix Emix o D B C A}
3395 test oo-21.4 {OO: inheritance ordering} -setup {
3396 oo::class create A
3397 } -body {
3398 oo::define A method m {} {lappend ::result A}
3399 oo::class create B {
3400 superclass A
3401 method m {} {lappend ::result B;next}
3402 method f {} {lappend ::result B-filt;next}
3403 method g {} {lappend ::result B-cfilt;next}
3404 }
3405 oo::class create C {
3406 superclass A
3407 method m {} {lappend ::result C;next}
3408 }
3409 oo::class create D {
3410 superclass B C
3411 method m {} {lappend ::result D;next}
3412 method g {} {lappend ::result D-cfilt;next}
3413 filter g
3414 }
3415 oo::class create Emix {
3416 superclass C
3417 method m {} {lappend ::result Emix;next}
3418 method f {} {lappend ::result Emix-filt;next}
3419 }
3420 oo::class create Fmix {
3421 superclass Emix
3422 method m {} {lappend ::result Fmix;next}
3423 }
3424 D create o
3425 oo::objdefine o {
3426 method m {} {lappend ::result o;next}
3427 mixin Fmix
3428 filter f
3429 }
3430 set result {}
3431 o m
3432 return $result
3433 } -cleanup {
3434 A destroy
3435 } -result {Emix-filt B-filt D-cfilt B-cfilt Fmix Emix o D B C A}
3436
3437 test oo-22.1 {OO and info frame} -setup {
3438 oo::class create c
3439 c create i
3440 } -match glob -body {
3441 oo::define c self method frame {} {
3442 info frame 0
3443 }
3444 oo::define c {
3445 method frames {} {
3446 info frame 0
3447 }
3448 method level {} {
3449 info frame
3450 }
3451 }
3452 oo::objdefine i {
3453 method frames {} {
3454 list [next] [info frame 0]
3455 }
3456 method level {} {
3457 expr {[next] - [info frame]}
3458 }
3459 }
3460 list [i level] [i frames] [dict get [c frame] object]
3461 } -cleanup {
3462 c destroy
3463 } -result {1 {{* cmd {info frame 0} method frames class ::c level 0} {* cmd {info frame 0} method frames object ::i level 0}} ::c}
3464 test oo-22.2 {OO and info frame: Bug 3001438} -setup {
3465 oo::class create c
3466 } -body {
3467 oo::define c method test {{x 1}} {
3468 if {$x} {my test 0}
3469 lsort {q w e r t y u i o p}; # Overwrite the Tcl stack
3470 info frame 0
3471 }
3472 [c new] test
3473 } -match glob -cleanup {
3474 c destroy
3475 } -result {* cmd {info frame 0} method test class ::c level 0}
3476 # Common code for oo-22.{3,4,5,6}
3477 oo::class create WorkerBase
3478 oo::class create WorkerSupport {
3479 superclass oo::class WorkerBase
3480 variable result stop
3481 method WithWorkers {nworkers args script} {
3482 set workers {}
3483 try {
3484 for {set n 1} {$n <= $nworkers} {incr n} {
3485 lappend workers [set worker [[self] new]]
3486 $worker schedule {*}$args
3487 }
3488 return [uplevel 1 $script]
3489 } finally {
3490 foreach worker $workers {$worker destroy}
3491 }
3492 }
3493 method run {nworkers} {
3494 set result {}
3495 set stopvar [my varname stop]
3496 set stop false
3497 my WithWorkers $nworkers [list my Work [my varname result]] {
3498 after idle [namespace code {set stop true}]
3499 vwait $stopvar
3500 }
3501 return $result
3502 }
3503 }
3504 oo::class create Worker {
3505 superclass WorkerBase
3506 method schedule {args} {
3507 set coro [namespace current]::coro
3508 if {![llength [info commands $coro]]} {
3509 coroutine $coro {*}$args
3510 }
3511 }
3512 method Work args {error unimplemented}
3513 method dump {} {
3514 info frame [expr {[info frame] - 1}]
3515 }
3516 }
3517 test oo-22.3 {OO and coroutines and info frame: Bug 87271f7cd6} -body {
3518 # Triggers a crash with incorrectly restored pmPtr->procPtr->cmdPtr
3519 WorkerSupport create A {
3520 superclass Worker
3521 method Work {var} {
3522 after 0 [info coroutine]
3523 yield
3524 lappend $var [my dump]
3525 }
3526 }
3527 A run 2
3528 } -cleanup {
3529 catch {rename dump {}}
3530 catch {A destroy}
3531 } -match glob -result {{* method Work class ::A *} {* method Work class ::A *}}
3532 test oo-22.4 {OO and coroutines and info frame: Bug 87271f7cd6} -body {
3533 # Triggers a crash with incorrectly cloned pmPtr->procPtr->cmdPtr
3534 WorkerSupport create A {
3535 superclass Worker
3536 method Work {var} {
3537 after 0 [info coroutine]
3538 yield
3539 lappend $var [my dump]
3540 }
3541 }
3542 # Copies the methods, changing the declarer
3543 # Test it works with the source class still around
3544 oo::copy A B
3545 B run 2
3546 } -cleanup {
3547 catch {rename dump {}}
3548 catch {A destroy}
3549 catch {B destroy}
3550 } -match glob -result {{* method Work class ::B *} {* method Work class ::B *}}
3551 test oo-22.5 {OO and coroutines and info frame: Bug 87271f7cd6} -body {
3552 # Triggers a crash with incorrectly cloned pmPtr->procPtr->cmdPtr
3553 WorkerSupport create A {
3554 superclass Worker
3555 method Work {var} {
3556 after 0 [info coroutine]
3557 yield
3558 lappend $var [my dump]
3559 }
3560 }
3561 # Copies the methods, changing the declarer
3562 # Test it works with the source class deleted
3563 oo::copy A B
3564 catch {A destroy}
3565 B run 2
3566 } -cleanup {
3567 catch {rename dump {}}
3568 catch {B destroy}
3569 } -match glob -result {{* method Work class ::B *} {* method Work class ::B *}}
3570 test oo-22.6 {OO and coroutines and info frame: Bug 87271f7cd6} -body {
3571 # Triggers a crash with incorrectly cloned pmPtr->procPtr->cmdPtr
3572 WorkerSupport create A {
3573 superclass Worker
3574 method Work {var} {
3575 after 0 [info coroutine]
3576 yield
3577 lappend $var [my dump]
3578 }
3579 }
3580 # Copies the methods, changing the declarer
3581 # Test it works in the original source class with the copy around
3582 oo::copy A B
3583 B run 2
3584 A run 2
3585 } -cleanup {
3586 catch {rename dump {}}
3587 catch {A destroy}
3588 catch {B destroy}
3589 } -match glob -result {{* method Work class ::A *} {* method Work class ::A *}}
3590 WorkerBase destroy
3591
3592 # Prove that the issue in [Bug 1865054] isn't an issue any more
3593 test oo-23.1 {Self-like derivation; complex case!} -setup {
3594 oo::class create SELF {
3595 superclass oo::class
3596 unexport create new
3597 # Next is just a convenience
3598 method method args {oo::define [self] method {*}$args}
3599 method derive {name} {
3600 set o [my new [list superclass [self]]]
3601 oo::objdefine $o mixin $o
3602 uplevel 1 [list rename $o $name]\;[list namespace which $name]
3603 }
3604 self mixin SELF
3605 }
3606 set result {}
3607 } -body {
3608 [SELF derive foo1] method bar1 {} {return 1}
3609 lappend result [foo1 bar1]
3610 [foo1 derive foo2] method bar2 {} {return [my bar1],2}
3611 lappend result [foo2 bar2]
3612 [foo2 derive foo3] method bar3 {} {return [my bar2],3}
3613 lappend result [foo3 bar3]
3614 [foo3 derive foo4] method bar4 {} {return [my bar3],4}
3615 lappend result [foo4 bar4]
3616 foo2 method bar2 {} {return [my bar1],x}
3617 lappend result [foo4 bar4]
3618 } -cleanup {
3619 SELF destroy
3620 } -result {1 1,2 1,2,3 1,2,3,4 1,x,3,4}
3621
3622 test oo-24.1 {unknown method method - Bug 1965063} -setup {
3623 oo::class create cls
3624 } -cleanup {
3625 cls destroy
3626 } -returnCodes error -body {
3627 oo::define cls {
3628 method dummy {} {}
3629 method unknown args {next {*}$args}
3630 }
3631 [cls new] foo bar
3632 } -result {unknown method "foo": must be destroy, dummy or unknown}
3633 test oo-24.2 {unknown method method - Bug 1965063} -setup {
3634 oo::class create cls
3635 } -cleanup {
3636 cls destroy
3637 } -returnCodes error -body {
3638 oo::define cls {
3639 method dummy {} {}
3640 method unknown args {next {*}$args}
3641 }
3642 cls create obj
3643 oo::objdefine obj {
3644 method dummy2 {} {}
3645 method unknown args {next {*}$args}
3646 }
3647 obj foo bar
3648 } -result {unknown method "foo": must be destroy, dummy, dummy2 or unknown}
3649 test oo-24.3 {unknown method method - absent method name} -setup {
3650 set o [oo::object new]
3651 } -cleanup {
3652 $o destroy
3653 } -body {
3654 oo::objdefine $o method unknown args {
3655 return "unknown: >>$args<<"
3656 }
3657 list [$o] [$o foobar] [$o foo bar]
3658 } -result {{unknown: >><<} {unknown: >>foobar<<} {unknown: >>foo bar<<}}
3659
3660 # Probably need a better set of tests, but this is quite difficult to devise
3661 test oo-25.1 {call chain caching} -setup {
3662 oo::class create cls {
3663 method ab {} {return ok}
3664 }
3665 set result {}
3666 } -cleanup {
3667 cls destroy
3668 } -body {
3669 cls create foo
3670 cls create bar
3671 set m1 ab
3672 set m2 a; append m2 b ;# different object!
3673 lappend result [foo $m1] [foo $m1] [bar $m1] [foo $m1]
3674 lappend result [foo $m2] [bar $m2]
3675 oo::objdefine foo method ab {} {return good}
3676 lappend result [foo $m1] [bar $m2]
3677 } -result {ok ok ok ok ok ok good ok}
3678 test oo-25.2 {call chain caching - Bug #2120903} -setup {
3679 set c [oo::class create MyClass]
3680 set o [$c new]
3681 } -body {
3682 oo::define MyClass {
3683 method name {} {return ok}
3684 method isa o {MyClass name $o}
3685 self method name o {$o name}
3686 }
3687 list [$o name] [$c name $o] [$o isa $o]
3688 } -cleanup {
3689 $c destroy
3690 } -result {ok ok ok}
3691
3692 test oo-26.1 {Bug 2037727} -setup {
3693 proc succeed args {}
3694 oo::object create example
3695 } -body {
3696 oo::objdefine example method foo {} {succeed}
3697 example foo
3698 proc succeed {} {return succeed}
3699 example foo
3700 } -cleanup {
3701 example destroy
3702 rename succeed {}
3703 } -result succeed
3704 test oo-26.2 {Bug 2037727} -setup {
3705 oo::class create example {
3706 method localProc {args body} {proc called $args $body}
3707 method run {} { called }
3708 }
3709 example create i1
3710 example create i2
3711 } -body {
3712 i1 localProc args {}
3713 i2 localProc args {return nonempty}
3714 list [i1 run] [i2 run]
3715 } -cleanup {
3716 example destroy
3717 } -result {{} nonempty}
3718 test oo-26.3 {Bug 2037727} -setup {
3719 oo::class create example {
3720 method subProc {args body} {
3721 namespace eval subns [list proc called $args $body]
3722 }
3723 method run {} { subns::called }
3724 }
3725 example create i1
3726 example create i2
3727 } -body {
3728 i1 subProc args {}
3729 i2 subProc args {return nonempty}
3730 list [i1 run] [i2 run]
3731 } -cleanup {
3732 example destroy
3733 } -result {{} nonempty}
3734
3735 test oo-27.1 {variables declaration - class introspection} -setup {
3736 oo::class create foo
3737 } -cleanup {
3738 foo destroy
3739 } -body {
3740 oo::define foo variable a b c
3741 info class variables foo
3742 } -result {a b c}
3743 test oo-27.2 {variables declaration - object introspection} -setup {
3744 oo::object create foo
3745 } -cleanup {
3746 foo destroy
3747 } -body {
3748 oo::objdefine foo variable a b c
3749 info object variables foo
3750 } -result {a b c}
3751 test oo-27.3 {variables declaration - basic behaviour} -setup {
3752 oo::class create parent
3753 } -cleanup {
3754 parent destroy
3755 } -body {
3756 oo::class create foo {
3757 superclass parent
3758 variable x!
3759 constructor {} {set x! 1}
3760 method y {} {incr x!}
3761 }
3762 foo create bar
3763 bar y
3764 bar y
3765 } -result 3
3766 test oo-27.4 {variables declaration - destructors too} -setup {
3767 oo::class create parent
3768 set result bad!
3769 } -cleanup {
3770 parent destroy
3771 } -body {
3772 oo::class create foo {
3773 superclass parent
3774 variable x!
3775 constructor {} {set x! 1}
3776 method y {} {incr x!}
3777 destructor {set ::result ${x!}}
3778 }
3779 foo create bar
3780 bar y
3781 bar y
3782 bar destroy
3783 return $result
3784 } -result 3
3785 test oo-27.5 {variables declaration - object-bound variables} -setup {
3786 oo::object create foo
3787 } -cleanup {
3788 foo destroy
3789 } -body {
3790 oo::objdefine foo {
3791 variable x!
3792 method y {} {incr x!}
3793 }
3794 foo y
3795 foo y
3796 } -result 2
3797 test oo-27.6 {variables declaration - non-interference of levels} -setup {
3798 oo::class create parent
3799 } -cleanup {
3800 parent destroy
3801 } -body {
3802 oo::class create foo {
3803 superclass parent
3804 variable x!
3805 constructor {} {set x! 1}
3806 method y {} {incr x!}
3807 }
3808 foo create bar
3809 oo::objdefine bar {
3810 variable y!
3811 method y {} {list [next] [incr y!] [info var] [info locals]}
3812 export eval
3813 }
3814 bar y
3815 list [bar y] [lsort [info object vars bar]] [bar eval {info vars *!}]
3816 } -result {{3 2 y! {}} {x! y!} {x! y!}}
3817 test oo-27.7 {variables declaration - one underlying variable space} -setup {
3818 oo::class create parent
3819 } -cleanup {
3820 parent destroy
3821 } -body {
3822 oo::class create foo {
3823 superclass parent
3824 variable x!
3825 constructor {} {set x! 1}
3826 method y {} {incr x!}
3827 }
3828 oo::class create foo2 {
3829 superclass foo
3830 variable y!
3831 constructor {} {set y! 42; next}
3832 method x {} {incr y! -1}
3833 }
3834 foo2 create bar
3835 oo::objdefine bar {
3836 variable x! y!
3837 method z {} {list ${x!} ${y!}}
3838 }
3839 bar y
3840 bar x
3841 list [bar y] [bar x] [bar z]
3842 } -result {3 40 {3 40}}
3843 test oo-27.8 {variables declaration - error cases - ns separators} -body {
3844 oo::define oo::object variable bad::var
3845 } -returnCodes error -result {invalid declared variable name "bad::var": must not contain namespace separators}
3846 test oo-27.9 {variables declaration - error cases - arrays} -body {
3847 oo::define oo::object variable bad(var)
3848 } -returnCodes error -result {invalid declared variable name "bad(var)": must not refer to an array element}
3849 test oo-27.10 {variables declaration - no instance var leaks with class resolvers} -setup {
3850 oo::class create parent
3851 } -cleanup {
3852 parent destroy
3853 } -body {
3854 oo::class create foo {
3855 superclass parent
3856 variable clsvar
3857 constructor {} {
3858 set clsvar 0
3859 }
3860 method step {} {
3861 incr clsvar
3862 return
3863 }
3864 method value {} {
3865 return $clsvar
3866 }
3867 }
3868 foo create inst1
3869 inst1 step
3870 foo create inst2
3871 inst2 step
3872 inst1 step
3873 inst2 step
3874 inst1 step
3875 list [inst1 value] [inst2 value]
3876 } -result {3 2}
3877 test oo-27.11 {variables declaration - no instance var leaks with class resolvers} -setup {
3878 oo::class create parent
3879 } -cleanup {
3880 parent destroy
3881 } -body {
3882 oo::class create foo {
3883 superclass parent
3884 variable clsvar
3885 constructor {} {
3886 set clsvar 0
3887 }
3888 method step {} {
3889 incr clsvar
3890 return
3891 }
3892 method value {} {
3893 return $clsvar
3894 }
3895 }
3896 foo create inst1
3897 oo::objdefine inst1 {
3898 variable clsvar
3899 method reinit {} {
3900 set clsvar 0
3901 }
3902 }
3903 foo create inst2
3904 oo::objdefine inst2 {
3905 variable clsvar
3906 method reinit {} {
3907 set clsvar 0
3908 }
3909 }
3910 inst1 step
3911 inst2 step
3912 inst1 reinit
3913 inst2 reinit
3914 inst1 step
3915 inst2 step
3916 inst1 step
3917 inst2 step
3918 inst1 step
3919 list [inst1 value] [inst2 value]
3920 } -result {3 2}
3921 test oo-27.12 {variables declaration: leak per instance} -setup {
3922 oo::class create foo
3923 } -constraints memory -body {
3924 oo::define foo {
3925 variable v
3926 constructor {} {
3927 set v 0
3928 }
3929 }
3930 leaktest {[foo new] destroy}
3931 } -cleanup {
3932 foo destroy
3933 } -result 0
3934 # This test will actually (normally) crash if it fails!
3935 test oo-27.13 {variables declaration: Bug 3185009: require refcount management} -setup {
3936 oo::object create foo
3937 } -body {
3938 oo::objdefine foo {
3939 variable x
3940 method set v {set x $v}
3941 method unset {} {unset x}
3942 method exists {} {info exists x}
3943 method get {} {return $x}
3944 }
3945 list [foo exists] [foo set 7] [foo exists] [foo get] [foo unset] \
3946 [foo exists] [catch {foo get} msg] $msg
3947 } -cleanup {
3948 foo destroy
3949 } -result {0 7 1 7 {} 0 1 {can't read "x": no such variable}}
3950 test oo-27.14 {variables declaration - multiple use} -setup {
3951 oo::class create parent
3952 } -cleanup {
3953 parent destroy
3954 } -body {
3955 oo::class create foo {
3956 superclass parent
3957 variable x
3958 variable y
3959 method boo {} {
3960 return [incr x],[incr y]
3961 }
3962 }
3963 foo create bar
3964 list [bar boo] [bar boo]
3965 } -result {1,1 2,2}
3966 test oo-27.15 {variables declaration - multiple use} -setup {
3967 oo::class create parent
3968 } -cleanup {
3969 parent destroy
3970 } -body {
3971 oo::class create foo {
3972 superclass parent
3973 variable
3974 variable x y
3975 method boo {} {
3976 return [incr x],[incr y]
3977 }
3978 }
3979 foo create bar
3980 list [bar boo] [bar boo]
3981 } -result {1,1 2,2}
3982 test oo-27.16 {variables declaration - multiple use} -setup {
3983 oo::class create parent
3984 } -cleanup {
3985 parent destroy
3986 } -body {
3987 oo::class create foo {
3988 superclass parent
3989 variable x
3990 variable -clear
3991 variable y
3992 method boo {} {
3993 return [incr x],[incr y]
3994 }
3995 }
3996 foo create bar
3997 list [bar boo] [bar boo]
3998 } -result {1,1 1,2}
3999 test oo-27.17 {variables declaration - multiple use} -setup {
4000 oo::class create parent
4001 } -cleanup {
4002 parent destroy
4003 } -body {
4004 oo::class create foo {
4005 superclass parent
4006 variable x
4007 variable -set y
4008 method boo {} {
4009 return [incr x],[incr y]
4010 }
4011 }
4012 foo create bar
4013 list [bar boo] [bar boo]
4014 } -result {1,1 1,2}
4015 test oo-27.18 {variables declaration - multiple use} -setup {
4016 oo::class create parent
4017 } -cleanup {
4018 parent destroy
4019 } -body {
4020 oo::class create foo {
4021 superclass parent
4022 variable x
4023 variable -? y
4024 method boo {} {
4025 return [incr x],[incr y]
4026 }
4027 }
4028 foo create bar
4029 list [bar boo] [bar boo]
4030 } -returnCodes error -match glob -result {unknown method "-?": must be *}
4031 test oo-27.19 {variables declaration and [info vars]: Bug 2712377} -setup {
4032 oo::class create Foo
4033 set result {}
4034 } -body {
4035 # This is really a test of problems to do with Tcl's introspection when a
4036 # variable resolver is present...
4037 oo::define Foo {
4038 variable foo bar
4039 method setvars {f b} {
4040 set foo $f
4041 set bar $b
4042 }
4043 method dump1 {} {
4044 lappend ::result <1>
4045 foreach v [lsort [info vars *]] {
4046 lappend ::result $v=[set $v]
4047 }
4048 lappend ::result [info locals] [info locals *]
4049 }
4050 method dump2 {} {
4051 lappend ::result <2>
4052 foreach v [lsort [info vars *]] {
4053 lappend ::result $v=[set $v]
4054 }
4055 lappend ::result | foo=$foo [info locals] [info locals *]
4056 }
4057 }
4058 Foo create stuff
4059 stuff setvars what ever
4060 stuff dump1
4061 stuff dump2
4062 return $result
4063 } -cleanup {
4064 Foo destroy
4065 } -result {<1> bar=ever foo=what v v <2> bar=ever foo=what | foo=what v v}
4066 test oo-27.20 {variables declaration and [info vars]: Bug 2712377} -setup {
4067 oo::class create Foo
4068 set result {}
4069 } -body {
4070 # This is really a test of problems to do with Tcl's introspection when a
4071 # variable resolver is present...
4072 oo::define Foo {
4073 variable foo bar
4074 method setvars {f b} {
4075 set foo $f
4076 set bar $b
4077 }
4078 method dump1 {} {
4079 lappend ::result <1>
4080 foreach v [lsort [info vars *o]] {
4081 lappend ::result $v=[set $v]
4082 }
4083 lappend ::result [info locals] [info locals *]
4084 }
4085 method dump2 {} {
4086 lappend ::result <2>
4087 foreach v [lsort [info vars *o]] {
4088 lappend ::result $v=[set $v]
4089 }
4090 lappend ::result | foo=$foo [info locals] [info locals *]
4091 }
4092 }
4093 Foo create stuff
4094 stuff setvars what ever
4095 stuff dump1
4096 stuff dump2
4097 return $result
4098 } -cleanup {
4099 Foo destroy
4100 } -result {<1> foo=what v v <2> foo=what | foo=what v v}
4101 test oo-27.21 {variables declaration uniqueifies: Bug 3396896} -setup {
4102 oo::class create Foo
4103 } -body {
4104 oo::define Foo variable v v v t t v t
4105 info class variable Foo
4106 } -cleanup {
4107 Foo destroy
4108 } -result {v t}
4109 test oo-27.22 {variables declaration uniqueifies: Bug 3396896} -setup {
4110 oo::object create foo
4111 } -body {
4112 oo::objdefine foo variable v v v t t v t
4113 info object variable foo
4114 } -cleanup {
4115 foo destroy
4116 } -result {v t}
4117 test oo-27.23 {variable resolver leakage: Bug 1493a43044} -setup {
4118 oo::class create Super
4119 oo::class create parent {
4120 superclass Super
4121 variable member1 member2
4122 constructor {} {
4123 set member1 parent1
4124 set member2 parent2
4125 }
4126 method getChild {} {
4127 Child new [self]
4128 }
4129 }
4130 oo::class create Child {
4131 superclass Super
4132 variable member1 result
4133 constructor {m} {
4134 set [namespace current]::member1 child1
4135 set ns [info object namespace $m]
4136 namespace upvar $ns member1 l1 member2 l2
4137 upvar 1 member1 l3 member2 l4
4138 [format namespace] upvar $ns member1 l5 member2 l6
4139 [format upvar] 1 member1 l7 member2 l8
4140 set result [list $l1 $l2 $l3 $l4 $l5 $l6 $l7 $l8]
4141 }
4142 method result {} {return $result}
4143 }
4144 } -body {
4145 [[parent new] getChild] result
4146 } -cleanup {
4147 Super destroy
4148 } -result {parent1 parent2 parent1 parent2 parent1 parent2 parent1 parent2}
4149
4150 # A feature that's not supported because the mechanism may change without
4151 # warning, but is supposed to work...
4152 test oo-28.1 {scripted extensions to oo::define} -setup {
4153 interp create foo
4154 foo eval {oo::class create cls {export eval}}
4155 } -cleanup {
4156 interp delete foo
4157 } -body {
4158 foo eval {
4159 proc oo::define::privateMethod {name arguments body} {
4160 uplevel 1 [list method $name $arguments $body]
4161 uplevel 1 [list unexport $name]
4162 }
4163 oo::define cls privateMethod m {x y} {return $x,$y}
4164 cls create obj
4165 list [catch {obj m 1 2}] [obj eval my m 3 4]
4166 }
4167 } -result {1 3,4}
4168
4169 test oo-29.1 {self class with object-defined methods} -setup {
4170 oo::object create obj
4171 } -body {
4172 oo::objdefine obj method demo {} {
4173 self class
4174 }
4175 obj demo
4176 } -returnCodes error -cleanup {
4177 obj destroy
4178 } -result {method not defined by a class}
4179
4180 test oo-30.1 {Bug 2903011: deleting an object in a constructor} -setup {
4181 oo::class create cls
4182 } -body {
4183 oo::define cls {constructor {} {[self] destroy}}
4184 cls new
4185 } -returnCodes error -cleanup {
4186 cls destroy
4187 } -result {object deleted in constructor}
4188 test oo-30.2 {Bug 2903011: deleting an object in a constructor} -setup {
4189 oo::class create cls
4190 } -body {
4191 oo::define cls {constructor {} {my destroy}}
4192 cls new
4193 } -returnCodes error -cleanup {
4194 cls destroy
4195 } -result {object deleted in constructor}
4196
4197 test oo-31.1 {Bug 3111059: when objects and coroutines entangle} -setup {
4198 oo::class create cls
4199 } -constraints memory -body {
4200 oo::define cls {
4201 method justyield {} {
4202 yield
4203 }
4204 constructor {} {
4205 coroutine coro my justyield
4206 }
4207 }
4208 list [leaktest {[cls new] destroy}] [info class instances cls]
4209 } -cleanup {
4210 cls destroy
4211 } -result {0 {}}
4212 test oo-31.2 {Bug 3111059: when objects and coroutines entangle} -setup {
4213 oo::class create cls
4214 } -constraints memory -body {
4215 oo::define cls {
4216 method justyield {} {
4217 yield
4218 }
4219 constructor {} {
4220 coroutine coro my justyield
4221 }
4222 destructor {
4223 rename coro {}
4224 }
4225 }
4226 list [leaktest {[cls new] destroy}] [info class instances cls]
4227 } -cleanup {
4228 cls destroy
4229 } -result {0 {}}
4230
4231 proc SampleSlotSetup script {
4232 set script0 {
4233 oo::class create SampleSlot {
4234 superclass oo::Slot
4235 constructor {} {
4236 variable contents {a b c} ops {}
4237 }
4238 method contents {} {variable contents; return $contents}
4239 method ops {} {variable ops; return $ops}
4240 method Get {} {
4241 variable contents
4242 variable ops
4243 lappend ops [info level] Get
4244 return $contents
4245 }
4246 method Set {lst} {
4247 variable contents $lst
4248 variable ops
4249 lappend ops [info level] Set $lst
4250 return
4251 }
4252 }
4253 }
4254 append script0 \n$script
4255 }
4256
4257 proc SampleSlotCleanup script {
4258 set script0 {
4259 SampleSlot destroy
4260 }
4261 append script \n$script0
4262 }
4263
4264 test oo-32.1 {TIP 380: slots - class test} -setup [SampleSlotSetup {
4265 SampleSlot create sampleSlot
4266 }] -body {
4267 list [info level] [sampleSlot contents] [sampleSlot ops]
4268 } -cleanup [SampleSlotCleanup {
4269 rename sampleSlot {}
4270 }] -result {0 {a b c} {}}
4271 test oo-32.2 {TIP 380: slots - class test} -setup [SampleSlotSetup {
4272 SampleSlot create sampleSlot
4273 }] -body {
4274 list [info level] [sampleSlot -clear] \
4275 [sampleSlot contents] [sampleSlot ops]
4276 } -cleanup [SampleSlotCleanup {
4277 rename sampleSlot {}
4278 }] -result {0 {} {} {1 Set {}}}
4279 test oo-32.3 {TIP 380: slots - class test} -setup [SampleSlotSetup {
4280 SampleSlot create sampleSlot
4281 }] -body {
4282 list [info level] [sampleSlot -append g h i] \
4283 [sampleSlot contents] [sampleSlot ops]
4284 } -cleanup [SampleSlotCleanup {
4285 rename sampleSlot {}
4286 }] -result {0 {} {a b c g h i} {1 Get 1 Set {a b c g h i}}}
4287 test oo-32.4 {TIP 380: slots - class test} -setup [SampleSlotSetup {
4288 SampleSlot create sampleSlot
4289 }] -body {
4290 list [info level] [sampleSlot -set d e f] \
4291 [sampleSlot contents] [sampleSlot ops]
4292 } -cleanup [SampleSlotCleanup {
4293 rename sampleSlot {}
4294 }] -result {0 {} {d e f} {1 Set {d e f}}}
4295 test oo-32.5 {TIP 380: slots - class test} -setup [SampleSlotSetup {
4296 SampleSlot create sampleSlot
4297 }] -body {
4298 list [info level] [sampleSlot -set d e f] [sampleSlot -append g h i] \
4299 [sampleSlot contents] [sampleSlot ops]
4300 } -cleanup [SampleSlotCleanup {
4301 rename sampleSlot {}
4302 }] -result {0 {} {} {d e f g h i} {1 Set {d e f} 1 Get 1 Set {d e f g h i}}}
4303
4304 test oo-33.1 {TIP 380: slots - defaulting} -setup [SampleSlotSetup {
4305 set s [SampleSlot new]
4306 }] -body {
4307 list [$s x y] [$s contents]
4308 } -cleanup [SampleSlotCleanup {
4309 rename $s {}
4310 }] -result {{} {a b c x y}}
4311 test oo-33.2 {TIP 380: slots - defaulting} -setup [SampleSlotSetup {
4312 set s [SampleSlot new]
4313 }] -body {
4314 list [$s destroy; $s unknown] [$s contents]
4315 } -cleanup [SampleSlotCleanup {
4316 rename $s {}
4317 }] -result {{} {a b c destroy unknown}}
4318 test oo-33.3 {TIP 380: slots - defaulting} -setup [SampleSlotSetup {
4319 set s [SampleSlot new]
4320 }] -body {
4321 oo::objdefine $s forward --default-operation my -set
4322 list [$s destroy; $s unknown] [$s contents] [$s ops]
4323 } -cleanup [SampleSlotCleanup {
4324 rename $s {}
4325 }] -result {{} unknown {1 Set destroy 1 Set unknown}}
4326 test oo-33.4 {TIP 380: slots - errors} -setup [SampleSlotSetup {
4327 set s [SampleSlot new]
4328 }] -body {
4329 # Method names beginning with "-" are special to slots
4330 $s -grill q
4331 } -returnCodes error -cleanup [SampleSlotCleanup {
4332 rename $s {}
4333 }] -result \
4334 {unknown method "-grill": must be -append, -clear, -set, contents or ops}
4335
4336 test oo-34.1 {TIP 380: slots - presence} -setup {
4337 set obj [oo::object new]
4338 set result {}
4339 } -body {
4340 oo::define oo::object {
4341 ::lappend ::result [::info object class filter]
4342 ::lappend ::result [::info object class mixin]
4343 ::lappend ::result [::info object class superclass]
4344 ::lappend ::result [::info object class variable]
4345 }
4346 oo::objdefine $obj {
4347 ::lappend ::result [::info object class filter]
4348 ::lappend ::result [::info object class mixin]
4349 ::lappend ::result [::info object class variable]
4350 }
4351 return $result
4352 } -cleanup {
4353 $obj destroy
4354 } -result {::oo::Slot ::oo::Slot ::oo::Slot ::oo::Slot ::oo::Slot ::oo::Slot ::oo::Slot}
4355 test oo-34.2 {TIP 380: slots - presence} {
4356 lsort [info class instances oo::Slot]
4357 } {::oo::define::filter ::oo::define::mixin ::oo::define::superclass ::oo::define::variable ::oo::objdefine::filter ::oo::objdefine::mixin ::oo::objdefine::variable}
4358 proc getMethods obj {
4359 list [lsort [info object methods $obj -all]] \
4360 [lsort [info object methods $obj -private]]
4361 }
4362 test oo-34.3 {TIP 380: slots - presence} {
4363 getMethods oo::define::filter
4364 } {{-append -clear -set} {Get Set}}
4365 test oo-34.4 {TIP 380: slots - presence} {
4366 getMethods oo::define::mixin
4367 } {{-append -clear -set} {--default-operation Get Set}}
4368 test oo-34.5 {TIP 380: slots - presence} {
4369 getMethods oo::define::superclass
4370 } {{-append -clear -set} {--default-operation Get Set}}
4371 test oo-34.6 {TIP 380: slots - presence} {
4372 getMethods oo::define::variable
4373 } {{-append -clear -set} {Get Set}}
4374 test oo-34.7 {TIP 380: slots - presence} {
4375 getMethods oo::objdefine::filter
4376 } {{-append -clear -set} {Get Set}}
4377 test oo-34.8 {TIP 380: slots - presence} {
4378 getMethods oo::objdefine::mixin
4379 } {{-append -clear -set} {--default-operation Get Set}}
4380 test oo-34.9 {TIP 380: slots - presence} {
4381 getMethods oo::objdefine::variable
4382 } {{-append -clear -set} {Get Set}}
4383
4384 test oo-35.1 {Bug 9d61624b3d: Empty superclass must not cause crash} -setup {
4385 oo::class create fruit {
4386 method eat {} {}
4387 }
4388 set result {}
4389 } -body {
4390 lappend result [fruit create ::apple] [info class superclasses fruit]
4391 oo::define fruit superclass
4392 lappend result [info class superclasses fruit] \
4393 [info object class apple oo::object] \
4394 [info class call fruit destroy] \
4395 [catch { apple }]
4396 } -cleanup {
4397 unset -nocomplain result
4398 fruit destroy
4399 } -result {::apple ::oo::object ::oo::object 1 {{method destroy ::oo::object {core method: "destroy"}}} 1}
4400 test oo-35.2 {Bug 9d61624b3d: Empty superclass must not cause crash} -setup {
4401 oo::class create fruitMetaclass {
4402 superclass oo::class
4403 method eat {} {}
4404 }
4405 set result {}
4406 } -body {
4407 lappend result [fruitMetaclass create ::appleClass] \
4408 [appleClass create orange] \
4409 [info class superclasses fruitMetaclass]
4410 oo::define fruitMetaclass superclass
4411 lappend result [info class superclasses fruitMetaclass] \
4412 [info object class appleClass oo::class] \
4413 [catch { orange }] [info object class orange] \
4414 [appleClass create pear]
4415 } -cleanup {
4416 unset -nocomplain result
4417 fruitMetaclass destroy
4418 } -result {::appleClass ::orange ::oo::class ::oo::class 1 1 ::appleClass ::pear}
4419 test oo-35.3 {Bug 593baa032c: superclass list teardown} {
4420 # Bug makes this crash, especially with mem-debugging on
4421 oo::class create B {}
4422 oo::class create D {superclass B}
4423 namespace eval [info object namespace D] [list [namespace which B] destroy]
4424 } {}
4425 test oo-35.4 {Bug 593baa032c: mixins list teardown} {
4426 # Bug makes this crash, especially with mem-debugging on
4427 oo::class create B {}
4428 oo::class create D {mixin B}
4429 namespace eval [info object namespace D] [list [namespace which B] destroy]
4430 } {}
4431 test oo-35.5 {Bug 1a56550e96: introspectors must traverse mixin links correctly} -setup {
4432 oo::class create base {
4433 unexport destroy
4434 }
4435 } -body {
4436 oo::class create C {
4437 superclass base
4438 method c {} {}
4439 }
4440 oo::class create D {
4441 superclass base
4442 mixin C
4443 method d {} {}
4444 }
4445 oo::class create E {
4446 superclass D
4447 method e {} {}
4448 }
4449 E create e1
4450 list [lsort [info class methods E -all]] [lsort [info object methods e1 -all]]
4451 } -cleanup {
4452 base destroy
4453 } -result {{c d e} {c d e}}
4454 test oo-35.6 {
4455 Bug : teardown of an object that is a class that is an instance of itself
4456 } -setup {
4457 oo::class create obj
4458
4459 oo::copy obj obj1 obj1
4460 oo::objdefine obj1 {
4461 mixin obj1 obj
4462 }
4463 oo::copy obj1 obj2
4464 oo::objdefine obj2 {
4465 mixin obj2 obj1
4466 }
4467 } -body {
4468 rename obj2 {}
4469 rename obj1 {}
4470 # doesn't crash
4471 return done
4472 } -cleanup {
4473 rename obj {}
4474 } -result done
4475 test oo-35.7.1 {Bug 7842f33a5c: destructor cascading in stereotypes} -setup {
4476 oo::class create base
4477 oo::class create RpcClient {
4478 superclass base
4479 method write name {
4480 lappend ::result "RpcClient -> $name"
4481 }
4482 method create_bug {} {
4483 MkObjectRpc create cfg [self] 111
4484 }
4485 }
4486 oo::class create MkObjectRpc {
4487 superclass base
4488 variable hdl
4489 constructor {rpcHdl mqHdl} {
4490 set hdl $mqHdl
4491 oo::objdefine [self] forward rpc $rpcHdl
4492 }
4493 destructor {
4494 my rpc write otto-$hdl
4495 }
4496 }
4497 set ::result {}
4498 } -body {
4499 # In this case, sub-objects are deleted during major object NS cleanup and
4500 # are trying to call back into the major object (which is mostky gone at
4501 # this point). Things are messy; error is reported via bgerror as the
4502 # avenue most likely to reach a user.
4503 bgerrorIntercept ::result {
4504 set FH [RpcClient new]
4505 $FH create_bug
4506 $FH destroy
4507 update
4508 }
4509 join $result \n
4510 } -cleanup {
4511 base destroy
4512 } -result {impossible to invoke method "write": no defined method or unknown method}
4513 test oo-35.7.2 {Bug 7842f33a5c: destructor cascading in stereotypes} -setup {
4514 oo::class create base
4515 oo::class create RpcClient {
4516 superclass base
4517 method write name {
4518 lappend ::result "RpcClient -> $name"
4519 }
4520 method create_bug {} {
4521 MkObjectRpc create cfg [self] 111
4522 }
4523 destructor {
4524 lappend ::result "Destroyed"
4525 }
4526 }
4527 oo::class create MkObjectRpc {
4528 superclass base
4529 variable hdl
4530 constructor {rpcHdl mqHdl} {
4531 set hdl $mqHdl
4532 oo::objdefine [self] forward rpc $rpcHdl
4533 }
4534 destructor {
4535 my rpc write otto-$hdl
4536 }
4537 }
4538 set ::result {}
4539 } -body {
4540 # In this case, sub-objects are deleted during major object NS cleanup, and
4541 # we've a destructor on the major class to monitor when it happens. Things
4542 # are still messy, but the order is clear; error is reported via bgerror as
4543 # the avenue most likely to reach a user.
4544 bgerrorIntercept ::result {
4545 set FH [RpcClient new]
4546 $FH create_bug
4547 $FH destroy
4548 update
4549 }
4550 join $result \n
4551 } -cleanup {
4552 base destroy
4553 } -result {Destroyed
4554 impossible to invoke method "write": no defined method or unknown method}
4555 test oo-35.7.3 {Bug 7842f33a5c: destructor cascading in stereotypes} -setup {
4556 oo::class create base
4557 oo::class create RpcClient {
4558 superclass base
4559 variable interiorObjects
4560 method write name {
4561 lappend ::result "RpcClient -> $name"
4562 }
4563 method create_bug {} {
4564 set obj [MkObjectRpc create cfg [self] 111]
4565 lappend interiorObjects $obj
4566 return $obj
4567 }
4568 destructor {
4569 lappend ::result "Destroyed"
4570 # Explicit destroy of interior objects
4571 foreach obj $interiorObjects {
4572 $obj destroy
4573 }
4574 }
4575 }
4576 oo::class create MkObjectRpc {
4577 superclass base
4578 variable hdl
4579 constructor {rpcHdl mqHdl} {
4580 set hdl $mqHdl
4581 oo::objdefine [self] forward rpc $rpcHdl
4582 }
4583 destructor {
4584 my rpc write otto-$hdl
4585 }
4586 }
4587 set ::result {}
4588 } -body {
4589 # In this case, sub-objects are deleted while the destructor is running and
4590 # the destroy is neat, so things work sanely. Error follows standard Tcl
4591 # error flow route; bgerror is not used.
4592 bgerrorIntercept ::result {
4593 set FH [RpcClient new]
4594 $FH create_bug
4595 $FH destroy
4596 update
4597 }
4598 join $result \n
4599 } -cleanup {
4600 base destroy
4601 } -result "Destroyed\nRpcClient -> otto-111"
4602
4603 rename bgerrorIntercept {}
4604 cleanupTests
4605 return
4606
4607 # Local Variables:
4608 # mode: tcl
4609 # End: