-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.test.ts
284 lines (242 loc) · 7.14 KB
/
index.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { expect, it, vi } from 'vitest'
import { quansync, toGenerator } from '../src'
import { quansync as quansyncMacro } from '../src/macro'
it('basic', async () => {
const add = quansync({
name: 'add',
sync: (a: number, b: number) => a + b,
async: async (a: number, b: number) => {
await new Promise(resolve => setTimeout(resolve, 10))
return a + b
},
})
expect(add.sync(4, 5)).toBe(9)
expect(await add.async(4, 5)).toBe(9)
})
it('generator', async () => {
const add = quansync({
name: 'add',
sync: (a: number, b: number) => a + b,
async: async (a: number, b: number) => {
await new Promise(resolve => setTimeout(resolve, 10))
return a + b
},
})
const toString = quansync({
name: 'toString',
sync: (value: any) => String(value),
async: async (value: any) => {
await new Promise(resolve => setTimeout(resolve, 10))
return String(value)
},
})
const multiply = quansync(function* (a: number, b: number) {
let sum = (yield 0) as number
for (let i = 0; i < b; i++) {
const value = yield* add(sum, a)
sum = value
}
const foo = yield* toString(sum)
return foo
})
expect(multiply.sync(2, 3)).toBe('6')
expect(await multiply.async(4, 5)).toBe('20')
})
it('consume with await', async () => {
const add = quansync({
name: 'add',
sync: (a: number, b: number) => a + b,
async: async (a: number, b: number) => {
await new Promise(resolve => setTimeout(resolve, 10))
return a + b
},
})
await expect(add(2, 3)).resolves.toBe(5)
expect(add.sync(2, 6)).toBe(8)
await expect(add.async(2, 3)).resolves.toBe(5)
})
it('yield optional promise', async () => {
interface Transformer {
transform: (code: string) => string | Promise<string>
}
const transform = quansync(function* (transformers: Transformer[], code: string) {
let current = code
for (const transformer of transformers) {
current = yield* toGenerator(transformer.transform(current))
// ...
}
return current
})
expect(transform.sync([], '')).toBe('')
await expect(transform.async([], '')).resolves.toBe('')
expect(
transform.sync([
{
transform: (code: string) => `${code}1`,
},
], 'foo'),
).toBe('foo1')
expect(() =>
transform.sync([
{
transform: async (code: string) => `${code}1`,
},
], 'foo'),
).toThrowErrorMatchingInlineSnapshot(`[QuansyncError: Unexpected promise in sync context]`)
await expect(
transform.async([
{
transform: async (code: string) => `${code}1`,
},
], 'foo'),
).resolves.toBe('foo1')
})
it('yield promise', async () => {
const run = quansync(function* (code: string) {
const result = yield new Promise<string>((resolve) => {
setTimeout(() => resolve(code), 10)
})
return result
})
expect(() => run.sync('foo'))
.toThrowErrorMatchingInlineSnapshot(`[QuansyncError: Unexpected promise in sync context]`)
await expect(run.async('foo')).resolves.toBe('foo')
})
it('throw errors', async () => {
const throwError = quansync({
name: 'throwError',
sync: () => {
throw new Error('sync error')
},
async: async () => {
throw new Error('async error')
},
})
await expect(throwError()).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: async error]`)
await expect(throwError.async()).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: async error]`)
expect(() => throwError.sync()).toThrowErrorMatchingInlineSnapshot(`[Error: sync error]`)
})
it('handle errors', async () => {
const throwError = quansync({
name: 'throwError',
sync: () => {
throw new Error('sync error')
},
async: async () => {
return Promise.reject(new Error('async error'))
},
})
const returnError = quansync(function* (fn: () => any) {
try {
yield* fn()
}
catch (err) {
return err
}
})
const fn = quansync(function* (fn: () => any) {
return yield* fn()
})
await expect(returnError(throwError)).resolves.toThrowErrorMatchingInlineSnapshot(`[Error: async error]`)
await expect(returnError.async(throwError)).resolves.toThrowErrorMatchingInlineSnapshot(`[Error: async error]`)
expect(returnError.sync(throwError)).toMatchInlineSnapshot(`[Error: sync error]`)
expect(returnError.sync(() => fn(throwError))).toMatchInlineSnapshot(`[Error: sync error]`)
await expect(returnError.async(() => fn(throwError))).resolves.toMatchInlineSnapshot(`[Error: async error]`)
})
it('yield generator', async () => {
const toString = quansync({
name: 'toString',
sync: (value: any) => String(value),
async: async (value: any) => String(value),
})
function* produce() {
yield 1
yield 2
return 3
}
const multiply = quansync(function* () {
const plainGenerator = produce()
expect(yield plainGenerator).toBe(plainGenerator)
const result = (yield toString('str')) as string
expect(result).toBe('str')
return result + (yield* toString('str'))
})
expect(multiply.sync()).toBe('strstr')
await expect(multiply.async()).resolves.toBe('strstr')
})
it('yield toGenerator array', async () => {
const run = quansync(function* () {
const input = ['1', 2, 3]
const result = yield* toGenerator(input)
expect(result).toBe(input)
return result
})
expect(run.sync()).toEqual(['1', 2, 3])
await expect(run.async()).resolves.toEqual(['1', 2, 3])
})
it('handles tail call', async () => {
const echo = quansync({
sync: (v: string) => v,
async: v => Promise.resolve(v),
})
const produce = quansync(() => echo('hello'))
await expect(produce()).resolves.toBe('hello')
await expect(produce.async()).resolves.toBe('hello')
expect(produce.sync()).toBe('hello')
})
it('import macro version', () => {
expect(quansyncMacro).toBe(quansync)
})
it('bind this', async () => {
const obj = quansync({
sync(this: any) {
return this
},
async async(this: any) {
return this
},
})
const fn = quansync(function* (this: any) {
const result = yield* (obj.call(this))
expect(this).toBe(result)
return result
})
class Cls {
sync() {
return fn.sync.call(this)
}
async() {
return fn.async.call(this)
}
async await() {
return await fn.call(this)
}
}
const cls = new Cls()
await expect(cls.await()).resolves.instanceOf(Cls)
await expect(cls.async()).resolves.instanceOf(Cls)
expect(cls.sync()).instanceOf(Cls)
})
it('call onYield hook', async () => {
const onYield = vi.fn(v => v + 1)
const run = quansync(function* () {
expect(yield 1).toBe(2)
expect(yield 2).toBe(3)
}, { onYield })
await run.async()
expect(onYield).toBeCalledTimes(2)
run.sync()
expect(onYield).toBeCalledTimes(4)
// custom error on promise
const run2 = quansync(function* () {
return yield Promise.resolve('foo')
}, {
onYield: (v, isAsync) => {
if (!isAsync && v instanceof Promise)
throw new TypeError('custom error')
return v
},
})
expect(() => run2.sync()).toThrowErrorMatchingInlineSnapshot(`[TypeError: custom error]`)
await expect(run2.async()).resolves.toMatchInlineSnapshot(`"foo"`)
})