-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
362 lines (331 loc) · 10.3 KB
/
tests.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"use strict"
test("forasync end promise", function() {
let result = forasync([], function() { })
if(typeof(result.then) != "function" || typeof(result.catch) != "function") {
throw("result should be a promise but is " + result)
} })
test("forasync sync iterable", function(pass, fail) {
let result1 = []
forasync([ 0, 1, 2 ], function(result2) {
result1.push(result2)
}).then(function() {
if(JSON.stringify(result1) == "[0,1,2]") {
pass()
} else {
fail("result should be [ 0, 1, 2 ] but is " + result1)
} }) })
test("forasync async iterable", function(pass, fail) {
let result1 = []
let result2 = asyncgen(function*() {
yield(0)
yield(1)
yield(2)
})
forasync(result2(), function(result3) {
result1.push(result3)
}).then(function() {
if(JSON.stringify(result1) == "[0,1,2]") {
pass()
} else {
fail("result should be [ 0, 1, 2 ] but is " + result1)
} }) })
test("forasync noniterable", function() {
try {
let result1 = forasync(true, function(...inputs) {
throw("should throw but executes callback with " + inputs)
})
throw("should throw but returns " + result1)
} catch(error) { }
})
test("forasync current iteration parameter", function(pass, fail) {
let result1 = []
forasync([ 0, 1, 2 ], function(result2, stop, current) {
result1.push(current)
}).then(function() {
if(JSON.stringify(result1) == "[1,2,3]") {
pass()
} else {
fail("result should be [ 1, 2, 3 ] but is " + result1)
} }) })
test("forasync original reference parameter", function(pass, fail) {
let result1 = [ 1, 2, 3 ], result2 = []
forasync(result1, function(result3, stop, current, original) {
result2.push(original)
}).then(function() {
if(result2[0] == result1 && result2[1] == result1) {
pass()
} else {
fail("result should be [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ] but is " + result2)
} }) })
test("forasync stopping iteration", function(pass, fail) {
let result1 = []
forasync([ 0, 1, 2, 3, 4 ], function(result2, stop) {
if(result2 == 2) {
let result3 = stop("value")
if(result3 != "value") {
fail("stop should return 'value' but returns " + result3)
} }
result1.push(result2)
}).then(function() {
if(JSON.stringify(result1) == "[0,1,2]") {
pass()
} else {
fail("result should be [ 0, 1, 2 ] but is " + result1)
} }) })
test("forasync results promise", function(pass, fail) {
forasync([ 0, 1, 2 ], function(result1) {
return(result1 + 2)
}).then(function(result2) {
if(JSON.stringify(result2) == "[2,3,4]") {
pass()
} else {
fail("result should be [ 2, 3, 4 ] but is " + result2)
} }) })
test("forasync callback sync error", function(pass, fail) {
forasync([ 0, 1, 2 ], function() {
throw("value")
}).then(function(result) {
fail("should reject but resolves with " + result)
}).catch(function(error) {
if(error == "value") {
pass()
} else {
fail('error should be "value" but is ' + error)
} }) })
test("forasync callback async error", function(pass, fail) {
forasync([ 0, 1, 2 ], function() {
return(Promise.resolve().then(function() {
return(Promise.reject("value"))
}))
}).then(function(result) {
fail("should reject but resolves with " + result)
}).catch(function(error) {
if(error == "value") {
pass()
} else {
fail('error should be "value" but is ' + error)
} }) })
test("object.forasync", function(pass, fail) {
let result1 = { key1: "value1", key2: "value2" }
result1.forasync(function(...result2) {
if(JSON.stringify(result2) != '["key1","value1",null,1,{"key1":"value1","key2":"value2"}]') {
fail(`callback parameters should be [ "key", "value", stop(), 1, ${result1} ] but are ` + result2)
}
return(result2[2]("value"))
}).then(function(result3) {
if(JSON.stringify(result3) == '["value"]') {
pass()
} else {
fail('forasync result should be "value" but is ' + result3)
} }) })
test("array.forasync", function(pass, fail) {
let result1 = [ "value1", "value2" ]
result1.forasync(function(...result2) {
if(JSON.stringify(result2) != '["value1",0,null,1,["value1","value2"]]') {
fail(`callback parameters should be [ "value", 0, stop(), 1, ${result1} ] but are ` + result2)
}
return(result2[2]("value"))
}).then(function(result3) {
if(JSON.stringify(result3) == '["value"]') {
pass()
} else {
fail('forasync result should be "value" but is ' + result3)
} }) })
test("string.forasync", function(pass, fail) {
let result1 = "value"
result1.forasync(function(...result2) {
if(JSON.stringify(result2) != '["v",0,null,1,"value"]') {
fail('callback parameters should be [ "v", 0, stop(), 1, "value" ] but are ' + result2)
}
return(result2[2]("value"))
}).then(function(result3) {
if(JSON.stringify(result3) == '["value"]') {
pass()
} else {
fail('forasync result should be "value" but is ' + result3)
} }) })
test("number.forasync", function(pass, fail) {
let result1 = 4
result1.forasync(function(...result2) {
if(JSON.stringify(result2) != '[1,null,1,4]') {
fail('callback parameters should be [ 1, stop(), 1, 4 ] but are ' + result2)
}
return(result2[1]("value"))
}).then(function(result3) {
if(JSON.stringify(result3) == '["value"]') {
pass()
} else {
fail('foreach result should be "value" but is ' + result3)
} }) })
test("asyncgen toString", function() {
let result1 = asyncgen(function*() { })
let result2 = result1.toString()
if(result2 != "async function* () { }") {
throw('should return "async function* () { }" but returns ' + result2)
} })
test("asyncgen returns iterator", function() {
let result1 = asyncgen(function*() { })
let result2 = result1()
if(typeof(result2.next) != "function") {
throw('should return an iterator but returns ' + result2)
} })
test("asyncgen this value", function(pass, fail) {
let result = asyncgen(function*() {
if(this == "value") {
pass()
} else {
fail('should be "value" but is ' + this)
} })
result.call("value").next()
})
test("asyncgen arguments", function(pass, fail) {
let result = asyncgen(function*(value1, value2) {
if(value1 == "value1" && value2 == "value2") {
pass()
} else {
fail('should be "value1" and "value2" but is ' + [ value1, value2 ])
} })
result("value1", "value2").next()
})
test("asyncgen returning values ends iteration", function(pass, fail) {
let result1 = asyncgen(function*() {
return("value")
})
result1().next().then(function(result2) {
if(result2.done && result2.value == "value") {
pass()
} else {
fail('result should be { done: true, value: "value" } but is ' + result2)
} }) })
test("asyncgen throwing errors ends iteration", function(pass, fail) {
let result = asyncgen(function*() {
throw("value")
})
result().next().then(function(result) {
fail('should reject but resolved with ' + result)
}).catch(function(error) {
if(error == "value") {
pass()
} else {
fail('error should be "value" but is ' + error)
} }) })
test("asyncgen yield awaits promise", function(pass, fail) {
let result1 = asyncgen(function*() {
let result2 = yield(Promise.resolve("value"))
if(result2 == "value") {
pass()
} else {
fail('result should be "value" but is ' + result2)
} })
result1().next()
})
test("asyncgen yield passes values", function(pass, fail) {
let result1 = asyncgen(function*() {
let result2 = yield("value")
})
result1().next().then(function(result3) {
if(result3.value == "value") {
pass()
} else {
fail('result should be "value" but is' + result3.value)
} }) })
test("asyncgen multiple yield awaits", function(pass, fail) {
let result1 = asyncgen(function*() {
let result2 = yield(Promise.resolve("value1"))
let result3 = yield(Promise.resolve("value2"))
if(result2 == "value1" && result3 == "value2") {
pass()
} else {
fail('results should be "value1" and "value2" but are ' + [ result2, result3 ])
} })
result1().next()
})
test("asyncgen multiple yield results", function(pass, fail) {
let result1 = asyncgen(function*() {
let result2 = yield("value1")
let result3 = yield("value2")
if(result2 == "value1" && result3 == "value2") {
pass()
} else {
fail('results should be "value1" and "value2" but are ' + [ result2, result3 ])
} })
let result2 = result1()
result2.next().then(function(result3) {
return(result2.next(result3.value))
}).then(function(result4) {
return(result2.next(result4.value))
}) })
test("asyncgen yield result, await, and throw", function(pass, fail) {
let result1 = asyncgen(function*() {
let result2 = yield(Promise.resolve("value"))
let result3 = yield(result2)
throw(result3)
})
let result4 = result1()
result4.next().then(function(result5) {
return(result4.next(result5.value))
}).then(function(result6) {
fail('should reject but resolved with ' + result6)
}).catch(function(error) {
if(error == "value") {
pass()
} else {
fail('error should be "value" but is ' + error)
} }) })
test("asyncgen try and catch", function(pass, fail) {
let result1 = asyncgen(function*() {
try {
let result2 = yield(Promise.reject("value"))
fail('should reject but resolved with ' + result2)
} catch(error) {
if(error == "value") {
pass()
} else {
fail('error should be "value" but is ' + error)
} } })
let result2 = result1()
result2.next().then(result2.next)
})
test("asyncgen nested yield", function(pass, fail) {
let result1 = asyncgen(function*() {
yield(yield(Promise.resolve("value")))
})
result1().next().then(function(result2) {
if(result2.value == "value") {
pass()
} else {
fail('result should be "value" but is ' + result2.value)
} }) })
test("asyncgen external throw", function(pass, fail) {
let result1 = asyncgen(function*() {
let result2 = yield("value")
fail("should throw but returns with " + result2)
})
let result3 = result1()
result3.next().then(function(result4) {
return(result3.throw(result4.value))
}).then(function(result5) {
fail("should reject but resolve with " + result5)
}).catch(function(error) {
if(error == "value") {
pass()
} else {
fail('error should be "value" but is ' + error)
} }) })
test("asyncgen external return", function(pass, fail) {
let result2 = asyncgen(function*() {
try {
let result3 = yield("value1")
fail("should return early but continues with " + result3)
} finally {
return("value2")
} })
let result4 = result2()
result4.next().then(function(result5) {
return(result4.return(result5.value))
}).then(function(result6) {
if(result6.value == "value2") {
pass()
} else {
fail('result should be "value2" but is ' + result6.value)
} }) })