File: destroy.test

package info (click to toggle)
nsf 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 13,208 kB
  • sloc: ansic: 32,687; tcl: 10,723; sh: 660; pascal: 176; javascript: 135; lisp: 41; makefile: 24
file content (1120 lines) | stat: -rw-r--r-- 35,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# -*- Tcl -*-
package require nx
package require nx::test

set ::tcl86 [package vsatisfies [package req Tcl] 8.6-]

nx::test configure -count 10

::nx::configure defaultMethodCallProtection false

::nsf::method::alias ::nx::Object set -frame object ::set

nx::Class create O -superclass nx::Object {
  :method init {} {
    set ::ObjectDestroy 0
    set ::firstDestroy 0
  }
  :method destroy {} {
    incr ::ObjectDestroy
    #[:info class] dealloc [current]
    next
  }
}

#
# classical simple case
#
set case "simple destroy (1)"
nx::test case simple-destroy-1
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  :destroy
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  puts stderr XXXX2
  ? "[current] set x" 1 "$::case can still access [current]"
  puts stderr XXXX3
  ? {::nsf::object::exists c1} 1 "$::case object still exists in proc"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 1 "ObjectDestroy called"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 0 "$::case object deleted"
? "set ::firstDestroy" 1 "firstDestroy called"


#
# simple case, destroy does not propagate, c1 survives
#
set case "simple destroy (2), destroy blocks"
nx::test case simple-destroy-2
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy block"}
C method foo {} {
  puts stderr "==== $::case [current]"
  :destroy
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  ? "[current] set x" 1 "$::case can still access [current]"
  ? {::nsf::object::exists c1} 1 "$::case object still exists in proc"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 0 "ObjectDestroy called"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 1 "$::case object deleted"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 0 "ObjectDestroy called"

#
# simple object recreate
#
set case "recreate"
nx::test case recreate
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  [:info class] create [current]
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  ? "[current] set x" 1 "$::case can still access [current]"
  ? {::nsf::object::exists c1} 1 "$::case object still exists in proc"
  ? "set ::firstDestroy" 0 "firstDestroy called"
  ? "set ::ObjectDestroy" 0 "ObjectDestroy called"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 1 "$::case object deleted"
? "set ::firstDestroy" 0 "firstDestroy called"

#
# cmd rename to empty, xotcl provides its own rename and calls destroy
# .. like simple case above
#
set case "cmd rename empty (1)"
nx::test case rename-empty-1
nx::Object create o
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  rename [current] ""
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  ? "[current] set x" 1 "$::case can still access [current]"
  ? {::nsf::object::exists c1} 1 "$::case object still exists in proc"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 1 "ObjectDestroy called"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 0 "$::case object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "ObjectDestroy called"

#
# cmd rename to empty, xotcl provides its own rename and calls
# destroy, but destroy does not propagate, c1 survives rename, since
# this is the situation like above, as long xotcl's rename is used.
#
set case "cmd rename empty (2)"
nx::test case rename-empty-2
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy block"}
C method foo {} {
  puts stderr "==== $::case [current]"
  rename [current] ""
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  ? "[current] set x" 1 "$::case can still access [current]"
  ? {::nsf::object::exists c1} 1 "$::case object still exists in proc"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 0 "ObjectDestroy called"
}
C create c1
c1 foo

#puts stderr ======[c1 set x]
? {::nsf::object::exists c1} 1 "$::case object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 0 "ObjectDestroy called"

#
# cmd rename other xotcl object to current, 
# xotcl's rename invokes a move 
#
set case "cmd rename object to current"
nx::test case rename-to-current
nx::Object create o
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  rename o [current]
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  #? "[current] set x" 1 "$::case can still access [current]"

  if {$::tcl86} {
    ? "[current] set x" {TCL LOOKUP VARNAME x} "$::case cannot access [current]"
  } else {
    ? "[current] set x" {can't read "x": no such variable} "$::case cannot access [current]"
  }
  ? {::nsf::object::exists c1} 1 "$::case object still exists in proc"
  #? "set ::firstDestroy" 0 "firstDestroy called"
  #? "set ::ObjectDestroy" 0 "ObjectDestroy called"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 1 "ObjectDestroy called"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 1 "$::case object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "ObjectDestroy called"

#
# cmd rename other proc to current object, 
# xotcl's rename invokes a move 
#
set case "cmd rename proc to current"
nx::test case rename-proc-to-current
proc o args {}
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  set x [catch {rename o [current]}]
  ? "set _ $x" 1 "$::case tcl refuses to rename into an existing command"
}
C create c1
c1 foo
? {::nsf::object::exists c1} 1 "$::case object still exists after proc"
? "set ::firstDestroy" 0 "firstDestroy called"
? "set ::ObjectDestroy" 0 "ObjectDestroy called"

rename o ""

#
# namespace delete: tcl delays delete until the namespace is not
# active anymore. destroy is called after BBBB. Hypothesis: destroy is
# called only when we are lucky, since C might be destroyed before c1
# by the namespace delete
#

set case "delete parent namespace (1)"
nx::test case delete-parent-namespace
namespace eval ::test {
  nx::Class create C -superclass O
  C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
  C method foo {} {
    puts stderr "==== $::case [current]"
    namespace delete ::test
    
    puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
    :set x 1
    #
    # If the following line is commented in, the namespace is deleted
    # here. Is there a bug with nsPtr->activationCount
    #
    #? "[current] set x" 1 "$::case can still access [current]"
    puts stderr "BBB"
    puts stderr "???? [current] exists [::nsf::object::exists [current]]"
    ? "::nsf::object::exists [current]" 0 ;# WHY?
    puts stderr "???? [current] exists [::nsf::object::exists [current]]"
    ? "set ::firstDestroy" 0 "firstDestroy called"
    ? "set ::ObjectDestroy" 0 "$::case destroy not yet called"
  }
}
test::C create test::c1
test::c1 foo

? {::nsf::object::exists test::c1} 0  "object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "destroy was called when popping stack frame"
? {::nsf::object::exists ::test::C} 0  "class still exists after proc"
? {namespace exists ::test::C} 0  "namespace ::test::C still exists after proc"
? {namespace exists ::test} 1  "parent ::test namespace still exists after proc"
? {namespace exists ::xotcl::classes::test::C} 0  "namespace ::xotcl::classes::test::C still exists after proc"

#
# namespace delete: tcl delays delete until the namespace is not
# active anymore. destroy is called after BBBB, but does not
# propagate.  
#
set case "delete parent namespace (2)"
nx::test case delete-parent-namespace-2
namespace eval ::test {
  ? {namespace exists test::C} 0 "exists test::C"
  nx::Class create C -superclass O
  C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy block"}
  C method foo {} {
    puts stderr "==== $::case [current]"
    namespace delete ::test
    puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
    :set x 1
    #
    # If the following line is commented in, the namespace is deleted
    # here. Is there a bug with nsPtr->activationCount
    #
    #? "[current] set x" 1 "$::case can still access [current]"
    puts stderr "BBBB"
    puts stderr "???? [current] exists [::nsf::object::exists [current]]"
    ? "::nsf::object::exists [current]" 0 "$::case object still exists in proc";# WHY?
    puts stderr "???? [current] exists [::nsf::object::exists [current]]"
    ? "set ::firstDestroy" 0 "firstDestroy called"
    ? "set ::ObjectDestroy" 0  "ObjectDestroy called"; # NOT YET CALLED
  }
}
test::C create test::c1
test::c1 foo

? {::nsf::object::exists test::c1} 0  "$::case object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 0 "ObjectDestroy called"  ;# toplevel destroy was blocked

#
# controlled namespace delete: xotcl has its own namespace cleanup,
# topological order should be always ok. however, the object o::c1 is
# already deleted, while a method of it is executed
#
set case "delete parent object (1)"
nx::test case delete-parent-object
nx::Object create o
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  o destroy
  puts stderr "AAAA"
  # the following object::exists call has a problem in Tcl_GetCommandFromObj(), 
  # which tries to access invalid memory
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  #? "[current] set x" 1 "$::case can still access [current]"
  puts stderr "BBBB"
  ? {::nsf::object::exists ::o::c1} 0 "$::case object still exists in proc"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 1 "ObjectDestroy called"
}
C create o::c1
o::c1 foo

