Invalid flags in a RegExp literal should be an early SyntaxError
[WebKit-https.git] / JSTests / stress / async-iteration-async-from-sync.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
118 function * foo(value) {
119   let re = yield '1:' + value;
120   re = yield '2:' + re;
121   re = yield '3:' + re;
122   return 'end foo:' + re;
123 }
124
125 async function * boo(value) {
126   let reply = yield '0:' + value;
127   reply = yield* foo(reply);
128   yield '4:' + reply;
129 }
130
131 var b = boo('init');
132 const errorprint = error => print(error);
133 b.next('0').then(fulfillSpy(logger), errorprint);
134 b.next('1').then(fulfillSpy(logger), errorprint);
135 b.next('2').then(fulfillSpy(logger), errorprint);
136 b.next('3').then(fulfillSpy(logger), errorprint);
137 b.next('4').then(fulfillSpy(logger), errorprint);
138 b.next('5').then(fulfillSpy(logger), errorprint);
139
140 drainMicrotasks();
141
142 assertLogger(logger)
143     .fullfilled('0:init')
144     .fullfilled('1:1')
145     .fullfilled('2:2')
146     .fullfilled('3:3')
147     .fullfilled('4:end foo:4')
148     .fullfilledDone(undefined)
149     .isFinal();
150
151 logger.clear();
152 var b2 = boo(':value');
153
154 b2.next(':0').then(fulfillSpy(logger));
155 b2.next(':1').then(fulfillSpy(logger), rejectSpy(logger));
156 b2.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
157 b2.next(':2').then(fulfillSpy(logger));
158 b2.next(':3').then(fulfillSpy(logger));
159 b2.next(':4').then(fulfillSpy(logger));
160
161 drainMicrotasks();
162
163 assertLogger(logger)
164     .fullfilled('0::value')
165     .fullfilled('1::1')
166     .fullfilledDone(someValue)
167     .fullfilledDone(undefined)
168     .fullfilledDone(undefined)
169     .fullfilledDone(undefined)
170     .isFinal();
171
172 logger.clear();
173 var b2 = boo('#value');
174
175 b2.next('#0').then(fulfillSpy(logger), rejectSpy(logger));
176 b2.next('#1').then(fulfillSpy(logger), rejectSpy(logger));
177 b2.next('#2').then(fulfillSpy(logger), rejectSpy(logger));
178 b2.throw(new Error(errorMessage)).then(fulfillSpy(logger), rejectSpy(logger));
179 b2.next('#3').then(fulfillSpy(logger), rejectSpy(logger));
180 b2.next('#4').then(fulfillSpy(logger), rejectSpy(logger));
181
182 drainMicrotasks();
183
184 assertLogger(logger)
185     .fullfilled('0:#value')
186     .fullfilled('1:#1')
187     .fullfilled('2:#2')
188     .rejected(new Error(errorMessage)) // TODO: Check if this correct
189     .fullfilledDone(undefined)
190     .fullfilledDone(undefined)
191     .isFinal();
192
193 function * bar() {
194   yield '1';
195   yield '2';
196   throw new Error(errorMessage);
197   yield '3';
198   return 'end foo';
199 }
200
201 async function * baz() {
202   yield '0';
203   yield* bar();
204   yield '4';
205 }
206
207 logger.clear();
208 var bz1 = baz();
209
210 bz1.next().then(fulfillSpy(logger), rejectSpy(logger));
211 bz1.next().then(fulfillSpy(logger), rejectSpy(logger));
212 bz1.next().then(fulfillSpy(logger), rejectSpy(logger));
213 bz1.next().then(fulfillSpy(logger), rejectSpy(logger));
214 bz1.next().then(fulfillSpy(logger), rejectSpy(logger));
215
216 drainMicrotasks();
217
218 assertLogger(logger)
219     .fullfilled('0')
220     .fullfilled('1')
221     .fullfilled('2')
222     .rejected(new Error(errorMessage))
223     .fullfilledDone(undefined)
224     .isFinal();
225
226 logger.clear();
227 let promiseHolder = {};
228
229 function *joo() {
230     yield '1';
231     yield getPromise(promiseHolder);
232 }
233
234 async function *goo () {
235     yield '0';
236     yield* joo();
237     yield '3';
238 }
239
240 let g = goo();
241
242 g.next().then(fulfillSpy(logger), rejectSpy(logger));
243 g.next().then(fulfillSpy(logger), rejectSpy(logger));
244 g.next().then(fulfillSpy(logger), rejectSpy(logger));
245 g.next().then(fulfillSpy(logger), rejectSpy(logger));
246 g.next().then(fulfillSpy(logger), rejectSpy(logger));
247
248 drainMicrotasks();
249
250 assertLogger(logger)
251     .fullfilled('0')
252     .fullfilled('1')
253     .isFinal();
254
255 promiseHolder.resolve('2');
256
257 drainMicrotasks();
258
259 assertLogger(logger)
260     .fullfilled('0')
261     .fullfilled('1')
262     .fullfilled('2')
263     .fullfilled('3')
264     .fullfilledDone(undefined)
265     .isFinal();
266
267 logger.clear();
268
269 g = goo();
270
271 g.next().then(fulfillSpy(logger), rejectSpy(logger));
272 g.next().then(fulfillSpy(logger), rejectSpy(logger));
273 g.next().then(fulfillSpy(logger), rejectSpy(logger));
274 g.next().then(fulfillSpy(logger), rejectSpy(logger));
275 g.next().then(fulfillSpy(logger), rejectSpy(logger));
276
277 drainMicrotasks();
278
279 assertLogger(logger)
280     .fullfilled('0')
281     .fullfilled('1')
282     .isFinal();
283
284 promiseHolder.reject('#2');
285
286 drainMicrotasks();
287
288 assertLogger(logger)
289     .fullfilled('0')
290     .fullfilled('1')
291     .rejected('#2')
292     .fullfilledDone(undefined)
293     .fullfilledDone(undefined)
294     .isFinal();
295
296 logger.clear();
297
298 g = goo();
299
300 g.next().then(fulfillSpy(logger), rejectSpy(logger));
301 g.next().then(fulfillSpy(logger), rejectSpy(logger));
302 g.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
303 g.next().then(fulfillSpy(logger), rejectSpy(logger));
304 g.next().then(fulfillSpy(logger), rejectSpy(logger));
305
306 drainMicrotasks();
307
308 assertLogger(logger)
309     .fullfilled('0')
310     .fullfilled('1')
311     .fullfilledDone(someValue)
312     .fullfilledDone(undefined)
313     .fullfilledDone(undefined)
314     .isFinal();
315
316 logger.clear();
317
318 g = goo();
319
320 g.next().then(fulfillSpy(logger), rejectSpy(logger));
321 g.next().then(fulfillSpy(logger), rejectSpy(logger));
322 g.next().then(fulfillSpy(logger), rejectSpy(logger));
323 g.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
324 g.next().then(fulfillSpy(logger), rejectSpy(logger));
325 g.next().then(fulfillSpy(logger), rejectSpy(logger));
326
327 drainMicrotasks();
328
329 assertLogger(logger)
330     .fullfilled('0')
331     .fullfilled('1')
332     .isFinal();
333
334 promiseHolder.resolve('#2');
335
336 drainMicrotasks();
337
338 assertLogger(logger)
339     .fullfilled('0')
340     .fullfilled('1')
341     .fullfilled('#2')
342     .fullfilledDone(someValue)
343     .fullfilledDone(undefined)
344     .fullfilledDone(undefined)
345     .isFinal();
346
347 logger.clear();
348
349 g = goo();
350
351 g.next().then(fulfillSpy(logger), rejectSpy(logger));
352 g.next().then(fulfillSpy(logger), rejectSpy(logger));
353 g.next().then(fulfillSpy(logger), rejectSpy(logger));
354 g.return(someValue).then(fulfillSpy(logger), rejectSpy(logger));
355 g.next().then(fulfillSpy(logger), rejectSpy(logger));
356 g.next().then(fulfillSpy(logger), rejectSpy(logger));
357
358 drainMicrotasks();
359
360 assertLogger(logger)
361     .fullfilled('0')
362     .fullfilled('1')
363     .isFinal();
364
365 promiseHolder.reject('#2');
366
367 drainMicrotasks();
368
369 assertLogger(logger)
370     .fullfilled('0')
371     .fullfilled('1')
372     .rejected('#2')
373     .fullfilledDone(someValue)
374     .fullfilledDone(undefined)
375     .fullfilledDone(undefined)
376     .isFinal();
377
378 logger.clear();
379 g = goo();
380
381 g.next().then(fulfillSpy(logger), rejectSpy(logger));
382 g.next().then(fulfillSpy(logger), rejectSpy(logger));
383 g.throw(new Error(errorMessage)).then(fulfillSpy(logger), rejectSpy(logger));
384 g.next().then(fulfillSpy(logger), rejectSpy(logger));
385 g.next().then(fulfillSpy(logger), rejectSpy(logger));
386
387 drainMicrotasks();
388
389 assertLogger(logger)
390     .fullfilled('0')
391     .fullfilled('1')
392     .rejected(new Error(errorMessage))
393     .fullfilledDone(undefined)
394     .fullfilledDone(undefined)
395     .isFinal();
396
397 promiseHolder.resolve('#2');
398
399 drainMicrotasks();
400
401 assertLogger(logger)
402     .fullfilled('0')
403     .fullfilled('1')
404     .rejected(new Error(errorMessage))
405     .fullfilledDone(undefined)
406     .fullfilledDone(undefined)
407     .isFinal();
408
409 logger.clear();
410 g = goo();
411
412 g.next().then(fulfillSpy(logger), rejectSpy(logger));
413 g.next().then(fulfillSpy(logger), rejectSpy(logger));
414 g.next().then(fulfillSpy(logger), rejectSpy(logger));
415 g.throw(new Error(errorMessage)).then(fulfillSpy(logger), rejectSpy(logger));
416 g.next().then(fulfillSpy(logger), rejectSpy(logger));
417 g.next().then(fulfillSpy(logger), rejectSpy(logger));
418
419 drainMicrotasks();
420
421 assertLogger(logger)
422     .fullfilled('0')
423     .fullfilled('1')
424     .isFinal();
425
426 promiseHolder.resolve('#2');
427
428 drainMicrotasks();
429
430 assertLogger(logger)
431     .fullfilled('0')
432     .fullfilled('1')
433     .fullfilled('#2')
434     .rejected(new Error(errorMessage))
435     .fullfilledDone(undefined)
436     .fullfilledDone(undefined)
437     .isFinal();
438
439 logger.clear();
440
441 g = goo();
442
443 g.next().then(fulfillSpy(logger), rejectSpy(logger));
444 g.next().then(fulfillSpy(logger), rejectSpy(logger));
445 g.next().then(fulfillSpy(logger), rejectSpy(logger));
446 g.throw(new Error(errorMessage)).then(fulfillSpy(logger), rejectSpy(logger));
447 g.next().then(fulfillSpy(logger), rejectSpy(logger));
448 g.next().then(fulfillSpy(logger), rejectSpy(logger));
449
450 drainMicrotasks();
451
452 assertLogger(logger)
453     .fullfilled('0')
454     .fullfilled('1')
455     .isFinal();
456
457 promiseHolder.reject('#2');
458
459 drainMicrotasks();
460
461 assertLogger(logger)
462     .fullfilled('0')
463     .fullfilled('1')
464     .rejected('#2')
465     .rejected(new Error(errorMessage))
466     .fullfilledDone(undefined)
467     .fullfilledDone(undefined)
468     .isFinal();