1 UnitTests define: #Dictionary &parents: {TestCase}.
2 "TODO only tests base functionality, add SingleValueMap, EmptyMap, etc."
4 UnitTests Dictionary months1 ::=
5 (Dictionary newSize: 12) `>>
7 at: 'February' put: 29.
12 add: (Association cloneSettingSlots: #{#key. #value} to: {'July'. 31}).
14 at: 'September' put: 30.
15 at: 'October' put: 31.
16 at: 'November' put: 30.
17 add: 'December' -> 31. ].
19 UnitTests Dictionary months2 ::=
21 [at: 'January' put: 31.
22 add: 'February' -> 29.
25 add: (Association cloneSettingSlots: #{#key. #value} to: {'May'. 31}).
29 at: 'September' put: 30.
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
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
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
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."
77 t assert: (t months2 keyAtValue: value) = ( t months1 keyAtValue: value) description: 'An element differs while iterating on values']."
79 t months1 keysAndValuesDo:
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: {
127 UnitTests define: #IdentityDictionary &parents: {TestCase}.
129 UnitTests IdentityDictionary list1 ::=
130 (IdentityDictionary newSize: 4) `>>
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}).
141 UnitTests IdentityDictionary empty ::= IdentityDictionary new.
143 t@(UnitTests IdentityDictionary traits) testSignals
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
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
179 t assert: (t list2 at: key) = (t list1 at: key) description: 'An element differs while iterating on keys'].
183 t assert: (t list2 keyAtValue: value) = ( t list1 keyAtValue: value) description: 'An element differs while iterating on values'].
185 t list1 keysAndValuesDo:
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
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'.
212 t assert: d1 = d2 description: 'd1 ~= d2 after mutating d2, too'.
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'.
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: {