-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.test.mjs
160 lines (134 loc) · 3.03 KB
/
exec.test.mjs
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
import { test } from 'tap'
import api from './api.mjs'
import exec from './exec.mjs'
const module = {
add: (a, b) => a + b,
identity: x => x,
concat: (a, b) => a.concat(b),
toArray: (...array) => array,
always: x => () => x,
multiply3: a => b => c => a * b * c,
map: fn => array => array.map(fn),
filter: pred => array => array.filter(pred),
hello: 'world',
noop: () => {},
resolve: x => Promise.resolve(x),
asyncFn: async () => 'my-async-function',
instanceOf: (x, t) => x instanceof t,
Function,
Promise,
}
test('should evaluate basic expressions correctly', async (t) => {
const evaluate = exec(module)
const { resolve, Function, Promise, instanceOf, promise, asyncFn, hello, map, toArray, add, concat, identity, always, multiply3 } = api(
Object.keys(module),
// Emulate serialization and de-serialization
async (program) => JSON.parse(JSON.stringify(await evaluate(program)))
)
t.strictSame(
await identity([
identity(1),
identity(2),
identity(3),
identity(4),
]),
[ 1,2,3,4 ]
);
t.rejects(async () => {
await evaluate('["ref", "notDefined"]')
})
t.equal(
await identity(null),
null
)
// Somewhat unexpected but JSON.stringify changes undefined to null when it
// is in an object
t.equal(JSON.stringify([undefined]), '[null]')
t.equal(
await identity(undefined),
null
)
// Consider using a different JSON.stringify if you want to support
// stringify-ing undefined.
t.rejects(async () => {
await noop()
})
t.equal(
await hello,
'world'
)
t.equal(
await add(1, 2),
3
)
t.equal(
await identity(1),
1
)
t.strictSame(
await identity([]),
[]
)
// Objects
t.strictSame(
await identity({ a: identity(1), b: always(2)() }),
{
a: 1,
b: 2
}
)
t.strictSame(
await concat([1, 2], [[add(1, 2)]]),
[1, 2, [3]]
)
t.strictSame(
await concat([1, 2], [add(1, 2), add(2, 2)]),
[1, 2, 3, 4]
)
t.strictSame(
await identity([identity(1), 2, 3]),
[1, 2, 3]
)
// Function that returns a function.
t.strictSame(
await always('hello')(),
'hello'
)
// Point free style supported.
t.strictSame(
await map(identity)([1, 2, 3, 4]),
[1, 2, 3, 4]
)
t.strictSame(
await multiply3(1)(2)(3),
6
)
t.strictSame(
await identity(['ref', 'doesNotExist']),
['ref', 'doesNotExist']
)
// Yes yes yes, this works now! Thank you quote keyword.
t.strictSame(
await identity(['ref', 'hello']),
await identity(toArray('ref', 'hello'))
)
t.strictSame(
await asyncFn(),
'my-async-function'
)
t.strictSame(
await resolve('my-promise'),
'my-promise'
)
// TODO: Supports passing a promise as a function argument
// t.strictSame(
// await instanceOf(resolve(asyncFn), Promise),
// true
// )
// TODO: Supports awaiting a promise returned from a function call
// t.strictSame(
// await instanceOf(await resolve(asyncFn), Function),
// true
// )
t.end()
})