Invalid flags in a RegExp literal should be an early SyntaxError
[WebKit-https.git] / JSTests / stress / async-iteration-yield-promise.js
1 var assert = function (result, expected, message = "") {
2   if (result !== expected) {
3     throw new Error('Error in assert. Expected "' + expected + '" but was "' + result + '":' + message );
4   }
5 };
6
7 const Logger = function () {
8     var log = [];
9
10     this.logEvent = (type, value, done) => {
11         log.push({ type, value, done});
12     };
13     this.logFulfilledEvent = (value, done) => {
14         this.logEvent('fulfilled', value, done);
15     };
16     this.logRejectEvent = error => {
17         this.logEvent('reject', error.toString(), true);
18     };
19     this.logCatchEvent = value => {
20         this.logEvent('catch', value, true);
21     };
22     this.getLogger = () => log;
23
24     this.clear = () => {
25         log = [];
26     }
27 };
28
29 const fulfillSpy = logger => result => logger.logFulfilledEvent(result.value, result.done);
30 const rejectSpy = logger => error => logger.logRejectEvent(error);
31 const catchSpy = logger => error => logger.logCatchEvent(error);
32
33 const assertLogger = function (loggerObject) {
34     const logger = loggerObject.getLogger();
35
36     var _assertLogger = function () {
37         let index = 0;
38
39         const isNotOutOfLength = () => {
40             assert(index < logger.length, true, `Index is greater then log length`);   
41         }
42
43         this.fullfilled = function (expectedValue, message = 'on fulfill') {
44             isNotOutOfLength();
45
46             const msg = `step: ${index} - ${message}`;
47             let step = logger[index];
48
49             assert(step.type, 'fulfilled', msg);
50             assert(step.value, expectedValue, msg);
51             assert(step.done, false, msg);
52
53             index++;
54             return this;
55         };
56
57         this.fullfilledDone = function (expectedValue, message = 'on fulfill with done true') {
58             isNotOutOfLength();
59
60             const msg = `step: ${index} - ${message}`;
61             let step = logger[index];
62
63             assert(step.type, 'fulfilled', msg);
64             assert(step.value, expectedValue, msg);
65             assert(step.done, true, msg);
66
67             index++;
68             return this;
69         };
70
71         this.rejected = function (error, message = 'on reject') {
72             isNotOutOfLength();
73
74             const msg = `step: ${index} - ${message}`;
75             let step = logger[index];
76
77             assert(step.type, 'reject', msg);
78             assert(step.value, error.toString(), msg);
79             assert(step.done, true, msg);
80
81             index++;
82             return this;
83         };
84
85         this.catched = function (expectedError, message = 'on catch') {
86             isNotOutOfLength();
87
88             const msg = `step: ${index} - ${message}`;
89             let step = logger[index];
90
91             assert(step.type, 'catch', msg);
92             assert(step.value, expectedError, msg);
93             assert(step.done, true, msg);
94
95             index++;
96             return this;
97         };
98
99         this.isFinal = function (message = '') {
100             assert(index, logger.length, `expected final step: ${message}`);
101         }; 
102     }; 
103     
104     return new _assertLogger();
105 };
106
107 const getPromise = promiseHolder => {
108     return new Promise((resolve, reject) => {
109         promiseHolder.resolve = resolve;
110         promiseHolder.reject = reject;
111     });
112 };
113
114 var logger = new Logger();
115 const someValue = 'some-value';
116 const errorMessage = 'error-message';
117 const promiseHolder = {};
118
119 async function * foo(val) {
120     let reply = yield '0:' + val;
121     reply = yield getPromise(promiseHolder);
122     return 'end foo:' + reply;
123 }
124
125 let f = foo(someValue);
126
127 f.next(someValue + ':0').then(fulfillSpy(logger));
128 f.next(someValue + ':1').then(fulfillSpy(logger));
129 f.next(someValue + ':2').then(fulfillSpy(logger));
130
131 drainMicrotasks();
132
133 assertLogger(logger)
134     .fullfilled('0:' + someValue)
135     .isFinal();
136
137 promiseHolder.resolve('1');
138
139 drainMicrotasks();
140
141 assertLogger(logger)
142     .fullfilled('0:' + someValue)
143     .fullfilled('1')
144     .fullfilledDone('end foo:' +  someValue + ':2')
145     .isFinal();
146
147 logger.clear();
148
149 f = foo('init');
150
151 f.next('0').then(fulfillSpy(logger), rejectSpy(logger));
152 f.next('1').then(fulfillSpy(logger), rejectSpy(logger));
153 f.next('2').then(fulfillSpy(logger), rejectSpy(logger));
154
155 drainMicrotasks();
156
157 assertLogger(logger)
158     .fullfilled('0:init')
159     .isFinal();
160
161 promiseHolder.reject('1');
162
163 drainMicrotasks();
164
165 assertLogger(logger)
166     .fullfilled('0:init')
167     .rejected('1')
168     .fullfilledDone()
169     .isFinal();
170
171 logger.clear();
172
173 f = foo('init');
174
175 f.next('0').then(fulfillSpy(logger)).catch(catchSpy(logger));
176 f.next('1').then(fulfillSpy(logger)).catch(catchSpy(logger));
177 f.next('2').then(fulfillSpy(logger)).catch(catchSpy(logger));
178
179 drainMicrotasks();
180
181 assertLogger(logger)
182     .fullfilled('0:init')
183     .isFinal();
184
185 promiseHolder.reject('1');
186
187 drainMicrotasks();
188
189 assertLogger(logger)
190     .fullfilled('0:init')
191     .fullfilledDone(undefined)
192     .catched('1')
193     .isFinal();
194
195 logger.clear();
196
197 const promiseHolder1 = {};
198 const promiseHolder2 = {};
199
200 async function * boo() {
201     const promise1 = getPromise(promiseHolder1);
202     const promise2 = getPromise(promiseHolder2);
203     yield '0';
204     yield promise1;
205     yield '1';
206     yield promise2;
207     return 'end foo';
208 }
209
210 let b = boo();
211
212 b.next().then(fulfillSpy(logger)).catch(catchSpy(logger));
213 b.next().then(fulfillSpy(logger)).catch(catchSpy(logger));
214 b.next().then(fulfillSpy(logger)).catch(catchSpy(logger));
215 b.next().then(fulfillSpy(logger)).catch(catchSpy(logger));
216 b.next().then(fulfillSpy(logger)).catch(catchSpy(logger));
217
218 drainMicrotasks();
219
220 assertLogger(logger)
221     .fullfilled('0')
222     .isFinal();
223
224 promiseHolder1.resolve('#1');
225
226 drainMicrotasks();
227
228 assertLogger(logger)
229     .fullfilled('0')
230     .fullfilled('#1')
231     .fullfilled('1')
232     .isFinal();
233
234 promiseHolder2.resolve('#2');
235 drainMicrotasks();
236
237 assertLogger(logger)
238     .fullfilled('0')
239     .fullfilled('#1')
240     .fullfilled('1')
241     .fullfilled('#2')
242     .fullfilledDone('end foo')
243     .isFinal();
244
245
246 logger.clear();
247
248 b = boo();
249
250 b.next().then(fulfillSpy(logger), rejectSpy(logger));
251 b.next().then(fulfillSpy(logger), rejectSpy(logger));
252 b.next().then(fulfillSpy(logger), rejectSpy(logger));
253 b.next().then(fulfillSpy(logger), rejectSpy(logger));
254 b.next().then(fulfillSpy(logger), rejectSpy(logger));
255
256 drainMicrotasks();
257
258 assertLogger(logger)
259     .fullfilled('0')
260     .isFinal();
261
262 promiseHolder1.resolve('#1');
263
264 drainMicrotasks();
265
266 assertLogger(logger)
267     .fullfilled('0')
268     .fullfilled('#1')
269     .fullfilled('1')
270     .isFinal();
271
272 promiseHolder2.reject('#2');
273
274 drainMicrotasks();
275
276 assertLogger(logger)
277     .fullfilled('0')
278     .fullfilled('#1')
279     .fullfilled('1')
280     .rejected('#2')
281     .fullfilledDone()
282     .isFinal();
283
284 logger.clear();
285
286 b = boo();
287
288 b.next().then(fulfillSpy(logger), rejectSpy(logger));
289 b.next().then(fulfillSpy(logger), rejectSpy(logger));
290 b.next().then(fulfillSpy(logger), rejectSpy(logger));
291 b.next().then(fulfillSpy(logger), rejectSpy(logger));
292 b.next().then(fulfillSpy(logger), rejectSpy(logger));
293
294 drainMicrotasks();
295
296 assertLogger(logger)
297     .fullfilled('0')
298     .isFinal();
299
300 promiseHolder1.reject('#1');
301 promiseHolder2.resolve('#2');
302
303 drainMicrotasks();
304
305 assertLogger(logger)
306     .fullfilled('0')
307     .rejected('#1')
308     .fullfilledDone()
309     .fullfilledDone()
310     .fullfilledDone()
311     .isFinal();
312
313 logger.clear();
314
315 b = boo();
316
317 b.next().then(fulfillSpy(logger), rejectSpy(logger));
318 b.next().then(fulfillSpy(logger), rejectSpy(logger));
319 b.next().then(fulfillSpy(logger), rejectSpy(logger));
320 b.next().then(fulfillSpy(logger), rejectSpy(logger));
321 b.next().then(fulfillSpy(logger), rejectSpy(logger));
322
323 drainMicrotasks();
324
325 assertLogger(logger)
326     .fullfilled('0')
327     .isFinal();
328
329 promiseHolder2.reject('#2');
330 promiseHolder1.reject('#1');
331
332 drainMicrotasks();
333
334 assertLogger(logger)
335     .fullfilled('0')
336     .rejected('#1')
337     .fullfilledDone()
338     .fullfilledDone()
339     .fullfilledDone()
340     .isFinal();
341
342 async function * bar() {
343   yield '0';
344   yield getPromise(promiseHolder1);
345   throw new Error(errorMessage);
346   yield getPromise(promiseHolder2);
347   return 'end foo';
348 }
349
350 logger.clear();
351 b = bar();
352
353 b.next().then(fulfillSpy(logger), rejectSpy(logger));
354 b.next().then(fulfillSpy(logger), rejectSpy(logger));
355 b.next().then(fulfillSpy(logger), rejectSpy(logger));
356 b.next().then(fulfillSpy(logger), rejectSpy(logger));
357
358 drainMicrotasks();
359
360 assertLogger(logger)
361     .fullfilled('0')
362     .isFinal();
363  
364
365 promiseHolder1.resolve('#1');
366
367 drainMicrotasks();
368
369 assertLogger(logger)
370     .fullfilled('0')
371     .fullfilled('#1')
372     .rejected(new Error(errorMessage))
373     .fullfilledDone(undefined)
374     .isFinal();
375
376 logger.clear();
377 b = bar();
378
379 b.next().then(fulfillSpy(logger), rejectSpy(logger));
380 b.next().then(fulfillSpy(logger), rejectSpy(logger));
381 b.next().then(fulfillSpy(logger), rejectSpy(logger));
382 b.next().then(fulfillSpy(logger), rejectSpy(logger));
383
384 drainMicrotasks();
385
386 assertLogger(logger)
387     .fullfilled('0')
388     .isFinal();
389
390 promiseHolder1.reject('#1');
391
392 drainMicrotasks();
393
394 assertLogger(logger)
395     .fullfilled('0')
396     .rejected('#1')
397     .fullfilledDone()
398     .fullfilledDone()
399     .isFinal();
400
401 logger.clear();
402 b = bar();
403
404 b.next().then(fulfillSpy(logger), rejectSpy(logger));
405 b.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
406 b.next().then(fulfillSpy(logger), rejectSpy(logger));
407
408 drainMicrotasks();
409
410 assertLogger(logger)
411     .fullfilled('0')
412     .fullfilledDone(someValue)
413     .fullfilledDone(undefined)
414     .isFinal();
415
416 promiseHolder1.resolve('#1');
417
418 drainMicrotasks();
419
420 assertLogger(logger)
421     .fullfilled('0')
422     .fullfilledDone(someValue)
423     .fullfilledDone(undefined)
424     .isFinal();
425
426 logger.clear();
427 b = bar();
428
429 b.next().then(fulfillSpy(logger), rejectSpy(logger));
430 b.next().then(fulfillSpy(logger), rejectSpy(logger));
431 b.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
432
433 drainMicrotasks();
434
435 assertLogger(logger)
436     .fullfilled('0')
437     .isFinal();
438
439 promiseHolder1.resolve('#1');
440
441 drainMicrotasks();
442
443 assertLogger(logger)
444     .fullfilled('0')
445     .fullfilled("#1")
446     .fullfilledDone(someValue)
447     .isFinal();
448
449 logger.clear();
450 b = bar();
451
452 b.next().then(fulfillSpy(logger), rejectSpy(logger));
453 b.next().then(fulfillSpy(logger), rejectSpy(logger));
454 b.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
455
456 drainMicrotasks();
457
458 assertLogger(logger)
459     .fullfilled('0')
460     .isFinal();
461
462 promiseHolder1.reject('#1');
463
464 drainMicrotasks();
465
466 assertLogger(logger)
467     .fullfilled('0')
468     .rejected("#1")
469     .fullfilledDone(someValue)
470     .isFinal();