Moved "Shell" code into a "Glob" type/namespace, and removed FormatSpecification...
[cslatevm.git] / tests / dictionary.slate
blob816ecee504d73dfb8e6d45255e1d86c5d4f885c6
1 UnitTests define: #Dictionary &parents: {TestCase}.
2 "TODO only tests base functionality, add SingleValueMap, EmptyMap, etc."
4 UnitTests Dictionary months1 ::=
5   (Dictionary newSize: 12) `>>
6     [add: 'January' -> 31.
7      at: 'February' put: 29.
8      at: 'March' put: 31.
9      add: 'April' -> 30.
10      at: 'May' put: 31.
11      at: 'June' put: 30.
12      add: (Association cloneSettingSlots: #{#key. #value} to: {'July'. 31}).
13      at: 'August' put: 31.
14      at: 'September' put: 30.
15      at: 'October' put: 31.
16      at: 'November' put: 30.
17      add: 'December' -> 31. ].
19 UnitTests Dictionary months2 ::=
20   Dictionary new `>>
21     [at: 'January' put: 31.
22      add: 'February' -> 29.
23      at: 'March' put: 31.
24      at: 'April' put: 30.
25      add: (Association cloneSettingSlots: #{#key. #value} to: {'May'. 31}).
26      at: 'June' put: 30.
27      at: 'July' put: 31.
28      at: 'August' put: 31.
29      at: 'September' put: 30.
30      add: 'October' -> 31.
31      at: 'November' put: 30.
32      add: (Association cloneSettingSlots: #{#key. #value} to: {'December'. 31}). ].
34 UnitTests Dictionary empty ::= Dictionary new.
36 t@(UnitTests Dictionary traits) testSignals
38   d1 ::= t months1.
40   t should: [d1 at: 'invalidKey'] raise: Mapping KeyNotFound
41     description: 'KeyNotFound is not signaled from at:'.
43   t should: [t empty anyOne] raise: Collection IsEmpty
44     description: 'IsEmpty is not signaled from anyOne'.
47 t@(UnitTests Dictionary traits) testAccess
49   d1 ::= t months1.
50   d2 ::= t months2.
52   t assert: (d1 at: 'January') = (d2 at: 'January') description: 'Value at January differs'.
53   t assert: (d1 at: 'December') = (d2 at: 'December') description: 'Value at December differs'.
55   t assert: (d1 at: 'January' ifAbsent: [t signalFailureDescription: 'at:ifAbsent: block should not get evaluated here']) = 31 description: 'Unexpected value at January'.
57   t assert: (d1 keyAtValue: 29) = (d2 keyAtValue: 29) description: 'keyAtValue: 29 differs'.
59   t assert: (d1 includes: 29) description: 'd1 includes: 29 failed'.
60   t deny: (d1 includes: 19) description: 'd1 includes: 19'.
62   t assert: (d1 includesKey: 'January') description: 'd1 includesKey: \'January\' failed'.
63   t deny: (d1 includesKey: 'foobar') description: 'd1 includesKey: \'foobar\''.
65   t assert: (d1 occurrencesOf: 30) = 4 description: '(d1 occurrencesOf: 30) ~= 4'.
68 t@(UnitTests Dictionary traits) testIteration
70   t months1 keysDo:
71     [| :key |
72      t assert: (t months2 at: key) = (t months1 at: key) description: 'An element differs while iterating on keys'].
74   "Unless we can ensure ordering, the following test doesn't make sense. But, if order can be ensured, this test might be useful."
75   "t months1 valuesDo:
76     [| :value |
77       t assert: (t months2 keyAtValue: value) = ( t months1 keyAtValue: value) description: 'An element differs while iterating on values']."
79   t months1 keysAndValuesDo:
80     [| :key :value |
81      t assert: (t months2 at: key) = value description: 'An element differs while iterating on keysAndValues'].
84 t@(UnitTests Dictionary traits) testSize
86   t assert: t months1 size = t months2 size description: 'Sizes differ'.
89 t@(UnitTests Dictionary traits) testEquality
91   t assert: t months1 = t months2 description: 'Equality failed'.
92   t deny: t months1 = t empty description: 'False equality with empty dictionary'.
95 t@(UnitTests Dictionary traits) testMutation
97   d1 ::= t months1 copy.
98   d2 ::= t months2 copy.
100   d1 at: 'January' put: 0.
101   t assert: (d1 occurrencesOf: 31) = 6 description: '(d1 occurrencesOf: 31) ~= 6 after January set to 0'.
102   t deny: (d1 at: 'January') = (d2 at: 'January') description: 'Mutating d1 failed'.
103   t deny: d1 = d2 description: 'd1 = d2 after mutating d1'.
105   d2 at: 'January' put: 0.
106   t assert: d1 = d2 description: 'd1 ~= d2 after mutating d2, too'.
108   d1 remove: 'February'.
109   t deny: d1 = d2 description: 'd1 = d2 after removing February from d1'.
110   t deny: d1 size = d2 size description: 'd1 size = d2 size after removing February from d1'.
112   d2 remove: 'February'.
113   t assert: d1 = d2 description: 'd1 ~= d2 after removing February from d2, too'.
114   t assert: d1 size = d2 size description: 'd1 size ~= d2 size after removing February from d2, too'.
117 t@(UnitTests Dictionary traits) suite
118 [t suiteForSelectors: {
119   #testAccess.
120   #testSignals.
121   #testSize.
122   #testEquality.
123   #testMutation.
124   #testIteration.
127 UnitTests define: #IdentityDictionary &parents: {TestCase}.
129 UnitTests IdentityDictionary list1 ::=
130   (IdentityDictionary newSize: 4) `>>
131     [add: Boolean -> 10.
132      at: True put: 20.
133      add: (Association cloneSettingSlots: #{#key. #value} to: {False. 30}). ].
135 UnitTests IdentityDictionary list2 ::=
136   (IdentityDictionary newSize: 4) `>>
137     [at: Boolean put: 10.
138      add: (Association cloneSettingSlots: #{#key. #value} to: {True. 20}).
139      add: False -> 30. ].
141 UnitTests IdentityDictionary empty ::= IdentityDictionary new.
143 t@(UnitTests IdentityDictionary traits) testSignals
145   d1 ::= t list1.
147   t should: [d1 at: 'invalidKey'] raise: Mapping KeyNotFound
148     description: 'KeyNotFound is not signaled from at:'.
150   t should: [t empty anyOne] raise: Collection IsEmpty
151     description: 'IsEmpty is not signaled from anyOne'.
154 t@(UnitTests IdentityDictionary traits) testAccess
156   d1 ::= t list1.
157   d2 ::= t list2.
159   t assert: (d1 at: Boolean) = (d2 at: Boolean) description: 'Value at Boolean differs'.
160   t assert: (d1 at: True) = (d2 at: True) description: 'Value at True differs'.
162   t assert: (d1 at: False ifAbsent: [t signalFailureDescription: 'at:ifAbsent: block should not get evaluated here']) = 30 description: 'Unexpected value at January'.
164   t assert: (d1 keyAtValue: 30) = (d2 keyAtValue: 30) description: 'keyAtValue: 30 differs'.
166   t assert: (d1 includes: 20) description: 'd1 includes: 20 failed'.
167   t deny: (d1 includes: 100) description: 'd1 includes: 100'.
169   t assert: (d1 includesKey: True) description: 'd1 includesKey: True failed'.
170   t deny: (d1 includesKey: Dictionary) description: 'd1 includesKey: Dictionary'.
172   t assert: (d1 occurrencesOf: 30) = 1 description: '(d1 occurrencesOf: 30) ~= 1'.
175 t@(UnitTests IdentityDictionary traits) testIteration
177   t list1 keysDo:
178     [| :key |
179      t assert: (t list2 at: key) = (t list1 at: key) description: 'An element differs while iterating on keys'].
181   t list1 valuesDo:
182     [| :value |
183      t assert: (t list2 keyAtValue: value) = ( t list1 keyAtValue: value) description: 'An element differs while iterating on values'].
185   t list1 keysAndValuesDo:
186     [| :key :value |
187      t assert: (t list2 at: key) = value description: 'An element differs while iterating on keysAndValues'].
190 t@(UnitTests IdentityDictionary traits) testSize
192   t assert: t list1 size = t list2 size description: 'Sizes differ'.
195 t@(UnitTests IdentityDictionary traits) testEquality
197   t assert: t list1 = t list2 description: 'Equality failed'.
198   t deny: t list1 = t empty description: 'False equality with empty dictionary'.
201 t@(UnitTests IdentityDictionary traits) testMutation
203   d1 ::= t list1 copy.
204   d2 ::= t list2 copy.
206   d1 at: False put: 0.
207   t assert: (d1 occurrencesOf: 30) = 0 description: '(d1 occurrencesOf: 30) ~= 0 after False (which was set to 30) was set to 0'.
208   t deny: (d1 at: False) = (d2 at: False) description: 'Mutating d1 failed'.
209   t deny: d1 = d2 description: 'd1 = d2 after mutating d1'.
211   d2 at: False put: 0.
212   t assert: d1 = d2 description: 'd1 ~= d2 after mutating d2, too'.
214   d1 remove: Boolean.
215   t deny: d1 = d2 description: 'd1 = d2 after removing Boolean from d1'.
216   t deny: d1 size = d2 size description: 'd1 size = d2 size after removing Boolean from d1'.
218   d2 remove: Boolean.
219   t assert: d1 = d2 description: 'd1 ~= d2 after removing Boolean from d2, too'.
220   t assert: d1 size = d2 size description: 'd1 size ~= d2 size after removing Boolean from d2, too'.
223 t@(UnitTests IdentityDictionary traits) suite
224 [t suiteForSelectors: {
225   #testSignals.
226   #testAccess.
227   #testIteration.
228   #testSize.
229   #testEquality.
230   #testMutation.