-
Notifications
You must be signed in to change notification settings - Fork 11
/
MirrorBuilderTesting.ns
961 lines (901 loc) · 35 KB
/
MirrorBuilderTesting.ns
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
Newspeak3
'Root'
class MirrorBuilderTesting usingPlatform: p testFramework: m = (|
private ClassDeclarationBuilder = p mirrors ClassDeclarationBuilder.
private ClassMirror = p mirrors ClassMirror.
private ObjectMirror = p mirrors ObjectMirror.
private MessageNotUnderstood = p kernel MessageNotUnderstood.
private Map = p collections Map.
private TestContext = m TestContext.
|) (
public class ClassDeclarationBuilderTests = TestContext () (
classFromSource: src <String> ^<Class> = (
^(ClassDeclarationBuilder fromSource: src) install applyToObject reflectee
)
conflictError = (
^Error
)
in: collection findMirrorNamed: name = (
collection do: [:mirror | mirror name = name ifTrue: [^mirror]].
^nil
)
syntaxError = (
^Error
)
public testAccessModifiersFromClass = (
| klass builder |
klass:: classFromSource:
'class Top = (| public slotA protected slotB private slotC slotD |)(
public class ClassA = ()()
protected class ClassB = ()()
private class ClassC = ()()
class ClassD = ()()
public methodA = ()
protected methodB = ()
private methodC = ()
methodD = ()
)'.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
assert: builder accessModifier equals: #public.
assert: (in: builder instanceSide slots findMirrorNamed: #slotA) accessModifier equals: #public.
assert: (in: builder instanceSide slots findMirrorNamed: #slotB) accessModifier equals: #protected.
assert: (in: builder instanceSide slots findMirrorNamed: #slotC) accessModifier equals: #private.
assert: (in: builder instanceSide slots findMirrorNamed: #slotD) accessModifier equals: #protected.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassA) accessModifier equals: #public.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassB) accessModifier equals: #protected.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassC) accessModifier equals: #private.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassD) accessModifier equals: #protected.
assert: (in: builder instanceSide methods findMirrorNamed: #methodA) accessModifier equals: #public.
assert: (in: builder instanceSide methods findMirrorNamed: #methodB) accessModifier equals: #protected.
assert: (in: builder instanceSide methods findMirrorNamed: #methodC) accessModifier equals: #private.
assert: (in: builder instanceSide methods findMirrorNamed: #methodD) accessModifier equals: #protected.
)
public testAccessModifiersFromSource = (
| builder |
builder:: ClassDeclarationBuilder fromSource:
'class Top = (| public slotA protected slotB private slotC slotD |)(
public class ClassA = ()()
protected class ClassB = ()()
private class ClassC = ()()
class ClassD = ()()
public methodA = ()
protected methodB = ()
private methodC = ()
methodD = ()
)'.
assert: builder accessModifier equals: #public.
assert: (in: builder instanceSide slots findMirrorNamed: #slotA) accessModifier equals: #public.
assert: (in: builder instanceSide slots findMirrorNamed: #slotB) accessModifier equals: #protected.
assert: (in: builder instanceSide slots findMirrorNamed: #slotC) accessModifier equals: #private.
assert: (in: builder instanceSide slots findMirrorNamed: #slotD) accessModifier equals: #protected.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassA) accessModifier equals: #public.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassB) accessModifier equals: #protected.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassC) accessModifier equals: #private.
assert: (in: builder instanceSide nestedClasses findMirrorNamed: #ClassD) accessModifier equals: #protected.
assert: (in: builder instanceSide methods findMirrorNamed: #methodA) accessModifier equals: #public.
assert: (in: builder instanceSide methods findMirrorNamed: #methodB) accessModifier equals: #protected.
assert: (in: builder instanceSide methods findMirrorNamed: #methodC) accessModifier equals: #private.
assert: (in: builder instanceSide methods findMirrorNamed: #methodD) accessModifier equals: #protected.
)
public testChangeObject = (
(* Object is an edge case for Primordial Soup (has nil superclass) and Newspeak-on-Squeak (has a Smalltalk superclass. *)
|
instance
builder <ClassDeclarationBuilder>
|
instance:: Object new.
should: [instance foo] signal: MessageNotUnderstood.
builder:: (ObjectMirror reflecting: instance) getClass mixin declaration asBuilder.
builder instanceSide methods addFromSource: 'public foo = (^1984)'.
builder install.
assert: instance foo equals: 1984.
builder instanceSide methods removeMirrorNamed: #foo.
builder install.
should: [instance foo] signal: MessageNotUnderstood.
)
public testChangeSmallInteger = (
(* SmallInteger is an edge case for Primordial Soup because it has a fixed CID. *)
|
instance
builder <ClassDeclarationBuilder>
|
instance:: 0.
should: [instance foo] signal: MessageNotUnderstood.
builder:: (ObjectMirror reflecting: instance) getClass mixin declaration asBuilder.
builder instanceSide methods addFromSource: 'public foo = (^1984)'.
builder install.
assert: instance foo equals: 1984.
builder instanceSide methods removeMirrorNamed: #foo.
builder install.
should: [instance foo] signal: MessageNotUnderstood.
)
public testClassDeclAddMethod = (
(* add a new method *)
|
klass <Class>
instance
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class EmptyClass = ()()'.
instance:: klass new.
should: [instance foo: 3] signal: MessageNotUnderstood.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide methods addFromSource: 'public foo: x = (^x * x)'.
should: [instance foo: 3] signal: MessageNotUnderstood.
builder install.
assert: (instance foo: 3) equals: 9.
)
public testClassDeclAddNestedClass = (
(* add a new nested class decl *)
|
klass <Class>
instance
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class EmptyClass = ()()'.
instance:: klass new.
should: [instance Nested] signal: MessageNotUnderstood.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide nestedClasses addFromSource: 'public class Nested = ()( public foo = (^91) )'.
should: [instance Nested] signal: MessageNotUnderstood.
builder install.
assert: instance Nested new foo equals: 91.
)
public testClassDeclAddSlot = (
(* add a new slot *)
|
klass <Class>
instance <Object>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class EmptyClass = ()()'.
instance:: klass new.
should: [instance newSlot] signal: MessageNotUnderstood.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder header source: 'class EmptyClass = ( | public newSlot ::= 91. | )'.
should: [instance newSlot] signal: MessageNotUnderstood.
builder install.
instance:: klass new.
assert: instance newSlot equals: 91.
instance newSlot: 101.
assert: instance newSlot equals: 101.
)
public testClassDeclCopyNested = (
(* The bug here seems to be the copy doesn't get its qualified name patched. *)
|
topBuilder <ClassDeclarationBuilder>
topClass
origBuilder
origMirror
copyBuilder
copySource
copyMirror
topMirror
|
topBuilder:: ClassDeclarationBuilder fromSource: 'class Top = ()()'.
topClass:: topBuilder install applyToObject reflectee.
origBuilder:: topBuilder instanceSide nestedClasses addFromSource: 'class TheOriginal = ()()'.
origMirror:: origBuilder install.
assert: origMirror name equals: 'TheOriginal'.
assert: origMirror accessModifier equals: #protected.
copyBuilder:: ClassDeclarationBuilder fromSource: origMirror source.
copyBuilder header name: 'TheCopy'.
copySource:: copyBuilder source.
copyBuilder:: topBuilder instanceSide nestedClasses addFromSource: copySource.
copyMirror:: copyBuilder install.
assert: copyMirror name equals: 'TheCopy'.
assert: copyMirror accessModifier equals: #protected.
assert: origMirror name equals: 'TheOriginal'. (* Unchanged *)
assert: origMirror accessModifier equals: #protected.
topMirror:: (ClassMirror reflecting: topClass) mixin declaration.
(* Both nested classes are present. *)
assert: (in: topMirror instanceSide nestedClasses findMirrorNamed: #TheOriginal) accessModifier equals: #protected.
assert: (in: topMirror instanceSide nestedClasses findMirrorNamed: #TheCopy) accessModifier equals: #protected.
)
public testClassDeclCopyTopLevel = (
|
origBuilder
origMirror
origClass
copyBuilder
copyMirror
copyClass
|
origBuilder:: ClassDeclarationBuilder fromSource: 'class TheOriginal = ()()'.
origClass:: origBuilder install applyToObject reflectee.
origMirror:: (ClassMirror reflecting: origClass) mixin declaration.
assert: origMirror name equals: 'TheOriginal'.
assert: origMirror accessModifier equals: #public.
copyBuilder:: ClassDeclarationBuilder fromSource: origMirror source.
copyBuilder header name: 'TheCopy'.
copyClass:: copyBuilder install applyToObject reflectee.
copyMirror:: (ClassMirror reflecting: copyClass) mixin declaration.
assert: copyMirror name equals: 'TheCopy'.
assert: copyMirror accessModifier equals: #public.
assert: origMirror name equals: 'TheOriginal'. (* Unchanged *)
assert: origMirror accessModifier equals: #public.
)
public testClassDeclModifySlot = (
(* modify the declaration of an existing slot *)
|
klass <Class>
instance <Object>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclModifySlot = ( | public x ::= 117. public y = 0. public z | )()'.
instance:: klass new.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder header source: 'class TestClassDeclModifySlot = ( | public x ::= 120. public y = 0. public z | )'.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
builder install.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
instance:: klass new.
assert: instance x equals: 120.
assert: instance y equals: 0.
assert: instance z equals: nil.
)
public testClassDeclRemoveMethod = (
(* remove an existing method *)
|
klass <Class>
instance
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclRemoveMethod = ()(
public foo = (^91)
public bar = (^42)
)'.
instance:: klass new.
assert: instance foo equals: 91.
assert: instance bar equals: 42.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide methods removeMirrorNamed: #foo.
assert: instance foo equals: 91.
assert: instance bar equals: 42.
builder install.
should: [instance foo] signal: MessageNotUnderstood.
assert: instance bar equals: 42.
)
public testClassDeclRemoveNestedClass = (
(* remove an existing nested class *)
|
klass <Class>
instance
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclRemoveNestedClass = ()(
public class Nested = ()() : ( public foo = (^70) )
)'.
instance:: klass new.
assert: instance Nested foo equals: 70.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide nestedClasses removeMirrorNamed: #Nested.
assert: instance Nested foo equals: 70.
builder install.
should: [instance Nested] signal: MessageNotUnderstood.
)
public testClassDeclRemoveNonExistentMethod = (
(* remove a non-existing method. The effect should be to do nothing, so our only goal here is to run without error *)
|
klass <Class>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclRemoveMethod = ()(
foo = (^91)
bar = (^42)
)'.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide methods removeMirrorNamed: #baz.
)
public testClassDeclRemoveNonExistentNestedClass = (
(* remove a non-existing nested class. The effect should be to do nothing, so our only goal here is to run without error *)
|
klass <Class>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclRemoveNonExistantNestedClass = ()()'.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide nestedClasses removeMirrorNamed: #Nested.
)
public testClassDeclRemoveSlot = (
(* remove an existing slot *)
|
klass <Class>
instance
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclRemoveSlot = ( | public x ::= 117. public y = 0. public z | )()'.
instance:: klass new.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder header source: 'class TestClassDeclRemoveSlot = ( | public x ::= 117. public y = 0. | )'.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
builder install.
assert: instance x equals: 117.
assert: instance y equals: 0.
should: [instance z] signal: MessageNotUnderstood.
)
public testClassDeclRenameNested = (
|
enclosingBuilder <ClassDeclarationBuilder>
enclosingClass
cls
builder
|
enclosingBuilder:: ClassDeclarationBuilder fromSource:
'class Enclosing = ()(
class OldName = ()()
public getOldName = (^self OldName)
)'.
enclosingClass:: enclosingBuilder install applyToObject reflectee.
cls:: enclosingClass new getOldName.
assert: (ClassMirror reflecting: cls) mixin name equals: 'OldName'.
assert: (ClassMirror reflecting: cls) mixin declaration accessModifier equals: #protected.
builder:: (ClassMirror reflecting: cls) mixin declaration asBuilder.
builder header name: 'NewName'.
builder install.
assert: (ClassMirror reflecting: cls) mixin name equals: 'NewName'.
assert: (ClassMirror reflecting: cls) mixin declaration accessModifier equals: #protected.
)
public testClassDeclRenameNestedPrivate = (
|
enclosingBuilder <ClassDeclarationBuilder>
enclosingClass
cls
builder
|
enclosingBuilder:: ClassDeclarationBuilder fromSource:
'class Enclosing = ()(
private class OldName = ()()
public getOldName = (^self OldName)
)'.
enclosingClass:: enclosingBuilder install applyToObject reflectee.
cls:: enclosingClass new getOldName.
assert: (ClassMirror reflecting: cls) mixin name equals: 'OldName'.
assert: (ClassMirror reflecting: cls) mixin declaration accessModifier equals: #private.
builder:: (ClassMirror reflecting: cls) mixin declaration asBuilder.
builder header name: 'NewName'.
builder install.
assert: (ClassMirror reflecting: cls) mixin name equals: 'NewName'.
assert: (ClassMirror reflecting: cls) mixin declaration accessModifier equals: #private.
)
public testClassDeclRenameTopLevel = (
|
builder <ClassDeclarationBuilder>
cls
builder2
|
builder:: ClassDeclarationBuilder fromSource: 'class OldName = ()()'.
cls:: builder install applyToObject reflectee.
assert: (ClassMirror reflecting: cls) mixin name equals: 'OldName'.
assert: (ClassMirror reflecting: cls) mixin declaration accessModifier equals: #public.
builder2:: (ClassMirror reflecting: cls) mixin declaration asBuilder.
builder2 header name: 'NewName'.
builder2 install.
assert: (ClassMirror reflecting: cls) mixin name equals: 'NewName'.
assert: (ClassMirror reflecting: cls) mixin declaration accessModifier equals: #public.
)
public testClassDeclReplaceMethod = (
(* replace an existing method *)
|
klass <Class>
instance
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclReplaceMethod = ()(
public foo = (^#before)
)'.
instance:: klass new.
assert: instance foo equals: #before.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide methods addFromSource: 'public foo = (^#after)'.
assert: instance foo equals: #before.
builder install.
assert: instance foo equals: #after.
)
public testClassDeclReplaceNestedClass = (
(* replace an existing nested class *)
|
builder
klass
instance
nestedKlass
|
klass:: classFromSource: 'class TestClassDeclReplaceNestedClass = ()(
public class Nested = ()() : ( public foo = (^#before) )
)'.
instance:: klass new.
nestedKlass:: instance Nested.
assert: nestedKlass foo equals: #before.
assert: instance Nested foo equals: #before.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder instanceSide nestedClasses addFromSource:
'public class Nested = ()() : ( public foo = (^#after) )'.
assert: nestedKlass foo equals: #before.
assert: instance Nested foo equals: #before.
builder install.
assert: nestedKlass foo equals: #before.
assert: instance Nested foo equals: #before.
assert: klass new Nested foo equals: #after.
)
public testClassDeclReplaceSlot = (
(* replace an existing slot *)
|
klass <Class>
instance <Object>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class TestClassDeclReplaceSlot = ( | public x ::= 117. public y = 0. public z | )()'.
instance:: klass new.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
should: [instance a] signal: MessageNotUnderstood.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder header source: 'class TestClassDeclReplaceSlot = ( | public a ::= 120. public y = 0. public z | )'.
assert: instance x equals: 117.
assert: instance y equals: 0.
assert: instance z equals: nil.
should: [instance a] signal: MessageNotUnderstood.
builder install.
should: [instance x] signal: MessageNotUnderstood.
assert: instance y equals: 0.
assert: instance z equals: nil.
assert: instance a equals: nil.
instance:: klass new.
should: [instance x] signal: MessageNotUnderstood.
assert: instance y equals: 0.
assert: instance z equals: nil.
assert: instance a equals: 120.
)
public testClassDeclSource = (
(* Note: We preserve exact class headers and methods, but not whole class declarations. *)
|
headerSource
nestedHeaderSource
methodSource
klass <Class>
builder <ClassDeclarationBuilder>
mirror <ClassMirror>
|
headerSource:: 'class Foo = (
|
bar
baz
|
)'.
nestedHeaderSource:: 'class Nested = (
| hest fisk |
)'.
methodSource:: 'public main = ()'.
builder:: ClassDeclarationBuilder fromSource:
headerSource, '(', nestedHeaderSource, '()', methodSource, ')'.
assert: (builder source indexOf: headerSource) > 0.
assert: (builder source indexOf: nestedHeaderSource) > 0.
assert: (builder source indexOf: methodSource) > 0.
klass:: builder install applyToObject reflectee.
mirror:: ClassMirror reflecting: klass.
assert: (mirror mixin declaration source indexOf: headerSource) > 0.
assert: (mirror mixin declaration source indexOf: nestedHeaderSource) > 0.
assert: (mirror mixin declaration source indexOf: methodSource) > 0.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
assert: (builder source indexOf: headerSource) > 0.
assert: (builder source indexOf: nestedHeaderSource) > 0.
assert: (builder source indexOf: methodSource) > 0.
)
public testClassHeaderComment = (
(* Note: We preserve exact class headers and methods, but not whole class declarations. *)
|
comment
headerSource
klass <Class>
builder <ClassDeclarationBuilder>
mirror <ClassMirror>
|
comment:: ' This is the (* nested *) comment. '.
headerSource:: 'class Foo = (
(*', comment, '*)
)'.
builder:: ClassDeclarationBuilder fromSource: headerSource, '()'.
assert: builder header classComment equals: comment.
klass:: builder install applyToObject reflectee.
mirror:: ClassMirror reflecting: klass.
assert: mirror mixin declaration header classComment equals: comment.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
assert: builder header classComment equals: comment.
)
public testClassHeaderPreamble = (
(* Note: We preserve exact class headers and methods, but not whole class declarations. *)
|
preamble
headerSource
klass <Class>
builder <ClassDeclarationBuilder>
mirror <ClassMirror>
|
preamble:: 'class Foo foo: x = Bar bar: x'.
headerSource:: preamble, ' (
|
bar
baz
|
)'.
builder:: ClassDeclarationBuilder fromSource: headerSource, '()'.
assert: builder header preamble equals: preamble.
klass:: builder install applyToObject reflectee.
mirror:: ClassMirror reflecting: klass.
assert: mirror mixin declaration header preamble equals: preamble.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
assert: builder header preamble equals: preamble.
)
public testClassHeaderSource = (
(* Note: We preserve exact class headers and methods, but not whole class declarations. *)
|
headerSource
klass <Class>
builder <ClassDeclarationBuilder>
mirror <ClassMirror>
|
headerSource:: 'class Foo = (
|
bar
baz
|
)'.
builder:: ClassDeclarationBuilder fromSource: headerSource, '()'.
assert: builder header source equals: headerSource.
klass:: builder install applyToObject reflectee.
mirror:: ClassMirror reflecting: klass.
assert: mirror mixin declaration header source equals: headerSource.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
assert: builder header source equals: headerSource.
)
public testConflictingFactoryOverMethod = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()() : ( foo = () )'.
should: [builder header source: 'class Conflicted foo = ()'] signal: conflictError.
)
public testConflictingMethodOverFactory = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted foo = ()()'.
should: [builder classSide methods addFromSource: 'foo = ()'] signal: conflictError.
)
public testConflictingMethodOverNestedClass = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()(
class foo = ()()
)'.
should: [builder instanceSide methods addFromSource: 'foo = ()'] signal: conflictError.
)
public testConflictingMethodOverSlot = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = (| foo bar = 1. |)()'.
should: [builder instanceSide methods addFromSource: 'foo = ()'] signal: conflictError.
should: [builder instanceSide methods addFromSource: 'foo: x = ()'] signal: conflictError.
should: [builder instanceSide methods addFromSource: 'bar = ()'] signal: conflictError.
builder instanceSide methods addFromSource: 'bar: x = ()'.
)
public testConflictingNestedClassFromRename = (
| builder nested |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()(
class Nested1 = ()()
class Nested2 = ()()
)'.
nested:: in: builder instanceSide nestedClasses findMirrorNamed: #Nested1.
should: [nested name: #Nested1] signal: conflictError.
)
public testConflictingNestedClassOverMethod = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()( foo = () )'.
should: [builder instanceSide nestedClasses addFromSource: 'class foo = ()()'] signal: conflictError.
)
public testConflictingNestedClassOverMethodFromRename = (
| builder nested |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()(
class Nested = ()()
foo = ()
)'.
nested:: in: builder instanceSide nestedClasses findMirrorNamed: #Nested.
should: [nested name: #foo] signal: conflictError.
)
public testConflictingNestedClassOverSlot = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = (| foo |)()'.
should: [builder instanceSide nestedClasses addFromSource: 'class foo = ()()'] signal: conflictError.
)
public testConflictingNestedClassOverSlotFromRename = (
| builder nested |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = (| foo |)(
class Nested = ()()
)'.
nested:: in: builder instanceSide nestedClasses findMirrorNamed: #Nested.
should: [nested name: #foo] signal: conflictError.
)
public testConflictingSlotOverMethod = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()(
foo = ()
)'.
should: [builder header source: 'Conflicted = (| foo |)'] signal: conflictError.
)
public testConflictingSlotOverNestedClass = (
| builder |
builder:: ClassDeclarationBuilder fromSource: 'class Conflicted = ()(
class foo = ()()
)'.
should: [builder header source: 'Conflicted = (| foo |)'] signal: conflictError.
)
public testEvaluateEmptyString = (
| thread |
thread:: (ObjectMirror reflecting: self) evaluate: ''.
assert: thread isFulfilled.
assert: thread result reflectee equals: self.
)
public testEvaluateWithScope = (
| scope thread |
scope:: Map new.
scope at: #x put: (ObjectMirror reflecting: 42).
scope at: #y put: (ObjectMirror reflecting: 2).
thread:: (ObjectMirror reflecting: self) evaluate: 'x - y' with: scope.
assert: thread isFulfilled.
assert: thread result reflectee equals: 40.
)
public testIncrementalScopeBuilding = (
(* add a new method *)
|
builder <ClassDeclarationBuilder>
klass <Class>
ooter
inner
thread
|
builder:: ClassDeclarationBuilder fromSource:
'class Outer = ( | public outerSlot = 1. | )(
public class Inner = ( | public innerSlot = 3. | )(
public class InnerInner = ()()
public innerMethod = (^4)
)
public class Sibling = ()()
public outerMethod = (^2)
)'.
klass:: builder install applyToObject reflectee.
ooter:: klass new.
inner:: ooter Inner new.
(* Incremental eval *)
thread:: (ObjectMirror reflecting: inner) evaluate:
'{outer Outer. outerSlot. outerMethod. outer Inner. innerSlot. innerMethod. Inner. Sibling. InnerInner}'.
assert: thread isFulfilled.
assertList: thread result reflectee equals: {ooter. ooter outerSlot. ooter outerMethod. inner. inner innerSlot. inner innerMethod. ooter Inner. ooter Sibling. inner InnerInner.}.
(* Incremental method *)
(in: builder instanceSide nestedClasses findMirrorNamed: #Inner) instanceSide methods addFromSource: 'public testMethod = (^{outer Outer. outerSlot. outerMethod. outer Inner. innerSlot. innerMethod. Inner. Sibling. InnerInner})'.
builder install.
assertList: inner testMethod equals: {ooter. ooter outerSlot. ooter outerMethod. inner. inner innerSlot. inner innerMethod. ooter Inner. ooter Sibling. inner InnerInner.}.
(* Incremental header *)
(in: builder instanceSide nestedClasses findMirrorNamed: #Inner) header source: 'public class Inner = ( | public innerSlot = 33. public testSlot = {outer Outer. outerSlot. outerMethod. outer Inner. innerSlot. innerMethod. Inner. Sibling. InnerInner}. |)'.
builder install.
inner:: ooter Inner new.
assertList: inner testSlot equals: {ooter. ooter outerSlot. ooter outerMethod. inner. inner innerSlot. inner innerMethod. ooter Inner. ooter Sibling. inner InnerInner.}.
(* Incremental nested class *)
builder instanceSide nestedClasses addFromSource: 'public class Inner2 = ( | public innerSlot = 333. | )(
public class InnerInner = ()()
public innerMethod = (^444)
public testMethod = (^{outer Outer. outerSlot. outerMethod. outer Inner2. innerSlot. innerMethod. Inner2. Sibling. InnerInner})
)'.
builder install.
inner:: ooter Inner2 new.
assertList: inner testMethod equals: {ooter. ooter outerSlot. ooter outerMethod. inner. inner innerSlot. inner innerMethod. ooter Inner2. ooter Sibling. inner InnerInner.}.
)
public testInstallAfterConflictErrorFactory = (
| klass builder |
klass:: classFromSource: 'class TestInstallAfterConflictErrorFactory factory = ()( public foo = (^1) )'.
assert: klass factory foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder classSide methods addFromSource: 'public factory = (^#wrong)'] signal: conflictError.
builder install.
assert: klass factory foo equals: 1. (* Unchanged. *)
)
public testInstallAfterConflictErrorMethod = (
| klass instance builder |
klass:: classFromSource: 'class TestInstallAfterConflictErrorMethod = ()( public foo = (^1) )'.
instance:: klass new.
assert: instance foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder header source: 'TestInstallAfterConflictErrorMethod = (| foo |)'] signal: conflictError.
builder install.
assert: instance foo equals: 1. (* Unchanged. *)
)
public testInstallAfterConflictErrorNestedClass = (
| klass instance builder |
klass:: classFromSource: 'class TestInstallAfterConflictErrorNestedClass = ()(
public class Nested = ()() : ( public foo = (^1) )
)'.
instance:: klass new.
assert: instance Nested foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder instanceSide addFromSource: 'public Nested = (^#wrong)'] signal: conflictError.
builder install.
assert: instance Nested foo equals: 1. (* Unchanged. *)
)
public testInstallAfterConflictErrorSlot = (
| klass instance builder |
klass:: classFromSource: 'class TestInstallAfterConflictErrorSlot = (| public foo = 1. |)()'.
instance:: klass new.
assert: instance foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder header source: 'TestInstallAfterConflictErrorSlot = (| foo |)'] signal: conflictError.
builder install.
assert: instance foo equals: 1. (* Unchanged. *)
)
public testInstallAfterSyntaxErrorHeader = (
| klass instance builder |
klass:: classFromSource: 'class TestInstallAfterSyntaxErrorHeader = (| public foo = 1. |)()'.
instance:: klass new.
assert: instance foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder header source: 'class TestInstallAfterSyntaxErrorHeader = (;;syntax error;;)()'] signal: syntaxError.
builder install.
assert: instance foo equals: 1. (* Unchanged. *)
)
public testInstallAfterSyntaxErrorMethod = (
| klass instance builder |
klass:: classFromSource: 'class TestInstallAfterSyntaxErrorMethod = ()( public foo = (^1) )'.
instance:: klass new.
assert: instance foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder instanceSide methods addFromSource: 'public foo = (^2 ;;syntax error;;)'] signal: syntaxError.
builder install.
assert: instance foo equals: 1. (* Unchanged. *)
)
public testInstallAfterSyntaxErrorNestedClass = (
| klass builder |
klass:: classFromSource: 'class TestInstallAfterSyntaxErrorNestedClass = ()(
public class Nested = ()() : ( public foo = (^1) )
)'.
assert: klass new Nested foo equals: 1.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
should: [builder instanceSide nestedClasses addFromSource: 'class Nested = (;;syntax error;;)()'] signal: syntaxError.
builder install.
assert: klass new Nested foo equals: 1. (* Unchanged. *)
)
public testMethodSource = (
(* Note: We preserve exact class headers and methods, but not whole class declarations. *)
|
methodSource
klass <Class>
builder <ClassDeclarationBuilder>
mirror <ClassMirror>
|
methodSource:: 'private foo = (
^bar baz
)'.
builder:: ClassDeclarationBuilder fromSource: 'class C = ()(', methodSource, ')'.
assert: (in: builder instanceSide methods findMirrorNamed: #foo) source equals: methodSource.
klass:: builder install applyToObject reflectee.
mirror:: ClassMirror reflecting: klass.
assert: (in: mirror mixin methods findMirrorNamed: #foo) source equals: methodSource.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
assert: (in: builder instanceSide methods findMirrorNamed: #foo) source equals: methodSource.
)
public testNewNestedClassSeenInSubclass = (
(* Note that the superclass and subclass must be part of separate modules. Otherwise this test will pass by virtue of the common enclosing class being dirty, and here we want to test that subclasses tracking is working. *)
|
Super <Class>
enclosing <Class>
klass <Class>
instance <Object>
builder <ClassDeclarationBuilder>
|
Super:: classFromSource: 'class Super = ()()'.
enclosing:: classFromSource: 'class Enclosing withSuper: s = ( | Super = s. | )(
public class Sub = Super ()(
public class OldNested = ()()
)
)'.
klass:: (enclosing withSuper: Super) Sub.
instance:: klass new.
assert: instance OldNested isKindOfBehavior.
should: [instance NewNested] signal: MessageNotUnderstood.
builder:: (ClassMirror reflecting: klass) superclass mixin declaration asBuilder.
builder instanceSide nestedClasses addFromSource: 'public class NewNested = ()()'.
assert: instance OldNested isKindOfBehavior.
should: [instance NewNested] signal: MessageNotUnderstood.
builder install.
assert: instance OldNested isKindOfBehavior.
assert: instance NewNested isKindOfBehavior.
deny: instance OldNested equals: instance NewNested. (* Should not alias slot. *)
instance:: klass new.
assert: instance OldNested isKindOfBehavior.
assert: instance NewNested isKindOfBehavior.
deny: instance OldNested equals: instance NewNested. (* Should not alias slot. *)
)
public testNewSlotSeenInSubclass = (
(* Note that the superclass and subclass must be part of separate modules. Otherwise this test will pass by virtue of the common enclosing class being dirty, and here we want to test that subclasses tracking is working. *)
|
Super <Class>
enclosing <Class>
klass <Class>
instance <Object>
builder <ClassDeclarationBuilder>
|
Super:: classFromSource: 'class Super = ()()'.
enclosing:: classFromSource: 'class Enclosing withSuper: s = ( | Super = s. | )(
public class Sub = Super ( | public oldSlot ::= 43. | )()
)'.
klass:: (enclosing withSuper: Super) Sub.
instance:: klass new.
assert: instance oldSlot equals: 43.
should: [instance newSlot] signal: MessageNotUnderstood.
builder:: (ClassMirror reflecting: klass) superclass mixin declaration asBuilder.
builder header source: 'class Super = ( | public newSlot ::= 47. | )'.
assert: instance oldSlot equals: 43.
should: [instance newSlot] signal: MessageNotUnderstood.
builder install.
assert: instance oldSlot equals: 43.
assert: instance newSlot equals: nil.
instance newSlot: 98.
assert: instance newSlot equals: 98.
assert: instance oldSlot equals: 43. (* Not aliased by newSlot. *)
instance:: klass new.
assert: instance oldSlot equals: 43.
assert: instance newSlot equals: 47.
)
public testShapeChangePreservesIdentityHash = (
|
klass <Class>
instance <Object>
oldHash <Integer>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class Foo = ()()'.
instance:: klass new.
oldHash:: instance hash.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder header source: 'class Foo = ( | public newSlot ::= 91. | )'.
builder install.
assert: instance newSlot equals: nil.
assert: instance hash equals: oldHash.
)
public testShapeChangeWithHostileEquals = (
|
klass <Class>
instance1 <Object>
instance2 <Object>
builder <ClassDeclarationBuilder>
|
klass:: classFromSource: 'class Class slot: s = ( | public slot = s. |)(
public = other ^<Boolean> = ( ^Error signal: ''Should not be called'' )
public hash ^<Integer> = ( ^Error signal: ''Should not be called'' )
) : (
public = other ^<Boolean> = ( ^Error signal: ''Should not be called'' )
public hash ^<Integer> = ( ^Error signal: ''Should not be called'' )
)'.
instance1:: klass slot: 1.
instance2:: klass slot: 2.
assert: instance1 slot equals: 1.
assert: instance2 slot equals: 2.
builder:: (ClassMirror reflecting: klass) mixin declaration asBuilder.
builder header source: 'class Class slot: s = ( | public slot = s. public newSlot |)'.
builder install.
instance1:: klass slot: 1.
instance2:: klass slot: 2.
assert: instance1 slot equals: 1.
assert: instance2 slot equals: 2.
assert: instance1 newSlot equals: nil.
assert: instance2 newSlot equals: nil.
)
) : (
TEST_CONTEXT = ()
)
) : (
)