? {::nsf::object::exists ::o::c1} 0 "$::case object o::c1 still exists after proc"
? {::nsf::object::exists o} 0 "$::case object o still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "ObjectDestroy called"

#
# controlled namespace delete: xotcl has its own namespace cleanup.
# destroy does not delegate, but still o::c1 does not survive, since o
# is deleted.
#
set case "delete parent object (2)"
nx::test case delete-parent-object-2
nx::Object create o
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy block"}
C method foo {} {
  puts stderr "==== $::case [current]"
  o destroy
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  #? "[current] set x" 1 "$::case can still access [current]"
  puts stderr "BBB"
  ? {::nsf::object::exists ::o::c1} 0 "$::case object still exists in proc"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 0 "ObjectDestroy called"
}
C create o::c1
o::c1 foo

? {::nsf::object::exists ::o::c1} 0 "$::case object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 0 "ObjectDestroy called"


#
# create another cmd with the current object's name.
# XOTcl 1.6 crashed on this test.
#
set case "redefine current object as proc"
nx::test case redefine-current-object-as-proc
nx::Object create o
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  proc [current] {args} {puts HELLO}
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  #? "[current] set x" 1 "$::case can still access [current]"
  puts stderr "BBB"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  ? "set ::ObjectDestroy" 1 "ObjectDestroy called"
  ? {::nsf::object::exists c1} 0 "$::case object still exists in proc"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 0 "$::case object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "ObjectDestroy called"

