1// Copyright 2014 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24// Flags: --harmony
25'use strict';
26description('Test Promise.all');
27
28var result = undefined;
29
30var p1 = new Promise(function(resolve) { resolve('p1'); });
31var p2 = new Promise(function(resolve) { resolve('p2'); });
32var p3 = new Promise(function(resolve) { resolve('p3'); });
33var p4 = new Promise(function() {});
34var p5 = new Promise(function() {});
35var p6 = new Promise(function(_, reject) { reject('p6'); });
36var p7 = new Promise(function(_, reject) { reject('p7'); });
37var p8 = new Promise(function(_, reject) { reject('p8'); });
38var p9 = new Promise(function(resolve) { resolve(p2); });
39
40Promise.all([p1, p2, p5]).then(function(result) {
41  testFailed('Promise.all([p1, p2, p5]) is fulfilled.');
42}, function() {
43  testFailed('Promise.all([p1, p2, p5]) is rejected.');
44});
45
46Promise.all().then(function() {
47  testFailed('Promise.all() is fulfilled.');
48}, function() {
49  testPassed('Promise.all() is rejected.');
50  return Promise.all([]).then(function(localResult) {
51    testPassed('Promise.all([]) is fulfilled.');
52    result = localResult;
53    shouldBe('result.length', '0');
54  }, function() {
55    testFailed('Promise.all([]) is rejected.');
56  });
57}).then(function() {
58  return Promise.all([p1, p2, p3]).then(function(localResult) {
59    testPassed('Promise.all([p1, p2, p3]) is fulfilled.');
60    result = localResult;
61    shouldBe('result.length', '3');
62    shouldBeEqualToString('result[0]', 'p1');
63    shouldBeEqualToString('result[1]', 'p2');
64    shouldBeEqualToString('result[2]', 'p3');
65  }, function() {
66    testFailed('Promise.all([p1, p2, p3]) is rejected.');
67  });
68}).then(function() {
69  return Promise.all([p1, p6, p5]).then(function(localResult) {
70    testFailed('Promise.all([p1, p6, p5]) is fulfilled.');
71  }, function(localResult) {
72    testPassed('Promise.all([p1, p6, p5]) is rejected.');
73    result = localResult;
74    shouldBeEqualToString('result', 'p6');
75  });
76}).then(function() {
77  return Promise.all([p9]).then(function(localResult) {
78    testPassed('Promise.all([p9]) is fulfilled.');
79    result = localResult;
80    shouldBe('result.length', '1');
81    shouldBeEqualToString('result[0]', 'p2');
82  }, function(result) {
83    testFailed('Promise.all([p9]) is rejected.');
84  });
85}).then(function() {
86  // Array hole should not be skipped.
87  return Promise.all([p9,,,]).then(function(localResult) {
88    testPassed('Promise.all([p9,,,]) is fulfilled.');
89    result = localResult;
90    shouldBe('result.length', '3');
91    shouldBeEqualToString('result[0]', 'p2');
92    shouldBe('result[1]', 'undefined');
93    shouldBe('result[2]', 'undefined');
94  }, function(localResult) {
95    testFailed('Promise.all([p9,,,]) is rejected.');
96  });
97}).then(function() {
98  // Immediate value should be converted to a promise object by the
99  // ToPromise operation.
100  return Promise.all([p9,42]).then(function(localResult) {
101    testPassed('Promise.all([p9,42]) is fulfilled.');
102    result = localResult;
103    shouldBe('result.length', '2');
104    shouldBeEqualToString('result[0]', 'p2');
105    shouldBe('result[1]', '42');
106  }, function(localResult) {
107    testFailed('Promise.all([p9,42]) is rejected.');
108  });
109}).then(function() {
110  return Promise.all({}).then(function(localResult) {
111    testFailed('Promise.all({}) is fulfilled.');
112  }, function(localResult) {
113    testPassed('Promise.all({}) is rejected.');
114  });
115}).then(finishJSTest, finishJSTest);
116
117shouldBe('result', 'undefined');
118