1 | const test = require('./config').test
|
2 | const { chain } = require('..')
|
3 |
|
4 | test('chain over single CPS function', t => {
|
5 | const cpsFun = cb => cb(42)
|
6 | const cpsNew = chain(x => cb => cb(x*2))(cpsFun)
|
7 |
|
8 | cpsNew(t.cis(84))
|
9 | })
|
10 |
|
11 | test('chain over single CPS function with several arguments', t => {
|
12 |
|
13 | const cpsFun = cb => cb(5, 2)
|
14 | const cpsNew = chain(
|
15 | (a, b) => cb => cb(a - b)
|
16 | )(cpsFun)
|
17 |
|
18 | cpsNew(t.cis(3))
|
19 | })
|
20 |
|
21 | test('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 |
|
25 | t.plan(2)
|
26 | cpsNew(t.cis(43))
|
27 | })
|
28 |
|
29 | test('also all repeated outputs passed from transforming functions', t => {
|
30 | const cpsFun = cb => { cb(42) }
|
31 | const cpsNew = chain(
|
32 |
|
33 | x => cb => cb(x + 1) + cb(x + 1)
|
34 | )(cpsFun)
|
35 |
|
36 | t.plan(2)
|
37 | cpsNew(t.cis(43))
|
38 | })
|
39 |
|
40 | test('chain over single function with no arguments', t => {
|
41 |
|
42 | const cpsFun = cb => cb()
|
43 |
|
44 | const cpsNew = chain(
|
45 | () => cb => cb(30)
|
46 | )(cpsFun)
|
47 |
|
48 | cpsNew(t.cis(30))
|
49 | })
|
50 |
|
51 | test('all callbacks passed when chain with single function', t => {
|
52 |
|
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 |
|
60 | t.plan(2)
|
61 | cpsNew(t.cis(84), t.cis(52))
|
62 | })
|
63 |
|
64 | test('chain over multiple functions with the same output twice', t => {
|
65 |
|
66 | const cpsFun = (cb1, cb2) => cb1(42) + cb2(10.5)
|
67 |
|
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 |
|
76 | test('chain over multiple functions merges the outputs', t => {
|
77 |
|
78 | let called = false
|
79 | const cpsFun = (cb1, cb2) => { cb1(2); cb2(5) }
|
80 | const newCps = chain(
|
81 |
|
82 | x => cb => cb(x/2),
|
83 |
|
84 | x => cb => cb(x*2)
|
85 | )(cpsFun)
|
86 |
|
87 |
|
88 | t.plan(2)
|
89 | newCps(res => {
|
90 | t.cis(called ? 10 : 1)(res)
|
91 | called = true
|
92 | })
|
93 | })
|
94 |
|
95 | test('multiple callbacks from transforming functions merge by index', t => {
|
96 | const cpsFun = (cb1, cb2) => { cb1(8); cb2(2) }
|
97 | const newCps = chain(
|
98 |
|
99 | x => (c1, c2) => c1(x/2) + c2(x),
|
100 |
|
101 | x => (cb1, cb2) => cb1(x*2) + cb2(x*4),
|
102 | )(cpsFun)
|
103 |
|
104 |
|
105 | t.plan(4)
|
106 | newCps(
|
107 |
|
108 | t.cis(4),
|
109 |
|
110 | t.cis(8)
|
111 | )
|
112 | })
|