rename c1 ""

#
# delete the active class
#
set case "delete active class"
nx::test case delete-active-class
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  C destroy
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  #? "[current] set x" 1 "$::case can still access [current]"
  puts stderr "BBB"
  #? [:info class] ::xotcl::Object "object reclassed"
  ? [:info class] ::C "object reclassed?"
  ? "set ::firstDestroy" 0 "firstDestroy called"
  ? "set ::ObjectDestroy" 0 "ObjectDestroy called"
  ? {::nsf::object::exists c1} 1 "object still exists in proc"
  #? {::nsf::is class ::C} 0 "class still exists in proc"
  ? {::nsf::is class ::C} 1 "class still exists in proc"
}
C create c1
c1 foo

? {::nsf::object::exists c1} 1 "object still exists after proc"
? [c1 info class] ::nx::Object "after proc: object reclassed?"
? "set ::firstDestroy" 0 "firstDestroy called"
? "set ::ObjectDestroy" 0 "ObjectDestroy called"

#
# delete active object nested in class
#
set case "delete active object nested in class"
nx::test case delete-active-object-nested-in-class
nx::Class create C -superclass O
C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
C method foo {} {
  puts stderr "==== $::case [current]"
  C destroy
  puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
  :set x 1
  #? "[current] set x" 1 "$::case can still access [current]"
  puts stderr "BBB"
  #? "set ::firstDestroy" 0 "firstDestroy called"
  ? "set ::firstDestroy" 1 "firstDestroy called"
  #? "set ::ObjectDestroy" 0 "ObjectDestroy called"
  ? "set ::ObjectDestroy" 1 "ObjectDestroy called"
  ? [:info class] ::C "object reclassed"
  #? [:info class] ::xotcl::Object "object reclassed"
  ? {::nsf::object::exists ::C::c1} 1 "object still exists in proc"
  ? {::nsf::is class ::C} 1 "class still exists in proc"
}
C create ::C::c1
C::c1 foo
#puts stderr ======[::nsf::object::exists ::C::c1]
? {::nsf::object::exists ::C::c1} 0 "object still exists after proc"
? {::nsf::is class ::C} 0 "class still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "ObjectDestroy called"

#
nx::test case nesting-destroy {
  nx::Object create x
  nx::Object create x::y
  x destroy
  ? {::nsf::object::exists x} 0 "parent object gone"
  ? {::nsf::object::exists x::y} 0 "child object gone"
}

