-
Notifications
You must be signed in to change notification settings - Fork 114
/
nel-spec.js
352 lines (305 loc) · 12.6 KB
/
nel-spec.js
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
describe('A Non-Empty immutable list', function () {
var NEL = Monet.NEL
var List = Monet.List
var Nil = Monet.Nil
var Some = Monet.Some
var None = Monet.None
var sideEffectsReceiver = null;
function expectCalls(spiedCall, values) {
expect(spiedCall.calls.all().length).toEqual(values.length)
for (var i = 0; i < values.length; i++) {
expect(spiedCall.calls.all()[i].args[0]).toEqual(values[i])
}
}
beforeEach(function () {
jasmine.addMatchers({
toBeSomeMaybe: getCustomMatcher(function (actual) {
return actual.isSome();
}),
toBeSomeMaybeWith: getCustomMatcher(function (actual, expected) {
return actual.some() == expected;
}),
toBeNoneMaybe: getCustomMatcher(function (actual) {
return actual.isNone();
})
});
sideEffectsReceiver = {
setVal: function (val) {
}
};
spyOn(sideEffectsReceiver, 'setVal');
});
var nonEmptyList = NEL(1, List(2, List(3, List(4, Nil))))
var listBase = [1, 2, 3, 4]
var plus = function (a, b) {
return a + b
}
it('should check equality', function () {
expect(NEL(1).equals(NEL(1))).toBe(true)
expect(NEL(1, Nil).equals(NEL(1))).toBe(true)
expect(NEL(1).equals(NEL(1, Nil))).toBe(true)
expect(
NEL(1, List(2)).equals(NEL(1, List(2, Nil)))
).toBe(true)
expect(NEL(1).equals(NEL([1, 1]))).toBe(false)
expect(NEL(1, Nil).equals(NEL(2))).toBe(false)
expect(NEL(1, List(1)).equals(NEL(1, Nil))).toBe(false)
expect(NEL(1, List(2)).equals(NEL(1), List(1, Nil))).toBe(false)
expect(NEL(1, List(2)).equals(NEL(1), List(2, List(3)))).toBe(false)
})
it('cannot be create with zero elements', function () {
expect(function () {
new NEL()
}).toThrow(new Error('Cannot create an empty Non-Empty List. Passed head is undefined.'))
})
it('must have a head', function () {
expect(new NEL(1, Nil).head()).toEqual(1)
})
it('should be created from list', function () {
expect(NEL.fromList(List.fromArray(listBase)).equals(Some(nonEmptyList))).toBe(true)
})
it('should be created from array', function () {
expect(NEL.fromArray(listBase).equals(Some(nonEmptyList))).toBe(true)
})
it('should be created from any iterable (array)', function () {
expect(NEL.from(listBase).equals(Some(nonEmptyList))).toBe(true)
})
it('should be created from any iterable (set)', function () {
expect(NEL.from(new Set(listBase)).equals(Some(nonEmptyList))).toBe(true)
})
it('should have a ".to()" operator', function () {
expect(nonEmptyList.to(Array.from)).toEqual(listBase)
expect(NEL.from(nonEmptyList.to(Array.from))).toEqual(Some(nonEmptyList))
expect(NEL.from(listBase).some().to(Array.from)).toEqual(listBase)
})
it('Must be mappable', function () {
expect(new NEL(1, Nil).map(function (a) {
return a + 1
}).toArray()).toEqual([2])
})
it('will be transformed by a flatMap', function () {
expect(nonEmptyList.flatMap(function (e) {
return NEL(e * e, List(e + e))
}).toArray()).toEqual([1, 2, 4, 4, 9, 6, 16, 8])
})
it('can be reversed', function () {
expect(nonEmptyList.reverse().toArray()).toEqual([4, 3, 2, 1])
expect(NEL(1, Nil).reverse().toArray()).toEqual([1])
})
it('allows side-effects with the forEach function', function () {
nonEmptyList.forEach(sideEffectsReceiver.setVal);
expectCalls(sideEffectsReceiver.setVal, [1, 2, 3, 4])
})
it('can be created from a list', function () {
expect(NEL.fromList(List.fromArray([1, 2, 3, 4])).some().toArray()).toEqual([1, 2, 3, 4])
expect(NEL.fromList(List())).toBeNoneMaybe()
})
it('can be converted to a List', function () {
expect(nonEmptyList.toList()).toEqual(List.fromArray([1, 2, 3, 4]))
})
it('can be converted to an Array', function () {
expect(nonEmptyList.toArray()).toEqual([1, 2, 3, 4])
})
it('can be converted to a Set', function () {
expect(nonEmptyList.toSet()).toEqual(new Set([1, 2, 3, 4]))
})
it('will return a NEL of NELs for tails()', function () {
expect(nonEmptyList.tails().map(function (m) {
return m.toArray()
}).toArray()).toEqual([
[1, 2, 3, 4],
[2, 3, 4],
[3, 4],
[4]
])
})
it('should be an Iterable', function () {
var onIter = jasmine.createSpy('onIteration')
for (var a of nonEmptyList) {
onIter(a)
}
expect(onIter.calls.allArgs()).toEqual([[1], [2], [3], [4]])
})
it('will map a function over the tails with cobind', function () {
expect(nonEmptyList.cobind(function (nel) {
return nel.foldLeft(0)(function (a, b) {
return a + b
})
}).toArray()).toEqual([10, 9, 7, 4])
})
it('will append two NELs together', function () {
var n1 = NEL.fromList(List.fromArray([1, 2, 3, 4])).some()
var n2 = NEL.fromList(List.fromArray([5, 6, 7, 8])).some()
var nAppend = n1.append(n2)
expect(nAppend.isNEL).toBeTruthy()
expect(nAppend.size()).toBe(8)
expect(nAppend.toArray()).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
})
describe('will flatten inner lists', function () {
it('with two elements', function () {
expect(
NEL.fromList(List.fromArray([
List.fromArray([1, 2]),
List.fromArray([3, 4]),
])).some().flatten().toArray()
).toEqual([1, 2, 3, 4])
expect(
NEL.fromList(List.fromArray([
NEL.fromList(List.fromArray([1, 2])).some(),
NEL.fromList(List.fromArray([3, 4])).some(),
])).some().join().toArray()
).toEqual([1, 2, 3, 4])
})
it('with one element', function () {
expect(
NEL.fromList(List.fromArray([List.fromArray([1, 2])])).some().flatten().toArray()
).toEqual([1, 2])
expect(
NEL.fromList(List.fromArray([
NEL.fromList(List.fromArray([1, 2])).some()
])).some().join().toArray()
).toEqual([1, 2])
})
})
describe('flattenMaybe', function () {
it(`should drop Nones and return List of Some values`, function () {
expect(
NEL.fromList(List.fromArray([Some(1), None(), Some(10)]))
.some()
.flattenMaybe()
.toArray()
).toEqual([1, 10])
})
})
it('will be filtered', function () {
var n1 = NEL.fromList(List.fromArray([1, 2, 3, 4, 5, 6, 7, 8])).some()
expect(n1.filter(function (a) {
return a % 2 == 0
}).toArray()).toEqual([2, 4, 6, 8])
})
it('will be filtered with filterNot', function () {
var n1 = NEL.fromList(List.fromArray([1, 2, 3, 4, 5, 6, 7, 8])).some()
expect(n1.filterNot(function (a) {
return a % 2 == 0
}).toArray()).toEqual([1, 3, 5, 7])
})
it('can be searched', function () {
var n1 = NEL.fromList(List.fromArray([1, 2, 3, 4, 5, 6, 7, 8])).some()
expect(n1.find(function (a) {
return a % 2 == 0
})).toBeSomeMaybeWith(2)
})
it('can be checked for not containing a value', function () {
var n1 = NEL.fromList(List.fromArray([1, 2, 3])).some()
expect(n1.contains(4)).toBe(false)
})
it('can be checked for containing a value', function () {
var n1 = NEL.fromList(List.fromArray([1, 2, 3])).some()
expect(n1.contains(3)).toBe(true)
})
it('can be reversed using foldLeft and cons', function () {
expect(nonEmptyList.foldLeft(Nil)(function (acc, e) {
return acc.cons(e)
}).toArray()).toEqual([4, 3, 2, 1])
})
it('can not be reversed using foldRight and cons', function () {
expect(nonEmptyList.foldRight(Nil)(function (e, acc) {
return acc.cons(e)
}).toArray()).toEqual([1, 2, 3, 4])
})
describe('cons', function () {
const cba = NEL('b', List('a')).cons('c')
it('should add elements', function () {
expect(cba.isNEL()).toBe(true)
expect(cba.toArray().join('')).toBe('cba')
})
})
describe('snoc', function () {
const abc = NEL('a', List('b')).snoc('c')
it('should add elements', function () {
expect(abc.isNEL()).toBe(true)
expect(abc.toArray().join('')).toBe('abc')
})
})
it('can be reduced using reduceLeft', function () {
expect(nonEmptyList.reduceLeft(plus)).toEqual(10)
})
it('single element NEL will reduce to the single element', function () {
expect(NEL(1, Nil).reduceLeft(plus)).toEqual(1)
})
it('will render as NEL(x1, x2, x3)', function () {
expect(NEL(1, NEL(false, Nil)).inspect()).toBe('NEL(1, false)')
})
describe('NEL.isInstance', function () {
it('will return true for NEL instances', function () {
expect(NEL.isInstance(NEL(1))).toBe(true)
})
it('will return false for other monads', function () {
expect(NEL.isInstance(Monet.Validation.Success({}))).toBe(false)
expect(NEL.isInstance(Monet.Validation.Fail({}))).toBe(false)
expect(NEL.isInstance(Monet.List.fromArray([]))).toBe(false)
})
it('will return false on non-monads', function () {
expect(NEL.isInstance({})).toBe(false)
expect(NEL.isInstance(true)).toBe(false)
expect(NEL.isInstance(false)).toBe(false)
expect(NEL.isInstance('foo')).toBe(false)
})
})
describe('can be described with collection predicates', function () {
var predicateAll = function (e) {
return e < 5
}
var predicateSome = function (e) {
return e < 3
}
var predicateNone = function (e) {
return e === -12
}
describe('every / forall', function () {
it('should test if all elements match predicate', function () {
expect(nonEmptyList.every(predicateAll)).toBe(true)
expect(nonEmptyList.every(predicateSome)).toBe(false)
expect(nonEmptyList.every(predicateNone)).toBe(false)
expect(nonEmptyList.every(predicateAll)).toBe(nonEmptyList.forall(predicateAll))
expect(nonEmptyList.every(predicateSome)).toBe(nonEmptyList.forall(predicateSome))
expect(nonEmptyList.every(predicateNone)).toBe(nonEmptyList.forall(predicateNone))
})
})
describe('exists', function () {
it('should test if any element matches predicate', function () {
expect(nonEmptyList.exists(predicateAll)).toBe(true)
expect(nonEmptyList.exists(predicateSome)).toBe(true)
expect(nonEmptyList.exists(predicateNone)).toBe(false)
})
})
})
describe('NEL.lookup', function () {
it('will return the corresponding element when present', function () {
var nel = NEL.fromList(List.fromArray([1, 2, 3, 4, 5])).some()
expect(nel.lookup(3)).toEqual(Some(4))
})
it('will return None if there is no corresponding element', function () {
var nel = NEL.fromList(List.fromArray([1, 2, 3, 4, 5])).some()
expect(nel.lookup(5)).toEqual(None())
})
it('will return None if the corresponding element is undefined', function () {
var nel = NEL.fromList(List.fromArray([1, 2, 3, 4, 5, undefined])).some()
expect(nel.lookup(5)).toEqual(None())
})
})
describe('NEL.nth', function () {
it('will return the corresponding element when present', function () {
var nel = NEL.fromList(List.fromArray([1, 2, 3, 4, 5])).some()
expect(nel.nth(3)).toEqual(4)
})
it('will return undefined if there is no corresponding element', function () {
var nel = NEL.fromList(List.fromArray([1, 2, 3, 4, 5])).some()
expect(nel.nth(5)).toEqual(undefined)
})
it('will return undefined if the corresponding element is undefined', function () {
var nel = NEL.fromList(List.fromArray([1, 2, 3, 4, 5, undefined])).some()
expect(nel.nth(5)).toEqual(undefined)
})
})
})