-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.mjs
More file actions
197 lines (174 loc) · 4.89 KB
/
index.test.mjs
File metadata and controls
197 lines (174 loc) · 4.89 KB
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
import { test } from 'tap'
import Sendscript from './index.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,
asyncAdd: async (a, b) => a + b,
aPromise: Promise.resolve(42),
delayedIdentity: async (x) => x,
Function,
Promise
}
const sendscript = Sendscript(module)
const { parse, stringify } = sendscript
const run = (program) => parse(stringify(program))
const RealPromise = Promise
test('should evaluate basic expressions correctly', async (t) => {
const {
aPromise,
asyncAdd,
resolve,
delayedIdentity,
noop,
Function,
Promise,
instanceOf,
asyncFn,
hello,
map,
toArray,
add,
concat,
identity,
always,
multiply3
} = sendscript.module
t.test('nested await works', async (t) => {
// Async identity passthrough
const resolvedId = await delayedIdentity
t.equal(await run(resolvedId('X')), 'X')
t.end()
})
t.test('deep nested awaits', async (t) => {
const nested = async () => await RealPromise.resolve(await delayedIdentity('deep'))
t.equal(await run(await nested()), 'deep')
t.end()
})
t.test('awaits in nested array structure', async (t) => {
const arr = [
await resolve(1),
[await resolve(2), [await resolve(3)]],
await delayedIdentity(4)
]
t.same(await run(arr), [1, [2, [3]], 4])
t.end()
})
t.test('awaits in deeply nested object structure', async (t) => {
const obj = {
a: await resolve('a'),
b: {
c: await delayedIdentity('c'),
d: {
e: await resolve('e')
}
}
}
t.same(await run(obj), {
a: 'a',
b: {
c: 'c',
d: { e: 'e' }
}
})
t.end()
})
t.test('await as computed value inside nested async function', async (t) => {
const asyncOuter = async () => {
const val = await delayedIdentity('nested')
return val
}
t.equal(await run(await asyncOuter()), 'nested')
t.end()
})
// return t.end()
t.test('promise resolution', async (t) => {
t.equal(await run(identity(await aPromise)), 42)
t.strictSame(await run(asyncFn()), 'my-async-function')
t.strictSame(await run(await resolve('my-promise')), 'my-promise')
t.strictSame(run(instanceOf(resolve(asyncFn), Promise)), true)
t.strictSame(
await run(instanceOf(await resolve(asyncFn), Function)), true
)
t.strictSame(
await run({ a: await resolve('b') }),
{ a: 'b' }
)
})
await t.test('async and promise handling', async (t) => {
// Await inside run input
const resolvedAdd = await RealPromise.resolve(asyncAdd)
t.equal(await run(resolvedAdd(2, 3)), 5)
// Using asyncFn in a nested structure
const nestedAsync = async () => await asyncFn()
t.equal(await run(await nestedAsync()), 'my-async-function')
// Awaiting inside object structure
t.same(await run({
type: 'response',
data: await resolve('some-data')
}), {
type: 'response',
data: 'some-data'
})
t.end()
})
t.test('basic types and identity', (t) => {
t.equal(run(identity(null)), null)
t.equal(run(identity(undefined)), null)
t.equal(run(noop()), undefined)
t.equal(run(identity(1)), 1)
t.strictSame(run(identity([])), [])
t.strictSame(run(identity([identity(1), 2, 3])), [1, 2, 3])
t.strictSame(run(always('hello')()), 'hello')
t.end()
})
t.test('objects and arrays', (t) => {
t.strictSame(
run(identity({ a: identity(1), b: always(2)(), c: add(1, 2) })),
{ a: 1, b: 2, c: 3 }
)
t.strictSame(run(concat([1, 2], [[add(1, 2)]])), [1, 2, [3]])
t.strictSame(run(concat([1, 2], [add(1, 2), add(2, 2)])), [1, 2, 3, 4])
t.strictSame(run(map(identity)([1, 2, 3, 4])), [1, 2, 3, 4])
t.end()
})
t.test('function composition and currying', (t) => {
t.strictSame(run(multiply3(1)(2)(3)), 6)
t.end()
})
t.test('special cases and errors', (t) => {
t.throws(() => parse('["ref", "notDefined"]'))
t.strictSame(
run(identity(['ref', 'doesNotExist'])),
['ref', 'doesNotExist']
)
t.strictSame(
run(identity(['ref', 'hello'])),
run(identity(toArray('ref', 'hello')))
)
t.end()
})
t.test('primitives and built-ins', (t) => {
t.equal(JSON.stringify([undefined]), '[null]')
t.equal(run(hello), 'world')
t.equal(run(add(1, 2)), 3)
t.end()
})
t.test('identity with arrays', (t) => {
t.strictSame(
run(identity([identity(1), identity(2), identity(3), identity(4)])),
[1, 2, 3, 4]
)
t.end()
})
})