-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.ts
59 lines (52 loc) · 1.25 KB
/
test.ts
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
import { test } from 'ava'
import { lazy } from './'
test('it gives an array', t => {
const fibs = lazy([0, 1])(_ => fibs[_ - 1] + fibs[_ - 2])
t.is(fibs.length, 2)
})
test('it support shorthand syntax', t => {
const evens = lazy(_ => _ * 2)
t.is(evens[5], 10)
})
test('it lazy-computes values', t => {
const fibs = lazy([0, 1])(_ => fibs[_ - 1] + fibs[_ - 2])
t.is(fibs[10], 55)
t.is(fibs.length, 11)
})
test('it efficiently caches previous computations', t => {
let calls = 0
const fibs = lazy([0, 1])(_ => {
calls++
return fibs[_ - 1] + fibs[_ - 2]
})
fibs[10]
t.is(calls, 9)
fibs[10]
t.is(calls, 9)
fibs[11]
t.is(calls, 10)
})
test('returns whether or not a given index has been computed', t => {
const fibs = lazy([0, 1])(_ => fibs[_ - 1] + fibs[_ - 2])
t.is(1 in fibs, true)
t.is(5 in fibs, false)
fibs[10]
t.is(5 in fibs, true)
})
test('deletes the given index', t => {
let calls = 0
const fibs = lazy([0, 1])(_ => {
calls++
return fibs[_ - 1] + fibs[_ - 2]
})
fibs[10]
t.is(calls, 9)
delete fibs[10]
t.is(calls, 9)
fibs[10]
t.is(calls, 10)
})
test('it does not allow directly setting values', t => {
const fibs = lazy([0, 1])(_ => fibs[_ - 1] + fibs[_ - 2])
t.throws(() => fibs[5] = 8)
})