nx::test case deleting-aliased-object1 {
  nx::Object create o
  nx::Object create o2
  # behave like an ensemble: aliased object has self of the caller
  ::nsf::object::property o2 perobjectdispatch 1
  ::nsf::method::alias o a o2
  ? {o a} ::o2 "call object via alias"
  ? {o info object method type a} alias
  ## the ensemble-object needs per-object methods
  o2 object method info args {:info {*}$args}
  o2 object method set args {:set {*}$args}
  ::nsf::object::property o2 keepcallerself 1
  ? {o a info vars} "" "call info on aliased object"
  ? {o set x 10} 10   "set variable on object"
  ? {o info vars} x   "query vars"
  ? {o a info vars} x  "query vars via alias"
  ? {o a set x} 10     "set var via alias"
  o2 destroy
  #? {o a info vars} "Trying to dispatch deleted object via method 'a'" "1st call on deleted object"
  #? {o a info vars} "::o: unable to dispatch method 'a'" "2nd call on deleted object"
  ? {o a info vars} {target "o2" of alias a apparently disappeared} "1st call on deleted object"
  ? {o a info vars} {target "o2" of alias a apparently disappeared} "2nd call on deleted object"
}

nx::test case deleting-aliased-object2 {
  nx::Object create o
  nx::Object create o2
  # The methods of the aliased object have their own self
  ::nsf::method::alias o a o2

  ? {o a} ::o2 "call object via alias"

  ? {o info object method type a} alias
  # In order to avoid recursive calls, we have to provide the
  # selector for the method definitions in nx::Object
  o2 object method info args {: ::nsf::classes::nx::Object::info {*}$args}
  o2 object method set args {: ::nsf::classes::nx::Object::set {*}$args}

  ? {o a info vars} ""   "call info on aliased object"
  ? {o set x 10} 10     "set variable on object o"
  ? {o info vars} x     "query vars of o"
  ? {o a info vars} ""  "query vars via alias (from o2)"
  ? {o a set y 1} 1     "set var via alias (on o2)"
  ? {o a info vars} y   "query vars via alias (from o2)"
  o2 destroy
  #? {o a info vars} "Trying to dispatch deleted object via method 'a'" "1st call on deleted object"
  #? {o a info vars} "::o: unable to dispatch method 'a'" "2nd call on deleted object"
  ? {o a info vars} {target "o2" of alias a apparently disappeared} "1st call on deleted object"
  ? {o a info vars} {target "o2" of alias a apparently disappeared} "2nd call on deleted object"
}

set case "deleting object with alias to object"
nx::test case deleting-object-with-alias-to-object
nx::Object create o
nx::Object create o3
::nsf::method::alias o x o3
o destroy
? {::nsf::object::exists o} 0 "parent object gone"
? {::nsf::object::exists o3} 1 "aliased object still here"
o3 destroy
? {::nsf::object::exists o3} 0 "aliased object destroyed"

set case "create an alias, and delete cmd via aggregation"
nx::test case create-alias-delete-via-aggregation
nx::Object create o
nx::Object create o3
::nsf::method::alias o x o3
#o::x destroy
o3 destroy
? {o x foo} {target "o3" of alias x apparently disappeared}
#? {o x foo} {Trying to dispatch deleted object via method 'x'}
? {::nsf::object::exists o3} 0 "aliased object destroyed"
o destroy

#
# create an alias, and recreate obj
#
nx::test case create-alias-and-recreate-obj {
  nx::Object create o
  nx::Object create o3
  o object alias x o3
  nx::Object create o3
  o3 object method set args {: ::nsf::classes::nx::Object::set {*}$args}
  o x set a 13
  ? {o x set a} 13 "aliased object works after recreate"
}

#
# create an alias on the class level, double aliasing, delete aliased
# object
#
nx::test case create-alias-on-class-delete-aliased-obj {
  nx::Class create C
  nx::Object create o
  nx::Object create o3

  ::nsf::object::property o keepcallerself 1
  ::nsf::object::property o3 keepcallerself 1
  ::nsf::object::property o perobjectdispatch 1
  ::nsf::object::property o3 perobjectdispatch 1
  o object alias a o3
  C alias b o

  o3 object method set args {: ::nsf::classes::nx::Object::set {*}$args}
  o object method set args {: ::nsf::classes::nx::Object::set {*}$args}

  C create c1
  ? {c1 b set B 2} 2 "call 1st level"
  ? {c1 b a set A 3} 3 "call 2nd level"
  
  ? {c1 set B} 2 "call 1st level ok"
  ? {c1 set A} 3 "call 2nd level ok"
  o destroy
  #? {c1 b} "Trying to dispatch deleted object via method 'b'" "call via alias to deleted object"
  ? {c1 b} {target "o" of alias b apparently disappeared} "call via alias to deleted object"
}

