Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: test/mjsunit/harmony/promise-prototype-finally.js

Issue 2695753002: [ESnext] Implement Promise.prototype.finally (Closed)
Patch Set: add tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/builtins/builtins-promise.cc ('K') | « src/objects-printer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-promise-finally --allow-natives-syntax
6
7 var asyncAssertsExpected = 0;
8
9 function assertUnreachable() {
10 %AbortJS("Unreachable: failure");
11 }
12
13 function assertAsyncRan() {
14 ++asyncAssertsExpected;
15 }
16
17 function assertAsync(b, s) {
18 if (b) {
19 print(s, "succeeded");
20 } else {
21 %AbortJS(s + " FAILED!");
22 }
23 --asyncAssertsExpected;
24 }
25
26 function assertEqualsAsync(b, s) {
27 if (b === s) {
28 print(b, "===", s, "succeeded");
29 } else {
30 %AbortJS(b + "===" + s + " FAILED!");
31 }
32 --asyncAssertsExpected;
33 }
34
35 function assertAsyncDone(iteration) {
36 var iteration = iteration || 0;
37 %EnqueueMicrotask(function() {
38 if (asyncAssertsExpected === 0)
39 assertAsync(true, "all");
40 else if (
41 iteration > 10 // Shouldn't take more.
42 )
43 assertAsync(false, "all... " + asyncAssertsExpected);
44 else
45 assertAsyncDone(iteration + 1);
46 });
47 }
48
49 (function() {
50 assertThrows(
51 function() {
52 Promise.prototype.finally.call(5);
53 },
54 TypeError
55 );
56 })();
57
58 // resolve/finally/then
59 (function() {
60 Promise.resolve(3).finally().then(
61 x => {
62 assertEqualsAsync(3, x);
63 },
64 assertUnreachable
65 );
66 assertAsyncRan();
67 })();
68
69 // reject/finally/then
70 (function() {
71 Promise.reject(3).finally().then(assertUnreachable, x => {
72 assertEqualsAsync(3, x);
73 });
74 assertAsyncRan();
75 })();
76
77 // reject/finally/catch
78 (function() {
79 Promise.reject(3).finally().catch(reason => {
80 assertEqualsAsync(3, reason);
81 });
82 assertAsyncRan();
83 })();
84
85 // reject/finally/then/catch
86 (function() {
87 Promise.reject(3).finally().then(assertUnreachable).catch(reason => {
88 assertEqualsAsync(3, reason);
89 });
90 assertAsyncRan();
91 })();
92
93 // resolve/then/finally/then
94 (function() {
95 Promise.resolve(3)
96 .then(x => {
97 assertEqualsAsync(3, x);
98 return x;
99 })
100 .finally()
101 .then(
102 x => {
103 assertEqualsAsync(3, x);
104 },
105 assertUnreachable
106 );
107 assertAsyncRan();
108 assertAsyncRan();
109 })();
110
111 // reject/catch/finally/then
112 (function() {
113 Promise.reject(3)
114 .catch(x => {
115 assertEqualsAsync(3, x);
116 return x;
117 })
118 .finally()
119 .then(
120 x => {
121 assertEqualsAsync(3, x);
122 },
123 assertUnreachable
124 );
125 assertAsyncRan();
126 assertAsyncRan();
127 })();
128
129 // resolve/finally-throw/then
130 (function() {
131 Promise.resolve(3)
132 .finally(function onFinally() {
133 assertAsync(arguments.length === 0);
134 throw 1;
135 })
136 .then(assertUnreachable, function onRejected(reason) {
137 assertEqualsAsync(1, reason);
138 });
139 assertAsyncRan();
140 assertAsyncRan();
141 })();
142
143 // reject/finally-throw/then
144 (function() {
145 Promise.reject(3)
146 .finally(function onFinally() {
147 assertAsync(arguments.length === 0);
148 throw 1;
149 })
150 .then(assertUnreachable, function onRejected(reason) {
151 assertEqualsAsync(1, reason);
152 });
153 assertAsyncRan();
154 assertAsyncRan();
155 })();
156
157 // resolve/finally-return/then
158 (function() {
159 Promise.resolve(3)
160 .finally(function onFinally() {
161 assertAsync(arguments.length === 0);
162 return 4;
163 })
164 .then(
165 x => {
166 assertEqualsAsync(x, 3);
167 },
168 assertUnreachable
169 );
170 assertAsyncRan();
171 assertAsyncRan();
172 })();
173
174 // reject/finally-return/then
175 (function() {
176 Promise.reject(3)
177 .finally(function onFinally() {
178 assertAsync(arguments.length === 0);
179 return 4;
180 })
181 .then(assertUnreachable, x => {
182 assertEqualsAsync(x, 3);
183 });
184 assertAsyncRan();
185 assertAsyncRan();
186 })();
187
188 // reject/catch-throw/finally-throw/then
189 (function() {
190 Promise.reject(3)
191 .catch(e => {
192 assertEqualsAsync(3, e);
193 throw e;
194 })
195 .finally(function onFinally() {
196 assertAsync(arguments.length === 0);
197 throw 4;
198 })
199 .then(assertUnreachable, function onRejected(e) {
200 assertEqualsAsync(4, e);
201 });
202 assertAsyncRan();
203 assertAsyncRan();
204 assertAsyncRan();
205 })();
206
207 // resolve/then-throw/finally-throw/then
208 (function() {
209 Promise.resolve(3)
210 .then(e => {
211 assertEqualsAsync(3, e);
212 throw e;
213 })
214 .finally(function onFinally() {
215 assertAsync(arguments.length === 0);
216 throw 4;
217 })
218 .then(assertUnreachable, function onRejected(e) {
219 assertEqualsAsync(4, e);
220 });
221 assertAsyncRan();
222 assertAsyncRan();
223 assertAsyncRan();
224 })();
225
226 // resolve/finally-return-rejected-promise/then
227 (function() {
228 Promise.resolve(3)
229 .finally(function onFinally() {
230 assertAsync(arguments.length === 0);
231 return Promise.reject(4);
232 })
233 .then(assertUnreachable, e => {
234 assertEqualsAsync(4, e);
235 });
236 assertAsyncRan();
237 assertAsyncRan();
238 })();
239
240 // reject/finally-return-rejected-promise/then
241 (function() {
242 Promise.reject(3)
243 .finally(function onFinally() {
244 assertAsync(arguments.length === 0);
245 return Promise.reject(4);
246 })
247 .then(assertUnreachable, e => {
248 assertEqualsAsync(4, e);
249 });
250 assertAsyncRan();
251 assertAsyncRan();
252 })();
253
254 // resolve/finally-return-resolved-promise/then
255 (function() {
256 Promise.resolve(3)
257 .finally(function onFinally() {
258 assertAsync(arguments.length === 0);
259 return Promise.resolve(4);
260 })
261 .then(
262 x => {
263 assertEqualsAsync(3, x);
264 },
265 assertUnreachable
266 );
267 assertAsyncRan();
268 assertAsyncRan();
269 })();
270
271 // reject/finally-return-resolved-promise/then
272 (function() {
273 Promise.reject(3)
274 .finally(function onFinally() {
275 assertAsync(arguments.length === 0);
276 return Promise.resolve(4);
277 })
278 .then(assertUnreachable, e => {
279 assertEqualsAsync(3, e);
280 });
281 assertAsyncRan();
282 assertAsyncRan();
283 })();
284
285 // reject/finally-return-resolved-promise/then
286 (function() {
287 Promise.reject(3)
288 .finally(function onFinally() {
289 assertAsync(arguments.length === 0);
290 return Promise.resolve(4);
291 })
292 .then(assertUnreachable, e => {
293 assertEqualsAsync(3, e);
294 });
295 assertAsyncRan();
296 assertAsyncRan();
297 })();
298
299 // resolve/finally-thenable-resolve/then
300 (function() {
301 var thenable = {
302 then: function(onResolve, onReject) {
303 onResolve(5);
304 }
305 };
306
307 Promise.resolve(5)
308 .finally(function onFinally() {
309 assertAsync(arguments.length === 0);
310 return thenable;
311 })
312 .then(
313 x => {
314 assertEqualsAsync(5, x);
315 },
316 assertUnreachable
317 );
318
319 assertAsyncRan();
320 assertAsyncRan();
321 })();
322
323 // reject/finally-thenable-resolve/then
324 (function() {
325 var thenable = {
326 then: function(onResolve, onReject) {
327 onResolve(1);
328 }
329 };
330
331 Promise.reject(5)
332 .finally(function onFinally() {
333 assertAsync(arguments.length === 0);
334 return thenable;
335 })
336 .then(assertUnreachable, e => {
337 assertEqualsAsync(5, e);
338 });
339
340 assertAsyncRan();
341 assertAsyncRan();
342 })();
343
344 // reject/finally-thenable-reject/then
345 (function() {
346 var thenable = {
347 then: function(onResolve, onReject) {
348 onReject(1);
349 }
350 };
351
352 Promise.reject(5)
353 .finally(function onFinally() {
354 assertAsync(arguments.length === 0);
355 return thenable;
356 })
357 .then(assertUnreachable, e => {
358 assertEqualsAsync(1, e);
359 });
360
361 assertAsyncRan();
362 assertAsyncRan();
363 })();
364
365 // resolve/finally-thenable-reject/then
366 (function() {
367 var thenable = {
368 then: function(onResolve, onReject) {
369 onReject(1);
370 }
371 };
372
373 Promise.resolve(5)
374 .finally(function onFinally() {
375 assertAsync(arguments.length === 0);
376 return thenable;
377 })
378 .then(assertUnreachable, e => {
379 assertEqualsAsync(1, e);
380 });
381
382 assertAsyncRan();
383 assertAsyncRan();
384 })();
385
386 // resolve/finally/finally/then
387 (function() {
388 Promise.resolve(5)
389 .finally(function onFinally() {
390 assertAsync(arguments.length === 0);
391 })
392 .finally(function onFinally() {
393 assertAsync(arguments.length === 0);
394 })
395 .then(
396 x => {
397 assertEqualsAsync(5, x);
398 },
399 assertUnreachable
400 );
401
402 assertAsyncRan();
403 assertAsyncRan();
404 assertAsyncRan();
405 })();
406
407 // resolve/finally-throw/finally/then
408 (function() {
409 Promise.resolve(5)
410 .finally(function onFinally() {
411 assertAsync(arguments.length === 0);
412 throw 1;
413 })
414 .finally(function onFinally() {
415 assertAsync(arguments.length === 0);
416 })
417 .then(assertUnreachable, e => {
418 assertEqualsAsync(1, e);
419 });
420
421 assertAsyncRan();
422 assertAsyncRan();
423 assertAsyncRan();
424 })();
425
426 // resolve/finally-return-rejected-promise/finally/then
427 (function() {
428 Promise.resolve(5)
429 .finally(function onFinally() {
430 assertAsync(arguments.length === 0);
431 return Promise.reject(1);
432 })
433 .finally(function onFinally() {
434 assertAsync(arguments.length === 0);
435 })
436 .then(assertUnreachable, e => {
437 assertEqualsAsync(1, e);
438 });
439
440 assertAsyncRan();
441 assertAsyncRan();
442 assertAsyncRan();
443 })();
444
445 // reject/finally/finally/then
446 (function() {
447 Promise.reject(5)
448 .finally(function onFinally() {
449 assertAsync(arguments.length === 0);
450 })
451 .finally(function onFinally() {
452 assertAsync(arguments.length === 0);
453 })
454 .then(assertUnreachable, e => {
455 assertEqualsAsync(5, e);
456 });
457
458 assertAsyncRan();
459 assertAsyncRan();
460 assertAsyncRan();
461 })();
462
463 // reject/finally-throw/finally/then
464 (function() {
465 Promise.reject(5)
466 .finally(function onFinally() {
467 assertAsync(arguments.length === 0);
468 throw 1;
469 })
470 .finally(function onFinally() {
471 assertAsync(arguments.length === 0);
472 })
473 .then(assertUnreachable, e => {
474 assertEqualsAsync(1, e);
475 });
476
477 assertAsyncRan();
478 assertAsyncRan();
479 assertAsyncRan();
480 })();
481
482 // reject/finally-return-rejected-promise/finally/then
483 (function() {
484 Promise.reject(5)
485 .finally(function onFinally() {
486 assertAsync(arguments.length === 0);
487 return Promise.reject(1);
488 })
489 .finally(function onFinally() {
490 assertAsync(arguments.length === 0);
491 })
492 .then(assertUnreachable, e => {
493 assertEqualsAsync(1, e);
494 });
495
496 assertAsyncRan();
497 assertAsyncRan();
498 assertAsyncRan();
499 })();
500
501 // resolve/finally-deferred-resolve/then
502 (function() {
503 var resolve, reject;
504 var deferred = new Promise((x, y) => {
505 resolve = x;
506 reject = y;
507 });
508 Promise.resolve(1)
509 .finally(function onFinally() {
510 assertAsync(arguments.length === 0);
511 return deferred;
512 })
513 .then(
514 x => {
515 assertEqualsAsync(1, x);
516 },
517 assertUnreachable
518 );
519
520 assertAsyncRan();
521 assertAsyncRan();
522
523 resolve(5);
524 })();
525
526 // resolve/finally-deferred-reject/then
527 (function() {
528 var resolve, reject;
529 var deferred = new Promise((x, y) => {
530 resolve = x;
531 reject = y;
532 });
533 Promise.resolve(1)
534 .finally(function onFinally() {
535 assertAsync(arguments.length === 0);
536 return deferred;
537 })
538 .then(assertUnreachable, e => {
539 assertEqualsAsync(5, e);
540 });
541
542 assertAsyncRan();
543 assertAsyncRan();
544
545 reject(5);
546 })();
547
548 // all/finally/then
549 (function() {
550 var resolve, reject;
551 var deferred = new Promise((x, y) => {
552 resolve = x;
553 reject = y;
554 });
555
556 Promise.all([deferred])
557 .finally(function onFinally() {
558 assertAsync(arguments.length === 0);
559 })
560 .then(
561 ([x]) => {
562 assertEqualsAsync(1, x);
563 },
564 assertUnreachable
565 );
566
567 assertAsyncRan();
568 assertAsyncRan();
569
570 resolve(1);
571 })();
572
573 // race/finally/then
574 (function() {
575 var resolve, reject;
576 var d1 = new Promise((x, y) => {
577 resolve = x;
578 reject = y;
579 });
580 var d2 = new Promise((x, y) => {
581 resolve = x;
582 reject = y;
583 });
584
585 Promise.race([d1, d2])
586 .finally(function onFinally() {
587 assertAsync(arguments.length === 0);
588 })
589 .then(
590 x => {
591 assertEqualsAsync(1, x);
592 },
593 assertUnreachable
594 );
595
596 assertAsyncRan();
597 assertAsyncRan();
598
599 resolve(1);
600 })();
601
602 var descriptor = Object.getOwnPropertyDescriptor(Promise.prototype, 'finally');
603 assertTrue(descriptor.writable);
604 assertTrue(descriptor.configurable);
605 assertFalse(descriptor.enumerable);
606
607 assertAsyncDone();
OLDNEW
« src/builtins/builtins-promise.cc ('K') | « src/objects-printer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698