/
objects.t
1774 lines (1422 loc) · 40.7 KB
/
objects.t
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
#!./parrot
# Copyright (C) 2001-2010, Parrot Foundation.
=head1 NAME
t/oo/objects.t - Objects
=head1 SYNOPSIS
% prove t/oo/objects.t
=head1 DESCRIPTION
Tests the object/class subsystem.
=cut
.sub main :main
.include 'test_more.pir'
.include "iglobals.pasm"
.include "interpinfo.pasm"
plan(191)
get_classname_from_class()
test_get_class()
test_isa()
does_scalar()
does_array()
new_object()
new_object__isa_test()
new_object__classname()
isa_subclass()
isa_subclass__objects()
test_addmethod()
test_addattribute()
addattribute_subclass()
addattribute_subclass__same_name()
set_and_get_object_attribs()
set_and_get_multiple_object_attribs()
attribute_values_are_specific_to_objects()
attribute_values_and_subclassing()
attribute_values_and_subclassing_2()
PMC_as_classes__overridden_mmd_methods()
typeof_class()
typeof_objects()
multiple_inheritance__with_attributes()
attributes_two_levels_of_inheritance()
class_op_test()
anon_subclass_has_no_name()
get_attrib_by_name()
get_attrib_by_name_subclass()
set_attrib_by_name_subclass()
PMC_as_classes()
PMC_as_classes__subclass()
PMC_as_classes__instantiate()
PMC_as_classes__methods()
PMC_as_classes__mmd_methods()
PMC_as_classes__derived_1()
PMC_as_classes__derived_2()
PMC_as_classes__derived_3()
subclassing_Class()
namespace_vs_name()
multiple_anon_classes()
subclassed_Integer_bug()
equality_of_subclassed_Integer()
short_name_attributes()
init_with_and_without_arg()
newclass_bracket_parsing()
verify_namespace_types()
verify_data_type()
new_keyed()
new_keyed_2()
new_keyed_3()
subclass_keyed()
test_class_name_multipart_name()
test_get_class_multipart_name()
isa_bug()
new_nested_ordering()
vtable_override_once_removed()
vtable_fails_for_subclasses_of_core_classes()
super___init_called_twice()
using_class_object_from_typeof_op_with_new()
setting_non_existent_attribute()
setting_non_existent_attribute_by_name()
getting_null_attribute()
getting_non_existent_attribute()
addparent_exceptions_1()
addparent_exceptions_2()
subclassing_a_non_existent_class()
anon_subclass_of_non_existent_class()
addattribute_duplicate()
wrong_way_to_create_new_objects()
attribute_values__subclassing_access_meths()
attribute_values__inherited_access_meths()
# END_OF_TESTS
.end
.sub get_classname_from_class
newclass $P1, "Foo5"
set $S0, $P1
is( $S0, "Foo5", "got classname Foo5" )
subclass $P2, $P1, "Bar5"
set $S1, $P2
is( $S1, "Bar5", "got subclass Bar5" )
subclass $P3, "Foo5", "Baz5"
set $S2, $P3
is( $S2, "Baz5", "got subclass Baz5" )
.end
.sub test_get_class
newclass $P1, "Foo6"
get_class $P2, "Foo6"
set $S2, $P2
is( $S2, "Foo6", 'get_class for Foo6' )
subclass $P3, $P1, "FooBar6"
get_class $P4, "FooBar6"
set $S4, $P4
is( $S4, 'FooBar6', 'get_class for FooBar6' )
get_class $P3, "NoSuch6"
isnull $I0, $P3
ok( $I0, "no class for 'NoSuch6'" )
.end
.sub test_isa
new $P1, ['Boolean']
isa $I0, $P1, "Boolean"
is( $I0, 1, 'Boolean isa Boolean' )
isa $I0, $P1, "Bool"
is( $I0, 0, 'Boolean !isa Bool' )
isa $I0, $P1, "scalar"
is( $I0, 1, 'Boolean isa scalar' )
isa $I0, $P1, "calar"
is( $I0, 0, 'Boolean !isa calar' )
isa $I0, $P1, " "
is( $I0, 0, 'Boolean !isa " "' )
isa $I0, $P1, ""
is( $I0, 0, 'Boolean !isa ""' )
null $S0
isa $I0, $P1, $S0
is( $I0, 0, 'Boolean !isa null $S0' )
set $S0, "scalar"
isa $I0, $P1, $S0
is( $I0, 1, 'Boolean isa scalar $S0' )
.end
.sub does_scalar
new $P1, ['Boolean']
does $I0, $P1, "Boolean"
is( $I0, 0, 'Boolean !does Boolean' )
does $I0, $P1, "Bool"
is( $I0, 0, 'Boolean !does Bool' )
does $I0, $P1, "scalar"
is( $I0, 1, 'Boolean does scalar' )
.end
.sub does_array
new $P1, ['OrderedHash']
does $I0, $P1, "Boolean"
is( $I0, 0, 'OrderedHash !does Boolean' )
does $I0, $P1, "Bool"
is( $I0, 0, 'OrderedHash !does Bool' )
does $I0, $P1, "hash"
is( $I0, 1, 'OrderedHash does hash' )
does $I0, $P1, "array"
is( $I0, 1, 'OrderedHash does array' )
.end
.sub new_object
newclass $P1, "Foo7"
new $P2, "Foo7"
ok( 1, 'created new object from Foo7 class' )
.end
.sub new_object__isa_test
newclass $P1, "Foo8"
new $P2, $P1
ok( 1, 'created new object from Foo8 class' )
isa $I0, $P2, "Foo8"
ok( $I0, 'new object isa Foo8' )
.end
.sub new_object__classname
newclass $P1, "Foo9"
new $P2, $P1
set $S0, $P1 # class
is( $S0, "Foo9", 'new object from Foo9 class as a string is Foo9' )
typeof $S0, $P2 # object
is( $S0, 'Foo9', 'typeof obj is Foo9' )
class $P3, $P1
set $S0, $P1 # class
is( $S0, 'Foo9', 'class of obj is Foo9' )
typeof $S0, $P2 # object
is( $S0, 'Foo9', 'typeof obj is Foo9' )
.end
.sub isa_subclass
newclass $P1, "Foo10"
subclass $P2, $P1, "Bar10"
isa_ok( $P2, "Foo10", 'newclass isa Foo10' )
isa_ok( $P2, "Bar10", 'new subclass isa Bar10' )
isa_ok( $P2, "Foo10", 'new subclass isa parent' )
isa_ok( $P2, "Class", 'new subclass isa Class' )
isa $I0, $P2, "Object"
is( $I0, 0, 'new subclass !isa Object' )
.end
.sub isa_subclass__objects
newclass $P3, "Foo30"
subclass $P4, $P3, "Bar30"
$P1 = $P3.'new'()
$P2 = $P4.'new'()
isa_ok( $P1, "Foo30", 'obj isa its class' )
isa_ok( $P2, "Bar30", 'obj isa its class' )
isa_ok( $P2, "Foo30", 'obj isa its parent class' )
isa_ok( $P2, "Object", 'obj isa Object' )
isa_ok( $P2, "Class", 'obj isa Class' )
.end
.sub test_addmethod
newclass $P0, 'Foo31'
$P2 = get_hll_global 'sayFoo31'
# add a method BEFORE creating a Foo object
addmethod $P0, 'foo31', $P2
$P1 = new 'Foo31'
$P1.'foo31'()
# get a method from some other namespace
$P2 = get_hll_global ['Bar31'], 'sayBar31'
# add a method AFTER creating the object
addmethod $P0, 'bar31', $P2
$P1.'bar31'()
.end
.sub sayFoo31
ok( 1, 'called method added before creating obj' )
.end
.namespace ['Bar31']
.sub sayBar31
ok( 1, 'called method added after created obj' )
.end
.namespace [] # Reset to root namespace for next test
.sub test_addattribute
newclass $P1, "Foo11"
addattribute $P1, "foo_i"
ok( 1, 'addattribute did not blow up' )
set $S0, $P1
is( $S0, "Foo11", '$P1 is still the same class as PMC' )
# Check that we can add multiple attributes
set $I0, 0
l1:
set $S0, $I0
addattribute $P1, $S0
inc $I0
lt $I0, 1000, l1
ok( 1, 'addattribute 1000x without blow up' )
.end
.sub addattribute_subclass
newclass $P1, "Foo12"
addattribute $P1, "foo_i"
ok( 1, 'addattribute to Foo12' )
subclass $P2, $P1, "Bar12"
addattribute $P2, "bar_i"
ok( 1, 'addattribute to subclass of Foo12' )
.end
.sub addattribute_subclass__same_name
newclass $P1, "Foo32"
addattribute $P1, "i"
addattribute $P1, "j"
subclass $P2, $P1, "Bar32"
addattribute $P2, "j"
addattribute $P2, "k"
ok( 1, 'created class and subclass and added attributes' )
.local pmc o
o = $P2.'new'()
$P0 = getattribute o, 'i'
is( $P0, 'Foo32.i', 'parent attrib initialized in init' )
$P0 = getattribute o, ['Foo32'], 'j'
is( $P0, 'Foo32.j', 'parent attrib initialized in init' )
$P0 = getattribute o, ['Bar32'], 'j'
is( $P0, 'Bar32.j', 'subclass attrib initialized in init' )
$P0 = getattribute o, 'k'
is( $P0, 'Bar32.k', 'subclass attrib initialized in init' )
$P0 = getattribute o, 'i'
is( $P0, 'Foo32.i', 'parent attrib init-ed' )
$P0 = getattribute o, ['Foo32'], "j"
is( $P0, 'Foo32.j', 'parent attrib init-ed' )
$P0 = getattribute o, 'j'
is( $P0, 'Bar32.j', 'subclass attrib returned over parent' )
$P0 = getattribute o, 'k'
is( $P0, 'Bar32.k', 'subclass attrib init-ed' )
.end
.namespace ['Bar32']
.sub init :vtable :method
$P0 = new ['String']
$P0 = 'Foo32.i'
setattribute self, ['Foo32'], "i", $P0
$P0 = new ['String']
$P0 = 'Foo32.j'
setattribute self, ["Foo32"], "j", $P0
$P0 = new ['String']
$P0 = 'Bar32.j'
setattribute self, ["Bar32"], "j", $P0
$P0 = new ['String']
$P0 = 'Bar32.k'
setattribute self, ["Bar32"], "k", $P0
.end
.namespace [] # Reset to root namespace for next test
.sub set_and_get_object_attribs
newclass $P1, "Foo13"
addattribute $P1, "i"
new $P2, $P1
new $P3, ['Integer']
set $P3, 1024
setattribute $P2, "i", $P3
new $P4, ['Integer']
getattribute $P4, $P2, "i"
is( $P4, 1024, 'set/get Integer attribute' )
.end
.sub set_and_get_multiple_object_attribs
newclass $P1, "Foo14"
addattribute $P1, "i"
addattribute $P1, "j"
new $P2, "Foo14"
new $P3, ['Integer']
set $P3, 4201
new $P4, ['Hash']
set $P4["Key"], "Value"
setattribute $P2, "i", $P3
setattribute $P2, "j", $P4
getattribute $P5, $P2, "i"
is( $P5, '4201', 'set/get Integer attribute' )
getattribute $P6, $P2, "j"
set $S0, $P6["Key"]
is( $S0, 'Value', 'set/get Hash attribute on same obj' )
.end
.sub attribute_values_are_specific_to_objects
newclass $P1, "Foo15"
addattribute $P1, "i"
new $P2, $P1
new $P3, $P1
new $P4, ['Integer']
set $P4, 100
setattribute $P2, "i", $P4
new $P5, ['String']
set $P5, "One hundred"
setattribute $P3, "i", $P5
getattribute $P6, $P2, "i"
is( $P6, 100, 'attribute value on 1st object is specific to obj' )
getattribute $P6, $P3, "i"
is( $P6, 'One hundred', 'attribute value on 2nd obj is specific to obj' )
.end
.sub attribute_values_and_subclassing
newclass $P1, "Foo16"
addattribute $P1, "i"
addattribute $P1, "j"
subclass $P2, $P1, "Bar16"
addattribute $P2, "k"
addattribute $P2, "l"
new $P2, "Bar16"
new $P3, "Bar16"
# Note that setattribute holds the actual PMC, not a copy, so
# in this test both attributes get the PMC from $P4, and should
# both have the same value, despite the C<inc>.
new $P4, ['Integer']
set $P4, 10
setattribute $P2, "i", $P4
inc $P4
setattribute $P2, "j", $P4
new $P5, ['Integer']
set $P5, 100
setattribute $P3, "i", $P5
inc $P5
setattribute $P3, "j", $P5
getattribute $P6, $P2, "i"
is( $P6, 11, 'setattrib with a PMC holds actual PMC not copy' )
getattribute $P6, $P2, "j"
is( $P6, 11, '...so changes to the PMC appear through the attrib' )
getattribute $P6, $P3, "i"
is( $P6, 101, '...and second test on new objects' )
getattribute $P6, $P3, "j"
is( $P6, 101, '...should have same result' )
.end
.sub attribute_values_and_subclassing_2
newclass $P1, "Foo17"
# must add attributes before object instantiation
addattribute $P1, ".i"
addattribute $P1, ".j"
subclass $P2, $P1, "Bar17"
addattribute $P2, ".k"
addattribute $P2, ".l"
# subclass is preferred for the SI case over
# newclass $P2, "Bar"
# addattrib ...
# addparent $P2, $P1
# which is suitable for adding multiple parents to one class
# instantiate a Bar object
new $P3, "Bar17"
# Set the attribute values
new $P10, ['String'] # set attribute values
set $P10, "i" # attribute slots have reference semantics
setattribute $P3, ".i", $P10 # so always put new PMCs in
# if you have unique values
new $P10, ['String']
set $P10, "j"
setattribute $P3, ".j", $P10
new $P10, ['String']
set $P10, "k"
setattribute $P3, ".k", $P10
new $P10, ['String']
set $P10, "l"
setattribute $P3, ".l", $P10
# retrieve attribs
getattribute $P11, $P3, ".i"
is( $P11, "i", 'string attribute get/set on parent' )
getattribute $P11, $P3, ".j"
is( $P11, "j", 'string attribute get/set on parent' )
getattribute $P11, $P3, ".k"
is( $P11, "k", 'string attribute get/set on subclass' )
getattribute $P11, $P3, ".l"
is( $P11, "l", 'string attribute get/set on subclass' )
.end
.sub PMC_as_classes__overridden_mmd_methods
.local pmc myint, i, j, k
get_class $P0, "Integer"
subclass myint, $P0, "MyInt1"
i = new 'MyInt1'
j = new 'MyInt1'
k = new 'MyInt1'
i = 6
j = 7
k = i + j
is( k, 13, 'added two MyInt1' )
j = new ['Integer']
j = 100
k = i + j
is( k, 106, 'added MyInt1 and an Integer' )
.end
.namespace ["MyInt1"]
.sub add :multi(MyInt1, MyInt1, MyInt1)
.param pmc self
.param pmc right
.param pmc dest
ok( 1, 'in the add method' )
$P0 = getattribute self, ['Integer'], "proxy"
$I0 = $P0
$I1 = right
$I2 = $I0 + $I1
dest = $I2
.return(dest)
.end
.namespace [] # Reset to root namespace for next test
.sub typeof_class
newclass $P0, "Foo21"
typeof $S0, $P0
is( $S0, "Class", 'typeof for a Class PMC is "Class"' )
.end
.sub typeof_objects
newclass $P0, "A"
newclass $P1, "B"
new $P0, ['A']
new $P1, ['B']
typeof $S0, $P0
typeof $S1, $P1
is( $S0, 'A', 'typeof object of class A is "A"' )
is( $S1, 'B', 'typeof object of class B is "B"' )
.end
.sub multiple_inheritance__with_attributes
newclass $P1, "Star"
addattribute $P1, "Spectral Type"
newclass $P2, "Company"
addattribute $P2, "Annual Profit"
subclass $P3, $P1, "Sun"
addparent $P3, $P2
new $P4, ['Sun']
new $P5, ['String']
set $P5, "G"
setattribute $P4, "Spectral Type", $P5
new $P6, ['String']
set $P6, "$100,000,000"
setattribute $P4, "Annual Profit", $P6
getattribute $P7, $P4, "Spectral Type"
is( $P7, 'G', 'direct parents attribute' )
getattribute $P8, $P4, "Annual Profit"
is( $P8, '$100,000,000', "addparent's attribute" )
.end
.sub attributes_two_levels_of_inheritance
newclass $P0, "Astronomical Object"
addattribute $P0, "Location"
subclass $P1, $P0, "Star2"
addattribute $P1, "Spectral Type"
newclass $P2, "Sun2"
addparent $P2, $P1
addparent $P2, $P0
new $P4, "Sun2"
new $P5, ['String']
set $P5, "Taurus"
setattribute $P4, "Location", $P5
getattribute $P6, $P4, "Location"
is( $P6, 'Taurus', 'attributes with two levels of inheritance' )
.end
.sub class_op_test
newclass $P0, "City1"
new $P1, "City1"
class $P2, $P1
set $S0, $P2
is( $S0, 'City1', 'class op works' )
.end
.sub anon_subclass_has_no_name
newclass $P0, "City2"
subclass $P1, $P0
set $S0, $P1
is( $S0, '', 'anonymous subclass has no name' )
.end
.sub get_attrib_by_name
newclass $P1, "Foo18"
addattribute $P1, "i"
new $P2, "Foo18"
new $P3, ['String']
set $P3, "ok"
setattribute $P2, "i", $P3
getattribute $P4, $P2, ["Foo18"], "i"
is( $P4, 'ok', 'get attrib by name' )
.end
.sub get_attrib_by_name_subclass
newclass $P0, "Bar19"
addattribute $P0, "j"
subclass $P1, $P0, "Foo19"
addattribute $P1, "i"
new $P2, "Foo19"
new $P3, ['String']
set $P3, "foo i"
setattribute $P2, "i", $P3
new $P3, ['String']
set $P3, "bar j"
setattribute $P2, "j", $P3
getattribute $P4, $P2, ["Foo19"], "i"
is( $P4, 'foo i', 'attribute from subclass get by name' )
getattribute $P4, $P2, ["Bar19"], "j"
is( $P4, 'bar j', 'attribute from parent class get by name' )
.end
.sub set_attrib_by_name_subclass
newclass $P0, "Bar20"
addattribute $P0, "j"
subclass $P1, $P0, "Foo20"
addattribute $P1, "i"
new $P2, "Foo20"
new $P3, ['String']
set $P3, "foo i"
setattribute $P2, ["Foo20"], "i", $P3
new $P3, ['String']
set $P3, "bar j"
setattribute $P2, ["Bar20"], "j", $P3
getattribute $P4, $P2, "i"
is( $P4, 'foo i', 'attribute from subclass set by name' )
getattribute $P4, $P2, "j"
is( $P4, 'bar j', 'attribute from parent class set by name' )
.end
.sub PMC_as_classes
get_class $P0, "Integer"
ok( 1, "get_class of Integer didn't croak" )
get_class $P0, "Integer"
ok( 1, "get_class of Integer didn't croak second time" )
typeof $S0, $P0
is( $S0, 'PMCProxy', 'typeof PMCProxy' )
.end
.sub PMC_as_classes__subclass
.local pmc MyInt3
get_class $P0, "Integer"
ok( 1, "get_class on Integer didn't blow up" )
subclass MyInt3, $P0, "MyInt3"
ok( 1, "subclassing didn't blow up" )
$S0 = typeof MyInt3
is( $S0, 'Class', 'new subclass is typeof Class' )
$I0 = isa MyInt3, "MyInt3"
ok( $I0, 'new subclass isa MyInt' )
$I0 = isa MyInt3, "Integer"
ok( $I0, 'new subclass isa parent class' )
.end
.sub PMC_as_classes__instantiate
.local pmc MyInt4
get_class $P0, "Integer"
ok( 1, 'able to get_class of Integer' )
subclass MyInt4, $P0, "MyInt4"
addattribute MyInt4, ".i"
ok( 1, 'able to addattribute to subclass' )
.local pmc i
i = new "MyInt4"
ok( 1, 'able to instantiate obj of subclass w/ attribute' )
.end
.sub PMC_as_classes__methods
.local pmc MyInt5
get_class $P0, "Integer"
subclass MyInt5, $P0, "MyInt5"
addattribute MyInt5, "intval"
.local pmc i, i2
i = new "MyInt5"
i2 = new ['Integer']
i2 = 43
i = 42 # set_integer is inherited from Integer
ok( 1, 'able to assign int to MyInt' )
$I0 = i # get_integer is overridden below
is( $I0, 42, 'get_integer is overridden for MyInt5' )
$S0 = i # get_string is overridden below
is( $S0, 'MyInt5(42)', 'get_string is overridden for MyInt5' )
.end
.namespace ["MyInt5"]
.sub set_integer_native :vtable :method
.param int new_value
$P1 = new ['Integer']
$P1 = new_value
setattribute self, "intval", $P1
.end
.sub get_integer :vtable :method
$P0 = getattribute self, "intval"
$I0 = $P0
.return ($I0)
.end
.sub get_string :vtable :method
$P0 = getattribute self, "intval"
$I0 = $P0
$S1 = $I0
$S0 = "MyInt5("
$S0 .= $S1
$S0 .= ")"
.return ($S0)
.end
.namespace [] # Reset to root namespace for next test
.sub PMC_as_classes__mmd_methods
.local pmc MyInt6
get_class $P0, "Integer"
subclass MyInt6, $P0, "MyInt6"
.local pmc i
.local pmc j
.local pmc k
i = new "MyInt6"
j = new "MyInt6"
k = new "MyInt6"
i = 6
j = 7
k = i * j
$I0 = k
is( $I0, 42, 'MyInt6 defaults to Integer class for mult' )
$S0 = k # get_string is overridden below
is( $S0, 'MyInt6(42)', 'get_string is overridden for MyInt6' )
.end
.namespace ["MyInt6"]
.sub get_string :vtable :method
$I0 = self # get_integer is not overridden
$S1 = $I0
$S0 = "MyInt6("
$S0 .= $S1
$S0 .= ")"
.return ($S0)
.end
.namespace [] # Reset to root namespace for next test
.sub PMC_as_classes__derived_1
.local pmc MyInt8
.local pmc MyInt8_2
get_class $P0, "Integer"
subclass MyInt8, $P0, "MyInt8"
addattribute MyInt8, 'intval'
get_class $P1, "MyInt8"
subclass MyInt8_2, $P1, "MyInt8_2"
.local pmc i
i = new "MyInt8_2"
$I0 = isa i, "Integer"
ok( $I0, 'obj isa grandparent (Integer)' )
$I0 = isa i, "MyInt8"
ok( $I0, 'obj isa parent (MyInt8)' )
$I0 = isa i, "MyInt8_2"
ok( $I0, 'obj isa its class (MyInt8_2)' )
i = 42 # set_integer is overridden below
$I0 = i # get_integer is overridden below
is( $I0, 42, 'set/get_integer overridden' )
$S0 = i # get_string is overridden below
is( $S0, 'MyInt8_2(42)', 'set/get_string overridden' )
.end
.namespace ["MyInt8"]
.sub 'set_integer_native' :vtable :method
.param int val
$P1 = new ['Integer']
$P1 = val
setattribute self, "intval", $P1
.return ()
.end
.sub get_integer :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
.return ($I0)
.end
.sub get_string :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
$S1 = $I0
$S0 = typeof self
$S0 .= "("
$S0 .= $S1
$S0 .= ")"
.return ($S0)
.end
.namespace [] # Reset to root namespace for next test
.sub PMC_as_classes__derived_2
.local pmc MyInt9
.local pmc MyInt9_2
get_class $P0, "Integer"
subclass MyInt9, $P0, "MyInt9"
addattribute MyInt9, 'intval'
get_class $P1, "MyInt9"
subclass MyInt9_2, $P1, "MyInt9_2"
.local pmc i
i = new "MyInt9_2"
$I0 = isa i, "Integer"
ok( $I0, 'obj isa grandparent (Integer)' )
$I0 = isa i, "MyInt9"
ok( $I0, 'obj isa parent (MyInt9)' )
$I0 = isa i, "MyInt9_2"
ok( $I0, 'obj isa its class (MyInt9_2)' )
i = 42 # set_integer is overridden below
$I0 = i # get_integer is overridden below
is( $I0, 43, 'set/get_integer overridden' )
$S0 = i # get_string is overridden below
is( $S0, 'MyInt9_2(42)', 'set/get_string overridden' )
.end
.namespace ["MyInt9_2"]
# subclassing methods from MyInt9 is ok
# this one changes the value a bit
.sub get_integer :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
inc $I0 # <<<<<
.return ($I0)
.end
.namespace ["MyInt9"]
.sub 'set_integer_native' :vtable :method
.param int val
$P1 = new ['Integer']
$P1 = val
setattribute self, "intval", $P1
.return ()
.end
.sub get_integer :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
.return ($I0)
.end
.sub get_string :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
$S1 = $I0
$S0 = typeof self
$S0 .= "("
$S0 .= $S1
$S0 .= ")"
.return ($S0)
.end
.namespace [] # Reset to root namespace for next test
.sub PMC_as_classes__derived_3
.local pmc MyInt10
.local pmc MyInt10_2
get_class $P0, "Integer"
subclass MyInt10, $P0, "MyInt10"
addattribute MyInt10, 'intval'
get_class $P1, "MyInt10"
subclass MyInt10_2, $P1, "MyInt10_2"
.local pmc i
i = new "MyInt10_2"
$I0 = isa i, "Integer"
ok( $I0, 'obj isa grandparent (Integer)' )
$I0 = isa i, "MyInt10"
ok( $I0, 'obj isa parent (MyInt10)' )
$I0 = isa i, "MyInt10_2"
ok( $I0, 'obj isa its class (MyInt102)' )
i = 42 # set_integer is overridden below
$I0 = i # get_integer is overridden below
is( $I0, 42, 'set/get_integer overridden' )
$S0 = i # get_string is overridden below
is( $S0, 'MyInt10_2(42)', 'set/get_string overridden' )
.end
.namespace ["MyInt10_2"]
.sub get_integer :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
.return ($I0)
.end
.sub get_string :vtable :method
$P0 = getattribute self, 'intval'
$I0 = $P0
$S1 = $I0
$S0 = typeof self
$S0 .= "("
$S0 .= $S1
$S0 .= ")"
.return ($S0)
.end
.namespace ['MyInt10']
.sub 'set_integer_native' :vtable :method
.param int val
$P1 = new ['Integer']
$P1 = val
setattribute self, "intval", $P1
.return ()
.end
.namespace [] # Reset to root namespace for next test
.sub subclassing_Class
.local pmc cl
.local pmc parent
parent = get_class "Class"
cl = subclass parent, "Foo33"
ok( 1, 'able to subclass Class' )
.local pmc o
o = new "Foo33"
ok( 1, 'able to instantiate subclass of Class' )
$S0 = typeof o
is( $S0, 'Foo33', 'object returns correct class' )
.end
.sub namespace_vs_name
.local pmc o, cl, f
newclass cl, "Foo34"
o = new "Foo34"
is( o, 'Foo34::get_string', 'found Foo34 namespace' )