#
# create an alias on the class level, double aliasing, destroy class
#
nx::test case create-alias-on-class-destroy-class {
  nx::Class create C
  nx::Object create o
  nx::Object create o3
  o object alias a o3
  C alias b o
  C create c1
  C destroy
  ? {::nsf::object::exists o} 1 "object o still here"
  ? {::nsf::object::exists o3} 1 "object o3 still here"
}

#
# test cases where preexisting namespaces are re-used
#

nx::test case module {
  # create a namespace with an object/class in it
  namespace eval ::module { nx::Object create foo }
  
  # reuse the namespace for a class/object
  nx::Class create ::module

  ? {::nsf::is class ::module} 1

  # delete the object/class ... and namespace
  ::module destroy

  ? {::nsf::is class ::module} 0
}

nx::test case namespace-import {

  namespace eval ::module {
    nx::Class create Foo {
      :create foo
    }
    namespace export Foo foo
  }
  nx::Class create ::module {
    :create mod1
  }
  ? {::nsf::is class ::module::Foo} 1
  ? {::nsf::is class ::module::foo} 0
  ? {::nsf::object::exists ::module::foo} 1
  ? {::nsf::is class ::module} 1

  nx::Object create ::o { :require namespace }
  namespace eval ::o {namespace import ::module::*}

  ? {::nsf::is class ::o::Foo} 1
  ? {::nsf::object::exists ::o::foo} 1

  # do not destroy namespace imported objects/classes
  ::o destroy

  ? {::nsf::is class ::o::Foo} 0
  ? {::nsf::object::exists ::o::foo} 0

  ? {::nsf::is class ::module::Foo} 1
  ? {::nsf::object::exists ::module::foo} 1

  ::module destroy
}


# to avoid CallDirectly, we could activate this line
::nx::Class create M {:method dealloc args {next}}

nx::test case delete-parent-namespace-dealloc
namespace eval ::test {
  nx::Class create C -superclass O
  C method destroy {} {incr ::firstDestroy; puts stderr "  *** [current] destroy"; next}
  C method foo {} {
    puts stderr "==== $::case [current]"
    namespace delete ::test
    puts stderr "AAAA [current] exists [::nsf::object::exists [current]]"
    :set x 1
    #
    # If the following line is commented in, the namespace is deleted
    # here. Is there a bug with nsPtr->activationCount
    #
    #? "[current] set x" 1 "$::case can still access [current]"
    puts stderr "???? [current] exists [::nsf::object::exists [current]]"
    ? "::nsf::object::exists [current]" 0 ;# WHY?
    puts stderr "???? [current] exists [::nsf::object::exists [current]]"
    ? "set ::firstDestroy" 0 "firstDestroy called"
    ? "set ::ObjectDestroy" 0 "$::case destroy not yet called"
  }
}

test::C create test::c1
test::c1 foo
? {::nsf::object::exists test::c1} 0  "object still exists after proc"
? "set ::firstDestroy" 1 "firstDestroy called"
? "set ::ObjectDestroy" 1 "destroy was called when popping stack frame"
? {::nsf::object::exists ::test::C} 0  "class still exists after proc"
? {namespace exists ::test::C} 0  "namespace ::test::C still exists after proc"
? {namespace exists ::test} 1  "parent ::test namespace still exists after proc"
? {namespace exists ::xotcl::classes::test::C} 0  "namespace ::xotcl::classes::test::C still exists after proc"

nx::test case destroy-during-init {
  # create class
  nx::Class create Foo {
    :public method bar {} {return 1}
    :public method baz {} {return 2}
  }
  
  # create object
  Foo create f1 { :bar; :baz; :destroy } 

  ? {info command f1} "" "explicit destroy of object"

  set c [nx::Class new {
    :public method bar {} {return 1}
    :public method baz {} {return 2}
    :new { :bar; :baz; :destroy }
    :destroy
  }]

  ? [list info command $c] "" "explicit destroy of class"

  package require nx::volatile
  ::nsf::method::require ::nx::Object volatile

  # create new class and object and cleanup everything
  set x [nx::Class new {
    :volatile
    :public method bar {} {return 1}
    :public method baz {} {return 2}
    :new { :volatile; :bar; :baz }
  }]
  
  ? [list info command $x] "" "destroy via volatile"
  
  set x [nx::Class new -volatile {
    :public method bar {} {return 1}
    :public method baz {} {return 2}
    :new { :volatile; :bar; :baz }
  }]
  
  ? [list info command $x] $x "destroy via volatile method"

  # create new class and object and cleanup everything + 2 filters

  ::nx::Object public method f1 args {next}
  ::nx::Object public method f2 args {next}
  ::nx::Object filters set {f1 f2}

  set x [nx::Class new {
    :volatile
    :public method bar {} {return 1}
    :public method baz {} {return 2}
    :new { :volatile; :bar; :baz }
  }]
  
  ? [list info command $x] "" "destroy via volatile + 2 filters"
  
  set x [nx::Class new -volatile {
    :public method bar {} {return 1}
    :public method baz {} {return 2}
    :new { :volatile; :bar; :baz }
  }]
  
  ? [list info command $x] $x "destroy via volatile method + 2 filters"

  ::nx::Object filters set ""
}

