/
dict_spec.ik
469 lines (383 loc) · 13.9 KB
/
dict_spec.ik
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
use("ispec")
describe("Dict",
it("should have the correct kind",
Dict should have kind("Dict")
)
it("should be possible to mimic",
x = Dict mimic
x should mimic(Dict)
x should have kind("Dict")
x should not be same(Dict)
)
it("should mimic Enumerable",
Dict should mimic(Mixins Enumerable)
)
it("should mimic Sequenced",
Dict should mimic(Mixins Sequenced)
)
describe("key?",
it("should return false if there is no such key",
{} key?(:foo) should be false
{fox: 123} key?(:foo) should be false
{"foo" => 123} key?(:foo) should be false
)
it("should return true if the key is there",
{foo: 123} key?(:foo) should be true
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:key?, :foo)
)
)
describe("hash",
it("should return the same number for equal dicts",
{} hash should == {} hash
x = {1 => 1}
y = {}
y[1] = 1
x hash should == y hash
{1 => 2} hash should == {1 => 2} hash
)
it("should return different numbers for different dicts",
{1 => 1} hash should not == {"1" => 1} hash
)
it("should do the computation using recursive applications of hash",
x = Origin mimic
y = Origin mimic
{x => 1} hash should not == {y => 1} hash
{1 => x} hash should not == {1 => y} hash
x hash = method(42)
y hash = method(42)
{x => 1} hash should == {y => 1} hash
{1 => x} hash should == {1 => y} hash
)
)
describe("==",
it("should return false when sent an argument that is not a dict",
{} should not == 1
{1=>2} should not == 1
{1=>2,2=>3,3=>4} should not == "foo"
{} should not == fn({})
)
it("should return true for two empty dicts",
x = {}.
x should == x
{} should == {}
)
it("should return true for two empty dicts where one has a new cell",
x = {}
y = {}
x blarg = 12
x should == y
)
it("should return false when the two dicts have a key element of different types",
{1=>2} should not == {"1"=>2}
{1=>2, 2=>3, 3=>4} should not == {"1"=>2, "2"=>3, "3"=>4}
)
it("should return false when the two dicts have different size",
{1} should not == {}
{1} should not == {1,2,3}
)
it("should return true if the elements in the dict are the same",
{1} should == {1=>nil}
{1=>"str"} should == {1=>"str"}
{"1"=>123} should == {"1"=>123}
{1,2,3,4,5,6,7} should == {1,2,3,4,5,6,7}
)
it("should return true if the elements in the dict are the same but in different order",
{1,2,3,4,5,6,7} should == {3,4,5,6,7,1,2,3}
)
it("should do the comparison using recursive applications of ==",
x = Origin mimic
x == = method(o, o == 42)
{x: x} should == {x: 42}
)
)
describe("addKeysAndValues",
it("should add the keys and values provided to the dict",
{} addKeysAndValues([:foo, :bar, :x], [32, 111, 4]) should == {foo: 32, bar: 111, x: 4}
)
it("should only add as many as there are keys",
{} addKeysAndValues([:foo, :bar, :x], [32, 111, 4, 10, 42]) should == {foo: 32, bar: 111, x: 4}
)
it("should return the dict",
v = {}. v addKeysAndValues([], []) should == v
)
)
describe("at",
it("should return nil if empty dict",
dict at(:foo) should be nil
dict at("bar") should be nil
dict at(42) should be nil
)
it("should return nil if argument is over the size",
dict(bar: 42) at(:foo) should be nil
)
it("should an element if it's in the dict",
{foo: 123, 321 => "foo", "bax" => 42} at(:foo) should == 123
{foo: 123, 321 => "foo", "bax" => 42} at(321) should == "foo"
{foo: 123, 321 => "foo", "bax" => 42} at("bax") should == 42
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:at, :foo)
)
)
describe("mergeWith",
it("should take one dictionary and merge that using replacement",
{foo: "bar"} mergeWith({}) should == {foo: "bar"}
{foo: "bar"} mergeWith({qux: 42}) should == {foo: "bar", qux: 42}
{foo: "bar", qux: 16} mergeWith({qux: 42}) should == {foo: "bar", qux: 42}
)
it("should take two argument where the second argument is the code to handle the merge",
{foo: 42} mergeWith({}, +) should == {foo: 42}
{foo: 42} mergeWith({foo: 2}, +) should == {foo: 44}
{foo: 42} mergeWith({bar: 2}, +) should == {foo: 42, bar: 2}
{foo: 42, bar: 25} mergeWith({foo: 2, bar: 2}, +) should == {foo: 44, bar: 27}
)
it("should take three arguments where the second argument is the name of the left hand side object",
{foo: 42} mergeWith({}, x, x +) should == {foo: 42}
{foo: 42} mergeWith({foo: 2}, x, x +) should == {foo: 44}
{foo: 42} mergeWith({bar: 2}, x, x +) should == {foo: 42, bar: 2}
{foo: 42, bar: 25} mergeWith({foo: 2, bar: 2}, x, x +) should == {foo: 44, bar: 27}
)
it("should take four arguments where the second argument is the name of the left hand side object and the third argument is the right hand side object",
{foo: 42} mergeWith({}, x, y, x + y) should == {foo: 42}
{foo: 42} mergeWith({foo: 2}, x, y, x + y) should == {foo: 44}
{foo: 42} mergeWith({bar: 2}, x, y, x + y) should == {foo: 42, bar: 2}
{foo: 42, bar: 25} mergeWith({foo: 2, bar: 2}, x, y, x + y) should == {foo: 44, bar: 27}
)
)
describe("merge",
it("should return an equal dict if given an empty dict",
{foo: "bar"} merge({}) should == {foo: "bar"}
)
it("should return a new dict that contains both of the keys and values",
{foo: "bar"} merge({qux: 42}) should == {foo: "bar", qux: 42}
)
it("should return a new dict that has overwritten the values with the same key",
{foo: "bar", qux: 16} merge({qux: 42}) should == {foo: "bar", qux: 42}
)
it("should take zero or more pairs and add them to the returned dict",
{foo: "bar", qux: 16} merge(:qux => 42) should == {foo: "bar", qux: 42}
{foo: "bar", qux: 16} merge(:qux => 42, {abc: 15}) should == {foo: "bar", qux: 42, abc: 15}
{foo: "bar", qux: 16} merge({abc: 15}, :qux => 42) should == {foo: "bar", qux: 42, abc: 15}
)
it("should take zero or more keyword arguments and add them to the returned dict",
{foo: "bar", qux: 16} merge(qux: 42) should == {foo: "bar", qux: 42}
{foo: "bar", qux: 16} merge(qux: 42, {abc: 15}) should == {foo: "bar", qux: 42, abc: 15}
{foo: "bar", qux: 16} merge({abc: 15}, qux: 42) should == {foo: "bar", qux: 42, abc: 15}
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:merge, {})
)
)
describe("withDefault",
it("should return a new mimic",
x = {}
x withDefault(1) should not be same(x)
{foo: 13} withDefault(42)[:foo] should == 13
)
it("should set the default value for that mimic",
x = {} withDefault(42)
x[:blarg] should == 42
x should == {}
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:withDefault, 42)
)
)
describe("[]",
it("should return nil if empty dict",
dict[:foo] should be nil
dict["bar"] should be nil
dict[42] should be nil
)
it("should return nil if argument is over the size",
dict(bar: 42)[:foo] should be nil
)
it("should an element if it's in the dict",
{foo: 123, 321 => "foo", "bax" => 42}[:foo] should == 123
{foo: 123, 321 => "foo", "bax" => 42}[321] should == "foo"
{foo: 123, 321 => "foo", "bax" => 42}["bax"] should == 42
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:"[]", :foo)
)
)
describe("[]=",
it("should add an element to an empty dict",
x = {}. x[:foo] = :bar. x should == {foo: :bar}
x = {42 => 32}. x[:foo] = :bar. x should == {42 => 32, foo: :bar}
)
it("should overwrite an existing element",
x = {foo: 6666}. x[:foo] = :bar. x should == {foo: :bar}
x = {foo: 6666, 42 => 32}. x[:foo] = :bar. x should == {42 => 32, foo: :bar}
)
it("should return the value set",
({foo: 6666}[42] = :bar) should == :bar
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:"[]=", :foo, :bar)
)
)
describe("keys",
it("should return an empty set for an empty dict",
{} keys should == set()
)
it("should return the one key in an dict with one element",
{foo: 1} keys should == set(:foo)
{1 => :foo} keys should == set(1)
{"str" => :bar} keys should == set("str")
)
it("should return all the keys",
{foo: 1, bar: 2, 3 => :quux} keys should == set(:foo, :bar, 3)
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:keys)
)
)
describe("seq",
it("should return false when calling next? on a seq from an empty dict",
{} seq next? should be false
)
it("should return a Sequence",
{foo: 1} seq should mimic(Sequence)
)
it("should return an object that yields all objects in the dict",
all = {}
ss = {foo: 42, bar: 45, bax: 333} seq
x = ss next. all[x key] = x value
x = ss next. all[x key] = x value
x = ss next. all[x key] = x value
ss next? should be false
all should == {foo: 42, bar: 45, bax: 333}
)
)
describe("each",
it("should return a seq when given no arguments",
x = {} each
x should mimic(Sequence)
)
it("should not do anything for an empty dict",
x = 0
{} each(. x++)
x should == 0
)
it("should be possible to just give it a message chain, that will be invoked on each object",
d = {one: 1, two: 2, three: 3}
Pair y = []
Pair x = method(y << self)
d each(x)
Pair y sort should == [:one => 1, :two => 2, :three => 3] sort
x = 0
{one: 1, two: 2, three: 3} each(nil. x++)
x should == 3
)
it("should be possible to give it an argument name, and code",
y = []
{one: 1, two: 2, three: 3} each(x, y << x)
y sort should == [:one => 1, :two => 2, :three => 3] sort
)
it("should return the object",
y = {one: 1, two: 2, three: 3}
(y each(x, x)) should == y
)
it("should establish a lexical context when invoking the methods. this context will be the same for all invocations.",
{one: 1, two: 2, three: 3} each(xxxx, blargus42=32)
cell?(:xxxx) should be false
cell?(:blargus42) should be false
x=14
{one: 1, two: 2, three: 3} each(x, blarg=32)
x should == 14
)
it("should be possible to give it an extra argument to get the index",
y = []
{one: 1, two: 2, three: 3, four: 4} each(i, x, y << i)
y should == [0,1,2,3]
)
it("should validate type of receiver",
Dict should checkReceiverTypeOn(:each, "foo")
)
)
describe("?|",
it("should just return itself if not empty",
dict(foo: 1) ?|(x/0) should == dict(foo: 1)
dict(foo: 1,bar: 2,quux: 3) ?|(x/0) should == dict(foo: 1,bar: 2,quux: 3)
x = dict(foo: 1,bar: 3)
x ?|(blarg) should be same(x)
)
it("should return the result of evaluating the code if empty",
dict ?|(42) should == 42
dict ?|([1,2,3]) should == [1,2,3]
)
)
describe("?&",
it("should just return itself if empty",
(dict ?& 42) should == dict
(dict ?& [1,2,3]) should == dict
)
it("should return the result of evaluating the code if non-empty",
dict(foo: 1) ?&(10) should == 10
dict(foo: 1,bar: 2,quux: 3) ?&(20) should == 20
x = dict(foo: 1,bar: 3)
x ?&([1,2,3]) should == [1,2,3]
)
)
)
describe(DefaultBehavior,
; These methods will NOT create Pair instances, instead it's cheating - just like keyword arguments
; At some point these methods should also take keyword arguments, interchangably with pairs.
describe("dict",
it("should create a new empty dict when given no arguments",
x = dict
x should be kind("Dict")
x should not be same(Dict)
x should mimic(Dict)
x = dict()
x should be kind("Dict")
x should not be same(Dict)
x should mimic(Dict)
)
it("should create a new dict with the evaluated arguments",
result = dict(1 => 2, "abc" => 3+42, :foo => 123, :bar => "x")
result asList length should == 4
)
it("should create a new dict with keyword arguments",
result = dict(abc: 2, foo: 3+42, bar: 123, quux: "x")
result asList length should == 4
)
it("should take arguments that are not keyword or pairs and add nil as value for them",
dict(123, "foo", 13+2, :bar) should == dict(123=>nil, "foo"=>nil, 15=>nil, :bar => nil)
)
it("should take keyword arguments without a next pointer and add nil as value for them",
dict(foo:, bar:, quux:) should == dict(:foo => nil, :bar => nil, :quux => nil)
)
)
describe("{}",
it("should create a new empty dict when given no arguments",
x = {}
x should be kind("Dict")
x should not be same(Dict)
x should mimic(Dict)
)
it("should create a new dict with the evaluated arguments",
result = {1 => 2, "abc" => 3+42, :foo => 123, :bar => "x"}
result asList length should == 4
)
it("should create a new dict with keyword arguments",
result = {abc: 2, foo: 3+42, bar: 123, quux: "x"}
result asList length should == 4
)
it("should take arguments that are not keyword or pairs and add nil as value for them",
{123, "foo", 13+2, :bar} should == {123=>nil, "foo"=>nil, 15=>nil, :bar => nil}
)
it("should take keyword arguments without a next pointer and add nil as value for them",
{foo:, bar:, quux:} should == {:foo => nil, :bar => nil, :quux => nil}
)
)
describe("size",
it("should be 0 for an empty dict",
dict size should == 0)
it("should be the number of pairs in dict",
dict(a: 1, b: 2) size should == 2)
)
)