UNPKG

2.94 kBJavaScriptView Raw
1const test = require('./config').test
2const { chain } = require('..')
3
4test('chain over single CPS function', t => {
5 const cpsFun = cb => cb(42)
6 const cpsNew = chain(x => cb => cb(x*2))(cpsFun)
7 // 84 passed as output into the first callback
8 cpsNew(t.cis(84))
9})
10
11test('chain over single CPS function with several arguments', t => {
12 // (5, 2) passed as output into the first callback
13 const cpsFun = cb => cb(5, 2)
14 const cpsNew = chain(
15 (a, b) => cb => cb(a - b)
16 )(cpsFun)
17 // new output is 3 = 5 - 2
18 cpsNew(t.cis(3))
19})
20
21test('all repeated outputs are passed', t => {
22 const cpsFun = cb => { cb(42); cb(42) }
23 const cpsNew = chain(x => cb => cb(x + 1))(cpsFun)
24 // the callback t.cis(43) must be executed twice
25 t.plan(2)
26 cpsNew(t.cis(43))
27})
28
29test('also all repeated outputs passed from transforming functions', t => {
30 const cpsFun = cb => { cb(42) }
31 const cpsNew = chain(
32 // cb is called twice - 2 outputs
33 x => cb => cb(x + 1) + cb(x + 1)
34 )(cpsFun)
35 // the callback t.cis(43) must be executed twice
36 t.plan(2)
37 cpsNew(t.cis(43))
38})
39
40test('chain over single function with no arguments', t => {
41 // empty tuple as output
42 const cpsFun = cb => cb()
43 // transform empty tuple into 30 as output
44 const cpsNew = chain(
45 () => cb => cb(30)
46 )(cpsFun)
47 // new output is 30
48 cpsNew(t.cis(30))
49})
50
51test('all callbacks passed when chain with single function', t => {
52 // 42 is passed as output
53 const cpsFun = cb => cb(42)
54 const cpsNew = chain(
55 x => (cb1, cb2) => {
56 cb1(x * 2)
57 cb2(x + 10)
58 })(cpsFun)
59 // 84 passed into the first, and 52 into the second callback
60 t.plan(2)
61 cpsNew(t.cis(84), t.cis(52))
62})
63
64test('chain over multiple functions with the same output twice', t => {
65 // 42 and 10.5 passed respectively into the first and second callback
66 const cpsFun = (cb1, cb2) => cb1(42) + cb2(10.5)
67 // both output are transformed into the same result
68 const cpsNew = chain(
69 x => cb => cb(x/2),
70 x => cb => cb(x*2)
71 )(cpsFun)
72 t.plan(2)
73 cpsNew(t.cis(21))
74})
75
76test('chain over multiple functions merges the outputs', t => {
77 // one output for each callback
78 let called = false
79 const cpsFun = (cb1, cb2) => { cb1(2); cb2(5) }
80 const newCps = chain(
81 // output 42 is passed here
82 x => cb => cb(x/2),
83 // output 21 is passed here
84 x => cb => cb(x*2)
85 )(cpsFun)
86
87 // called twice - with 21 and 42 as outputs
88 t.plan(2)
89 newCps(res => {
90 t.cis(called ? 10 : 1)(res)
91 called = true
92 })
93})
94
95test('multiple callbacks from transforming functions merge by index', t => {
96 const cpsFun = (cb1, cb2) => { cb1(8); cb2(2) }
97 const newCps = chain(
98 // output 8 is passed here as x
99 x => (c1, c2) => c1(x/2) + c2(x),
100 // output 2 is passed here as x
101 x => (cb1, cb2) => cb1(x*2) + cb2(x*4),
102 )(cpsFun)
103
104 // each callback is called twice
105 t.plan(4)
106 newCps(
107 // 4 = 8/2 = 2*2 is passed twice, once from each of c1 and cb1
108 t.cis(4),
109 // 8 = 8 = 2*4 is passed twice, once from each of c2 and cb2
110 t.cis(8)
111 )
112})