nx::test case nested-ordered-composite {
  # The following test case an explicit deletion/redefinition of an
  # toplevel object (o1) will cause the implicit deletion of a nested
  # object o1::o2. The object o2 has as well several included objects,
  # containing an "ordered composite". The deletion of the ordered
  # compostite causes the (explicit) deletion of its siblings (all
  # children of o1::o2). This is actually a stress test for the deletion
  # of o2's namespace, since the loop over its children will be
  # confronted with the deletion of indirectly deleted items (deleted by
  # the deletion of the ordered composite).

  nx::Class create C {
    :property os
    :public method destroy {} {
      #puts stderr "[self] destroy ${:os}"
      foreach o ${:os} {
	if {[::nsf::object::exists $o]} {
	  #puts stderr "--D $o destroy"
	  $o destroy
	}
	next
      }
    }
  }
  #
  # 10 siblings of oc1:
  # deletion order in bucket: 8 4 10 9 5 1 6 2 oc1 7 3
  # oc1 deletes 7 and 3, fine
  # ... loop might run into an epoched cmd -> might crash
  #

  set c 0
  for {set i 0} {$i < 10} {incr i} {
    set os [list]
    for {set j 0} {$j < 10} {incr j} {lappend os ::o1::o2::[incr c]}
    nx::Object create ::o1
    nx::Object create ::o1::o2
    foreach o $os {nx::Object create $o}
    C create ::o1::o2::oc1 -os $os
    ? {llength [o1 info children]} 1
    ? {llength [o1::o2 info children]} 11
  }

  ### 20 siblings of oc1 (has to be >12):
  # deletion order in bucket: 17 18 1 20 19 2 3 4 5 6 7 8 9 19 11 oc1 12 13 14 15 16
  # oc1 deletes 12 13 14 15 16
  # after destroy of oc1 
  #  a) NextHashEntry(hSearch) returns valid looking hPtr
  #  b) Tcl_GetHashValue(hPtr) returns garbage (uninitialized memory?) instead of cmd
  # --> might crash
  # 
  set c 0
  for {set i 0} {$i < 10} {incr i} {
    set os [list]
    for {set j 0} {$j < 20} {incr j} {lappend os ::o1::o2::[incr c]}
    nx::Object create ::o1
    nx::Object create ::o1::o2
    foreach o $os {nx::Object create $o}
    C create ::o1::o2::oc1 -os $os
    ? {llength [o1 info children]} 1
    ? {llength [o1::o2 info children]} 21
  }

  # similar to above, but this time partial deletes occur
  set c 0
  for {set i 0} {$i < 10} {incr i} {
    set os [list]
    for {set j 0} {$j < 20} {incr j} {lappend os ::o1::o2::[incr c]}
    nx::Object create ::o1
    nx::Object create ::o1::o2
    foreach o $os {nx::Object create $o}
    C create ::o1::o2::ocX -os {}
    C create ::o1::o2::ocY -os $os
    ? {llength [o1 info children]} 1
    ? {llength [o1::o2 info children]} 22
  }
}


#
# The following tests the deletion order triggering implicit
# deletions. This caused a crash in nsf 2.0b2.
#
package req nx::serializer

