forked from amber-smalltalk/amber
/
Kernel-Tests.st
1582 lines (1225 loc) · 35.9 KB
/
Kernel-Tests.st
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
Smalltalk current createPackage: 'Kernel-Tests' properties: #{}!
TestCase subclass: #BlockClosureTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!BlockClosureTest methodsFor: 'tests'!
testCanClearInterval
self shouldnt: [([Error new signal] valueWithInterval: 0) clearInterval] raise: Error
!
testCanClearTimeout
self shouldnt: [([Error new signal] valueWithTimeout: 0) clearTimeout] raise: Error
!
testCompiledSource
self assert: ([1+1] compiledSource includesSubString: 'function')
!
testCurrySelf
| curriedMethod array |
curriedMethod := [ :selfarg :x | selfarg at: x ] currySelf asCompiledMethod: 'foo:'.
array := #(3 1 4).
ClassBuilder new installMethod: curriedMethod forClass: Array category: '**test helper'.
[ self assert: (array foo: 2) equals: 1 ]
ensure: [ Array removeCompiledMethod: curriedMethod ]
!
testEnsure
self assert: ([3] ensure: [4]) equals: 3
!
testEnsureRaises
self should: [[Error new signal] ensure: [true]] raise: Error
!
testExceptionSemantics
"See https://github.com/NicolasPetton/amber/issues/314"
self timeout: 100.
(self async: [
[
self assert: true.
Error signal.
"The following should *not* be run"
self deny: true.
self finished.
] on: Error do: [ :ex | self finished ]
]) valueWithTimeout: 0
!
testNumArgs
self assert: [] numArgs equals: 0.
self assert: [:a :b | ] numArgs equals: 2
!
testOnDo
self assert: ([Error new signal] on: Error do: [:ex | true])
!
testValue
self assert: ([1+1] value) equals: 2.
self assert: ([:x | x +1] value: 2) equals: 3.
self assert: ([:x :y | x*y] value: 2 value: 4) equals: 8.
"Arguments are optional in Amber. This isn't ANSI compliant."
self assert: ([:a :b :c | 1] value) equals: 1
!
testValueWithPossibleArguments
self assert: ([1] valueWithPossibleArguments: #(3 4)) equals: 1.
self assert: ([:a | a + 4] valueWithPossibleArguments: #(3 4)) equals: 7.
self assert: ([:a :b | a + b] valueWithPossibleArguments: #(3 4 5)) equals: 7.
!
testWhileFalse
| i |
i := 0.
[i > 5] whileFalse: [i := i + 1].
self assert: i equals: 6.
i := 0.
[i := i + 1. i > 5] whileFalse.
self assert: i equals: 6
!
testWhileTrue
| i |
i := 0.
[i < 5] whileTrue: [i := i + 1].
self assert: i equals: 5.
i := 0.
[i := i + 1. i < 5] whileTrue.
self assert: i equals: 5
! !
TestCase subclass: #BooleanTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!BooleanTest methodsFor: 'tests'!
testEquality
"We're on top of JS...just be sure to check the basics!!"
self deny: 0 = false.
self deny: false = 0.
self deny: '' = false.
self deny: false = ''.
self assert: (true = true).
self deny: false = true.
self deny: true = false.
self assert: (false = false).
"JS may do some type coercing after sending a message"
self assert: (true yourself = true).
self assert: (true yourself = true yourself)
!
testIdentity
"We're on top of JS...just be sure to check the basics!!"
self deny: 0 == false.
self deny: false == 0.
self deny: '' == false.
self deny: false == ''.
self assert: true == true.
self deny: false == true.
self deny: true == false.
self assert: false == false.
"JS may do some type coercing after sending a message"
self assert: true yourself == true.
self assert: true yourself == true yourself
!
testIfTrueIfFalse
self assert: (true ifTrue: ['alternative block']) equals: 'alternative block'.
self assert: (true ifFalse: ['alternative block']) equals: nil.
self assert: (false ifTrue: ['alternative block']) equals: nil.
self assert: (false ifFalse: ['alternative block']) equals: 'alternative block'.
self assert: (false ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
self assert: (false ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
self assert: (true ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
self assert: (true ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
!
testIfTrueIfFalseWithBoxing
self assert: (true yourself ifTrue: ['alternative block']) equals: 'alternative block'.
self assert: (true yourself ifFalse: ['alternative block']) equals: nil.
self assert: (false yourself ifTrue: ['alternative block']) equals: nil.
self assert: (false yourself ifFalse: ['alternative block']) equals: 'alternative block'.
self assert: (false yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block2'.
self assert: (false yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block'.
self assert: (true yourself ifTrue: ['alternative block'] ifFalse: ['alternative block2']) equals: 'alternative block'.
self assert: (true yourself ifFalse: ['alternative block'] ifTrue: ['alternative block2']) equals: 'alternative block2'.
!
testLogic
"Trivial logic table"
self assert: (true & true);
deny: (true & false);
deny: (false & true);
deny: (false & false).
self assert: (true | true);
assert: (true | false);
assert: (false | true);
deny: (false | false).
"Checking that expressions work fine too"
self assert: (true & (1 > 0));
deny: ((1 > 0) & false);
deny: ((1 > 0) & (1 > 2)).
self assert: (false | (1 > 0));
assert: ((1 > 0) | false);
assert: ((1 > 0) | (1 > 2))
!
testLogicKeywords
"Trivial logic table"
self
assert: (true and: [ true]);
deny: (true and: [ false ]);
deny: (false and: [ true ]);
deny: (false and: [ false ]).
self
assert: (true or: [ true ]);
assert: (true or: [ false ]);
assert: (false or: [ true ]);
deny: (false or: [ false ]).
"Checking that expressions work fine too"
self
assert: (true and: [ 1 > 0 ]);
deny: ((1 > 0) and: [ false ]);
deny: ((1 > 0) and: [ 1 > 2 ]).
self
assert: (false or: [ 1 > 0 ]);
assert: ((1 > 0) or: [ false ]);
assert: ((1 > 0) or: [ 1 > 2 ])
!
testNonBooleanError
self should: [ '' ifTrue: [] ifFalse: [] ] raise: NonBooleanReceiver
! !
TestCase subclass: #ClassBuilderTest
instanceVariableNames: 'builder theClass'
package: 'Kernel-Tests'!
!ClassBuilderTest methodsFor: 'running'!
setUp
builder := ClassBuilder new
!
tearDown
theClass ifNotNil: [Smalltalk current removeClass: theClass. theClass := nil]
! !
!ClassBuilderTest methodsFor: 'tests'!
testClassCopy
theClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
self assert: theClass superclass == ObjectMock superclass.
self assert: theClass instanceVariableNames == ObjectMock instanceVariableNames.
self assert: theClass name equals: 'ObjectMock2'.
self assert: theClass package == ObjectMock package.
self assert: theClass methodDictionary keys equals: ObjectMock methodDictionary keys
!
testClassMigration
| instance oldClass |
oldClass := builder copyClass: ObjectMock named: 'ObjectMock2'.
instance := (Smalltalk current at: 'ObjectMock2') new.
"Change the superclass of ObjectMock2"
ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
instanceVariableNames: ''
package: 'Kernel-Tests'.
self deny: oldClass == ObjectMock2.
self assert: ObjectMock2 superclass == ObjectMock.
self assert: ObjectMock2 instanceVariableNames isEmpty.
self assert: ObjectMock2 selectors equals: oldClass selectors.
self assert: ObjectMock2 comment equals: oldClass comment.
self assert: ObjectMock2 package name equals: 'Kernel-Tests'.
self deny: instance class == ObjectMock2.
self assert: instance class name equals: 'OldObjectMock2'.
self assert: (Smalltalk current at: 'OldObjectMock2') isNil.
Smalltalk current removeClass: ObjectMock2
!
testClassMigrationWithClassInstanceVariables
builder copyClass: ObjectMock named: 'ObjectMock2'.
ObjectMock2 class instanceVariableNames: 'foo bar'.
"Change the superclass of ObjectMock2"
ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
instanceVariableNames: ''
package: 'Kernel-Tests'.
self assert: ObjectMock2 class instanceVariableNames equals: #('foo' 'bar').
Smalltalk current removeClass: ObjectMock2
!
testClassMigrationWithSubclasses
builder copyClass: ObjectMock named: 'ObjectMock2'.
ObjectMock2 subclass: 'ObjectMock3' instanceVariableNames: '' package: 'Kernel-Tests'.
ObjectMock3 subclass: 'ObjectMock4' instanceVariableNames: '' package: 'Kernel-Tests'.
"Change the superclass of ObjectMock2"
ObjectMock subclass: (Smalltalk current at: 'ObjectMock2')
instanceVariableNames: ''
package: 'Kernel-Tests'.
self assert: (ObjectMock subclasses includes: ObjectMock2).
self assert: (ObjectMock2 subclasses includes: ObjectMock3).
self assert: (ObjectMock3 subclasses includes: ObjectMock4).
ObjectMock allSubclasses do: [ :each | Smalltalk current removeClass: each ]
!
testInstanceVariableNames
self assert: (builder instanceVariableNamesFor: ' hello world ') equals: #('hello' 'world')
! !
TestCase subclass: #CollectionTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!CollectionTest methodsFor: 'accessing'!
collection
^ self collectionClass withAll: self defaultValues
!
collectionClass
^ self class collectionClass
!
collectionWithDuplicates
^ self collectionClass withAll: #('a' 'b' 'c' 1 2 1 'a')
!
defaultValues
^ #(1 2 3 -4)
! !
!CollectionTest methodsFor: 'convenience'!
assertSameContents: aCollection as: anotherCollection
self assert: (aCollection size = anotherCollection size).
aCollection do: [ :each |
self assert: ((aCollection occurrencesOf: each) = (anotherCollection occurrencesOf: each)) ]
! !
!CollectionTest methodsFor: 'testing'!
isCollectionReadOnly
^ false
! !
!CollectionTest methodsFor: 'tests'!
testAsArray
self
assertSameContents: self collection
as: self collection asArray
!
testAsOrderedCollection
self
assertSameContents: self collection
as: self collection asOrderedCollection
!
testAsSet
| c set |
c := self collectionWithDuplicates.
set := c asSet.
self assert: set size equals: 5.
c do: [ :each |
self assert: (set includes: each) ]
!
testCollect
| newCollection |
newCollection := #(1 2 3 4).
self
assertSameContents: (self collection collect: [ :each |
each abs ])
as: newCollection
!
testDetect
self assert: (self collection detect: [ :each | each < 0 ]) equals: -4.
self
should: [ self collection detect: [ :each | each = 6 ] ]
raise: Error
!
testDo
| newCollection |
newCollection := OrderedCollection new.
self collection do: [ :each |
newCollection add: each ].
self
assertSameContents: self collection
as: newCollection
!
testIsEmpty
self assert: self collectionClass new isEmpty.
self deny: self collection isEmpty
!
testSelect
| newCollection |
newCollection := #(2 -4).
self
assertSameContents: (self collection select: [ :each |
each even ])
as: newCollection
!
testSize
self assert: self collectionClass new size equals: 0.
self assert: self collection size equals: 4
! !
!CollectionTest class methodsFor: 'accessing'!
collectionClass
^ nil
! !
!CollectionTest class methodsFor: 'testing'!
isAbstract
^ self collectionClass isNil
! !
CollectionTest subclass: #HashedCollectionTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!HashedCollectionTest methodsFor: 'accessing'!
collection
^ #{ 'a' -> 1. 'b' -> 2. 'c' -> 3. 'd' -> -4 }
!
collectionWithDuplicates
^ #{ 'a' -> 1. 'b' -> 2. 'c' -> 3. 'd' -> -4. 'e' -> 1. 'f' -> 2. 'g' -> 10 }
! !
!HashedCollectionTest class methodsFor: 'accessing'!
collectionClass
^ HashedCollection
! !
HashedCollectionTest subclass: #DictionaryTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!DictionaryTest methodsFor: 'accessing'!
collection
^ Dictionary new
at: 1 put: 1;
at: 'a' put: 2;
at: true put: 3;
at: 4 put: -4;
yourself
!
collectionWithDuplicates
^ Dictionary new
at: 1 put: 1;
at: 'a' put: 2;
at: true put: 3;
at: 4 put: -4;
at: 'b' put: 1;
at: 3 put: 3;
at: false put: 12;
yourself
! !
!DictionaryTest methodsFor: 'tests'!
testAccessing
| d |
d := Dictionary new.
d at: 'hello' put: 'world'.
self assert: (d at: 'hello') equals: 'world'.
self assert: (d at: 'hello' ifAbsent: [nil]) equals: 'world'.
self deny: (d at: 'foo' ifAbsent: [nil]) = 'world'.
d at: 1 put: 2.
self assert: (d at: 1) equals: 2.
d at: 1@3 put: 3.
self assert: (d at: 1@3) equals: 3
!
testDynamicDictionaries
self assert: #{'hello' -> 1} asDictionary equals: (Dictionary with: 'hello' -> 1)
!
testEquality
| d1 d2 |
self assert: (Dictionary new = Dictionary new).
d1 := Dictionary new at: 1 put: 2; yourself.
d2 := Dictionary new at: 1 put: 2; yourself.
self assert: (d1 = d2).
d2 := Dictionary new at: 1 put: 3; yourself.
self deny: d1 = d2.
d2 := Dictionary new at: 2 put: 2; yourself.
self deny: d1 = d2.
d2 := Dictionary new at: 1 put: 2; at: 3 put: 4; yourself.
self deny: d1 = d2.
!
testIfAbsent
| d visited |
visited := false.
d := Dictionary new.
d at: 'hello' ifAbsent: [ visited := true ].
self assert: visited.
!
testIfPresent
| d visited absent |
visited := false.
d := Dictionary new.
d at: 'hello' put: 'world'.
d at: 'hello' ifPresent: [ :value | visited := value ].
self assert: visited equals: 'world'.
absent := d at: 'bye' ifPresent: [ :value | visited := value ].
self assert: absent isNil.
!
testIfPresentIfAbsent
| d visited |
visited := false.
d := Dictionary new.
d at: 'hello' put: 'world'.
d at: 'hello' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
self assert: visited equals: 'world'.
d at: 'buy' ifPresent: [ :value | visited := value ] ifAbsent: [ visited := true ].
self assert: visited.
!
testKeys
| d |
d := Dictionary new.
d at: 1 put: 2.
d at: 2 put: 3.
d at: 3 put: 4.
self assert: d keys equals: #(1 2 3)
!
testPrintString
self
assert: (Dictionary new
at:'firstname' put: 'James';
at:'lastname' put: 'Bond';
printString)
equals: 'a Dictionary(''firstname''->''James'' , ''lastname''->''Bond'')'
!
testRemoveKey
| d key |
d := Dictionary new.
d at: 1 put: 2.
d at: 2 put: 3.
d at: 3 put: 4.
key := 2.
self assert: d keys equals: #(1 2 3).
d removeKey: key.
self assert: d keys equals: #(1 3).
self assert: d values equals: #(2 4).
self deny: (d includesKey: 2)
!
testRemoveKeyIfAbsent
| d key |
d := Dictionary new.
d at: 1 put: 2.
d at: 2 put: 3.
d at: 3 put: 4.
key := 2.
self assert: (d removeKey: key) equals: 3.
key := 3.
self assert: (d removeKey: key ifAbsent: [42]) equals: 4.
key := 'why'.
self assert: (d removeKey: key ifAbsent: [42] ) equals: 42.
!
testSize
| d |
d := Dictionary new.
self assert: d size equals: 0.
d at: 1 put: 2.
self assert: d size equals: 1.
d at: 2 put: 3.
self assert: d size equals: 2.
!
testValues
| d |
d := Dictionary new.
d at: 1 put: 2.
d at: 2 put: 3.
d at: 3 put: 4.
self assert: d values equals: #(2 3 4)
! !
!DictionaryTest class methodsFor: 'accessing'!
collectionClass
^ Dictionary
! !
CollectionTest subclass: #SequenceableCollectionTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!SequenceableCollectionTest methodsFor: 'tests'!
testAt
self assert: (self collection at: 4) equals: -4.
self should: [ self collection at: 5 ] raise: Error
!
testAtIfAbsent
self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
! !
SequenceableCollectionTest subclass: #ArrayTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!ArrayTest methodsFor: 'tests'!
testAtIfAbsent
| array |
array := #('hello' 'world').
self assert: (array at: 1) equals: 'hello'.
self assert: (array at: 2) equals: 'world'.
self assert: (array at: 2 ifAbsent: ['not found']) equals: 'world'.
self assert: (array at: 0 ifAbsent: ['not found']) equals: 'not found'.
self assert: (array at: -10 ifAbsent: ['not found']) equals: 'not found'.
self assert: (array at: 3 ifAbsent: ['not found']) equals: 'not found'.
!
testFirstN
self assert: ({1. 2. 3. 4. 5} first: 3) equals: {1. 2. 3}
!
testIfEmpty
self assert: ( '' ifEmpty: ['zork'] ) equals: 'zork'
!
testPrintString
| array |
array := Array new.
self assert: array printString equals: 'a Array ()'.
array add: 1; add: 3.
self assert: array printString equals: 'a Array (1 3)'.
array add: 'foo'.
self assert: array printString equals: 'a Array (1 3 ''foo'')'.
array remove: 1; remove: 3.
self assert: array printString equals: 'a Array (''foo'')'.
array addLast: 3.
self assert: array printString equals: 'a Array (''foo'' 3)'.
array addLast: 3.
self assert: array printString equals: 'a Array (''foo'' 3 3)'.
! !
!ArrayTest class methodsFor: 'accessing'!
collectionClass
^ Array
! !
SequenceableCollectionTest subclass: #StringTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!StringTest methodsFor: 'accessing'!
collection
^'hello'
!
collectionWithDuplicates
^ 'abbaerte'
! !
!StringTest methodsFor: 'tests'!
testAddRemove
self should: ['hello' add: 'a'] raise: Error.
self should: ['hello' remove: 'h'] raise: Error
!
testAsArray
self assert: 'hello' asArray equals: #('h' 'e' 'l' 'l' 'o').
!
testAt
self assert: ('hello' at: 1) equals: 'h'.
self assert: ('hello' at: 5) equals: 'o'.
self assert: ('hello' at: 6 ifAbsent: [nil]) equals: nil
!
testAtPut
"String instances are read-only"
self should: ['hello' at: 1 put: 'a'] raise: Error
!
testCollect
| newCollection |
newCollection := 'hheelllloo'.
self
assertSameContents: (self collection collect: [ :each |
each, each ])
as: newCollection
!
testCopyWithoutAll
self
assert: ('*hello* *world*' copyWithoutAll: '*')
equals: 'hello world'
!
testDetect
self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
self
should: [ self collection detect: [ :each | each = 6 ] ]
raise: Error
!
testEquality
self assert: 'hello' equals: 'hello'.
self deny: 'hello' = 'world'.
self assert: 'hello' equals: 'hello' yourself.
self assert: 'hello' yourself equals: 'hello'.
"test JS falsy value"
self deny: '' = 0
!
testIdentity
self assert: 'hello' == 'hello'.
self deny: 'hello' == 'world'.
self assert: 'hello' == 'hello' yourself.
self assert: 'hello' yourself == 'hello'.
"test JS falsy value"
self deny: '' == 0
!
testIncludesSubString
self assert: ('amber' includesSubString: 'ber').
self deny: ('amber' includesSubString: 'zork').
!
testJoin
self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
!
testSelect
| newCollection |
newCollection := 'o'.
self
assertSameContents: (self collection select: [ :each |
each = 'o' ])
as: newCollection
!
testSize
self assert: 'smalltalk' size equals: 9.
self assert: '' size equals: 0
!
testStreamContents
self
assert: (String streamContents: [ :aStream |
aStream
nextPutAll: 'hello'; space;
nextPutAll: 'world' ])
equals: 'hello world'
! !
!StringTest class methodsFor: 'accessing'!
collectionClass
^ String
! !
SequenceableCollectionTest subclass: #SymbolTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!SymbolTest methodsFor: 'accessing'!
collection
^ #hello
!
collectionWithDuplicates
^ #phhaaarorra
! !
!SymbolTest methodsFor: 'tests'!
testAsString
self assert: #hello asString equals: 'hello'
!
testAsSymbol
self assert: #hello == #hello asSymbol
!
testAt
self assert: (#hello at: 1) equals: 'h'.
self assert: (#hello at: 5) equals: 'o'.
self assert: (#hello at: 6 ifAbsent: [nil]) equals: nil
!
testAtPut
"Symbol instances are read-only"
self should: ['hello' at: 1 put: 'a'] raise: Error
!
testCollect
| newCollection |
newCollection := #hheelllloo.
self
assertSameContents: (self collection collect: [ :each |
each, each ])
as: newCollection
!
testComparing
self assert: #ab > #aa.
self deny: #ab > #ba.
self assert: #ab < #ba.
self deny: #bb < #ba.
self assert: #ab >= #aa.
self deny: #ab >= #ba.
self assert: #ab <= #ba.
self deny: #bb <= #ba
!
testCopying
self assert: #hello copy == #hello.
self assert: #hello deepCopy == #hello
!
testDetect
self assert: (self collection detect: [ :each | each = 'h' ]) equals: 'h'.
self
should: [ self collection detect: [ :each | each = 'z' ] ]
raise: Error
!
testEquality
self assert: (#hello = #hello).
self deny: #hello = #world.
self assert: (#hello = #hello yourself).
self assert: (#hello yourself = #hello).
self deny: #hello = 'hello'.
self deny: 'hello' = #hello.
!
testIdentity
self assert: #hello == #hello.
self deny: #hello == #world.
self assert: #hello = #hello yourself.
self assert: #hello yourself = #hello asString asSymbol
!
testIsEmpty
self deny: self collection isEmpty.
self assert: '' asSymbol isEmpty
!
testIsSymbolIsString
self assert: #hello isSymbol.
self deny: 'hello' isSymbol.
self deny: #hello isString.
self assert: 'hello' isString
!
testSelect
| newCollection |
newCollection := 'o'.
self
assertSameContents: (self collection select: [ :each |
each = 'o' ])
as: newCollection
!
testSize
self assert: #a size equals: 1.
self assert: #aaaaa size equals: 5
! !
!SymbolTest class methodsFor: 'accessing'!
collectionClass
^ Symbol
! !
TestCase subclass: #JSObjectProxyTest
instanceVariableNames: ''
package: 'Kernel-Tests'!
!JSObjectProxyTest methodsFor: 'accessing'!
jsObject
<return jsObject = {a: 1, b: function() {return 2;}, c: function(object) {return object;}, d: '', 'e': null, 'f': undefined}>
! !
!JSObjectProxyTest methodsFor: 'tests'!
testAtIfAbsent
| testObject |
testObject := self jsObject.
self assert: (testObject at: 'abc' ifAbsent: ['Property does not exist']) equals: 'Property does not exist'.
self assert: (testObject at: 'e' ifAbsent: ['Property does not exist']) equals: nil.
self assert: (testObject at: 'a' ifAbsent: ['Property does not exist']) equals: 1.
self assert: (testObject at: 'f' ifAbsent: ['Property does not exist']) equals: nil.
!
testDNU
self should: [self jsObject foo] raise: MessageNotUnderstood
!
testMessageSend
self assert: self jsObject a equals: 1.
self assert: self jsObject b equals: 2.
self assert: (self jsObject c: 3) equals: 3
!
testMethodWithArguments
self assert: (self jsObject c: 1) equals: 1
!
testPrinting
self assert: self jsObject printString equals: '[object Object]'
!
testPropertyThatReturnsEmptyString
| object |
object := self jsObject.
self assert: object d equals: ''.
object d: 'hello'.
self assert: object d equals: 'hello'
!
testPropertyThatReturnsUndefined
| object |
object := self jsObject.
self shouldnt: [ object e ] raise: MessageNotUnderstood.
self assert: object e isNil
!
testValue
| testObject |
testObject := self jsObject.
self assert: testObject value printString equals: '[object Object]'.
testObject at: 'value' put: 'aValue'.
self assert: testObject value equals: 'aValue'