nx::test case class-object-property {

  nx::Class create C {
    :object property -accessor public x
    :property a:int
  }

  ? {::nsf::object::exists ::C} 1
  ? {::nsf::object::exists ::C::slot} 1
  
  set s(C) [C serialize]
  C destroy

  ? {::nsf::object::exists ::C} 0
  ? {::nsf::object::exists ::C::slot} 0

  eval $s(C)

  ? {::nsf::object::exists ::C} 1
  ? {::nsf::object::exists ::C::slot} 1

  C::slot destroy
  ? {::nsf::object::exists ::C} 1
  ? {::nsf::object::exists ::C::slot} 0

  C destroy
  ? {::nsf::object::exists ::C} 0
}

nx::test case unset-traces-during-cleanup {
  global i
  set i [interp create]
  $i eval {
    package req nx
    nx::Object create o {
      set :x 100
      # The following line is tricky: the trailing ";#" 
      # is used to trim the undesirable extra arguments 
      # from the trace command.
      ::trace add variable :x unset "[list set ::X ${:x}];#"
    }
  }

  ? {$i eval {info exists ::X}} 0
  $i eval {::nsf::finalize -keepvars}
  ? {$i eval {info exists ::X}} 1
  ? {$i eval {set ::X}} 100

  interp delete $i
  unset i
}

nx::test case unset-traces-during-cleanup-with-destroy {
  #
  # Make sure that a very-late destroy (in the unset trace) does not
  # fire ... and does not cause any side effects.
  #
  global i
  set i [interp create]
  $i eval {
    package req nx
    nx::Object create o {
      :public object method destroy args {
	incr ::X
	next
      } 
      set :x 100
      # The following line is tricky: the trailing ";#" 
      # is used to trim the undesirable extra arguments 
      # from the trace command.
      ::trace add variable :x unset "[list ::incr ::X]; [list [self] destroy];#"
    }
  }

  ? {$i eval {info exists ::X}} 0
  $i eval {::nsf::finalize -keepvars}
  ? {$i eval {info exists ::X}} 1
  ? {$i eval {set ::X}} 2

  interp delete $i
  unset i
}

nx::test case unset-traces-during-cleanup-with-destroy-2 {
  #
  # We are safe when trying to delete the base class/metaclass ...
  #
  global i
  set i [interp create]
  $i eval {
    package req nx
    nx::Object create o {
      set :x _
      # The following line is tricky: the trailing ";#" 
      # is used to trim the undesirable extra arguments 
      # from the trace command.
      ::trace add variable :x unset "[list catch [list ::nx::Object destroy] msg1]; [list catch [list ::nx::Class destroy] msg2]; set ::MSG \[list \$msg1 \$msg2\];#"
    }
  }
  
  ? {$i eval {info exists ::MSG}} 0
  $i eval {::nsf::finalize -keepvars}
  ? {$i eval {info exists ::MSG}} 1
  ? {$i eval {set ::MSG}} [list "cannot destroy base class ::nx::Object" "cannot destroy base class ::nx::Class"]

  interp delete $i
  unset i
}

nx::test case unset-traces-during-cleanup-with-reset {
  #
  # Check for leaks ...
  #
  global i
  set i [interp create]
  $i eval {
    package req nx
    nx::Object create o {
      set :x 100
      # The following line is tricky: the trailing ";#" 
      # is used to trim the undesirable extra arguments 
      # from the trace command.
      ::trace add variable :x unset "[list ::nsf::var::set [self] x ${:x}];#"
    }
  }

  ? {$i eval {::nsf::object::exists ::o}} 1
  ? {$i eval {info commands ::o}} ::o
  $i eval {::nsf::finalize -keepvars}
  ? {$i eval {info commands ::o}} ""

  interp delete $i
  unset i
}

#
# Test on UnsetTracedVars() under revived vars & unset traces
# for per-object variable tables (no namespace involved).
#
nx::test case unset-traces-during-cleanup-with-reset-2 {
  global i
  set i [interp create]
  $i eval {
    package req nx
    set ::called(reset) 0
    proc ::reset {obj var value} {
      #
      # ... revive object variable 'x' and add yet another unset trace
      #
      ::nsf::var::set $obj $var $value
      $obj eval [list ::trace add variable :$var unset \
                     "incr ::called(reset); ::reset $obj $var $value; #"]
    }
    nx::Object create ::o 
    ::reset ::o x 100
  }
  ? {$i eval {::nsf::object::exists ::o}} 1
  ? {$i eval {info commands ::o}} ::o
  ? {$i eval {namespace exists ::o}} 0
  ? {$i eval {set ::called(reset)}} 0
  $i eval {::nsf::finalize -keepvars}
  ? {$i eval {info commands ::o}} ""
  ? {$i eval {namespace exists ::o}} 0
 # ? {$i eval {set ::called(reset)}} 1; # unset trace, also re-registered, is only called once!

  interp delete $i
  unset i
}

#
# Test on UnsetTracedVars() under revived vars & unset traces
# for namespaced variable tables.
#
nx::test case unset-traces-during-cleanup-with-reset-3 {
  global i
  set i [interp create]
  $i eval {
    package req nx
    set ::called(reset) 0
    proc ::reset {obj var value} {
      #
      # ... revive object variable 'x' and add yet another unset trace
      #
      ::set ${obj}::$var $value
      $obj eval [list ::trace add variable ${obj}::$var unset \
                     "incr ::called(reset); ::reset $obj $var $value; #"]
    }
    nx::Object create ::o {
      :require namespace
    }
    ::reset ::o x 100
  }
  ? {$i eval {::nsf::object::exists ::o}} 1
  ? {$i eval {info commands ::o}} ::o
  ? {$i eval {namespace exists ::o}} 1
  ? {$i eval {set ::called(reset)}} 0
  $i eval {::nsf::finalize -keepvars}
  ? {$i eval {info commands ::o}} ""
  ? {$i eval {namespace exists ::o}} 0
  ? {$i eval {set ::called(reset)}} 1; # unset trace, also re-registered, is only called once!

  interp delete $i
  unset i
}



#
# Exercise renaming of cmds which are used as methods
#
nx::test case rename-cached-method {
  # Create a class with a namespace
  nx::Class create A {:public object method foo args {}}
  #
  # Add a proc named "new" to the namespace of the class.
  # This is not recommended, but we can't avoid it.
  #
  proc ::A::new {} { return "something from A" }
  #
  # Call the proc via the method interface. After the call the cmd is
  # cached in the Tcl_Obj "new".
  #
  ? {A new} "something from A"
  #
  # Delete the proc. The rename command has to take care, that the
  # cached cmd has to be invalidated.
  #
  rename ::A::new ""
  
  #
  # We expect that the original method works again.
  #
  ? {string match ::nsf::__#* [A new]} 1

  # 
  # Now try the same with the internal namespace from nsf.  Messing
  # around there is even less wanted, but still, we can't avoid this.
  # We make first a backup of the method.
  #
  rename ::nsf::classes::nx::Class::new ::nsf::classes::nx::Class::new.orig
  proc ::nsf::classes::nx::Class::new {} { return "something" }
  
  ? {A new} "something"
  #
  # Delete the proc and call "new" again
  #
  rename ::nsf::classes::nx::Class::new ""
  ? {A new} "method 'new' unknown for ::A; in order to create an instance of class ::A, consider using '::A create new ?...?'"
  
  #
  # Restore the original state
  #
  rename ::nsf::classes::nx::Class::new.orig ::nsf::classes::nx::Class::new
  #
  # We expect that the original method works again.
  #
  ? {string match ::nsf::__#* [A new]} 1
}

#
# Create a cyclical class dependency and delete it manually
#
nx::test case cyclical-dependency {
  nx::Object create o1
  ? {nx::Class create o1::C} ::o1::C
  ? {nsf::relation::set o1 class o1::C} ::o1::C
  o1 destroy
}

#
# Create a cyclical class dependency and let it be deleted on
# object-system-cleanup
#
nx::Object create o1
nx::Class create o1::C
nsf::relation::set o1 class o1::C

#
# Create a cyclical superclass dependency and delete it manually
#
nx::test case cyclical-dependency {
  nx::Class create C 
  nx::Class create C::*
  ? {nsf::relation::set C superclass {C::* nx::Object}} ""
  C destroy
}

#
# Create a cyclical superclass dependency and let it be deleted on
# object-system-cleanup
#
nx::Class create C 
nx::Class create C::*
nsf::relation::set C superclass {C::* nx::Object}

puts "==== EXIT [info script]"
#
# Local variables:
#    mode: tcl
#    tcl-indent-level: 2
#    indent-tabs-mode: nil
# End: