OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-arrays --harmony-generators | 5 // Flags: --harmony-arrays |
6 (function() { | 6 (function() { |
7 | 7 |
8 assertEquals(1, Array.from.length); | 8 assertEquals(1, Array.from.length); |
9 | 9 |
10 function assertArrayLikeEquals(value, expected, type) { | 10 function assertArrayLikeEquals(value, expected, type) { |
11 assertInstanceof(value, type); | 11 assertInstanceof(value, type); |
12 assertEquals(expected.length, value.length); | 12 assertEquals(expected.length, value.length); |
13 for (var i=0; i<value.length; ++i) { | 13 for (var i=0; i<value.length; ++i) { |
14 assertEquals(expected[i], value[i]); | 14 assertEquals(expected[i], value[i]); |
15 } | 15 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 | 84 |
85 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | 85 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { |
86 return this.filter(x); | 86 return this.filter(x); |
87 }, { | 87 }, { |
88 filter: function(x) { return x.toUpperCase(); } | 88 filter: function(x) { return x.toUpperCase(); } |
89 }), ['T', 'E', 'S', 'T'], constructor); | 89 }), ['T', 'E', 'S', 'T'], constructor); |
90 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | 90 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { |
91 return x.toUpperCase(); | 91 return x.toUpperCase(); |
92 }), ['T', 'E', 'S', 'T'], constructor); | 92 }), ['T', 'E', 'S', 'T'], constructor); |
93 | 93 |
94 this.thisArg = thisArg; | 94 assertThrows(function() { Array.from.call(thisArg, null); }, TypeError); |
95 assertThrows('Array.from.call(thisArg, null)', TypeError); | 95 assertThrows(function() { Array.from.call(thisArg, undefined); }, TypeError); |
96 assertThrows('Array.from.call(thisArg, undefined)', TypeError); | 96 assertThrows(function() { Array.from.call(thisArg, [], null); }, TypeError); |
97 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | 97 assertThrows(function() { Array.from.call(thisArg, [], "noncallable"); }, |
98 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError); | 98 TypeError); |
99 | 99 |
100 this.nullIterator = {}; | 100 var nullIterator = {}; |
101 nullIterator[Symbol.iterator] = null; | 101 nullIterator[Symbol.iterator] = null; |
102 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError); | 102 assertArrayLikeEquals(Array.from.call(thisArg, nullIterator), [], |
103 constructor); | |
103 | 104 |
104 this.nonObjIterator = {}; | 105 var nonObjIterator = {}; |
105 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; | 106 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; |
106 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError); | 107 assertThrows(function() { Array.from.call(thisArg, nonObjIterator); }, |
108 TypeError); | |
107 | 109 |
108 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | 110 assertThrows(function() { Array.from.call(thisArg, [], null); }, TypeError); |
111 | |
112 // Ensure iterator is only accessed once, and only invoked once | |
113 var called = false; | |
114 var arr = [1,2,3]; | |
arv (Not doing code reviews)
2015/01/22 21:14:58
ws after ,
| |
115 var obj = {}; | |
116 | |
117 // Test order --- only get iterator method once | |
118 function testIterator() { | |
119 if (!called) { | |
arv (Not doing code reviews)
2015/01/22 21:14:58
Maybe use assertFalse instead?
| |
120 called = true; | |
121 assertEquals(obj, this); | |
122 return arr[Symbol.iterator](); | |
123 } | |
124 assertUnreachable("@@iterator should be called only once"); | |
125 } | |
126 var getCalled = false; | |
127 Object.defineProperty(obj, Symbol.iterator, { | |
128 get: function() { | |
129 if (!getCalled) { | |
130 getCalled = true; | |
131 return testIterator; | |
132 } | |
133 assertUnreachable("@@iterator should be accessed only once"); | |
134 }, | |
135 set: function() { | |
136 assertUnreachable("@@iterator should not be set"); | |
137 } | |
138 }); | |
139 assertArrayLikeEquals(Array.from.call(thisArg, obj), [1,2,3], constructor); | |
arv (Not doing code reviews)
2015/01/22 21:14:58
ws
| |
109 } | 140 } |
110 | 141 |
111 function Other() {} | 142 function Other() {} |
112 | 143 |
113 var boundFn = (function() {}).bind(Array, 27); | 144 var boundFn = (function() {}).bind(Array, 27); |
114 | 145 |
115 testArrayFrom(Array, Array); | 146 testArrayFrom(Array, Array); |
116 testArrayFrom(null, Array); | 147 testArrayFrom(null, Array); |
117 testArrayFrom({}, Array); | 148 testArrayFrom({}, Array); |
118 testArrayFrom(Object, Object); | 149 testArrayFrom(Object, Object); |
119 testArrayFrom(Other, Other); | 150 testArrayFrom(Other, Other); |
120 testArrayFrom(Math.cos, Array); | 151 testArrayFrom(Math.cos, Array); |
121 testArrayFrom(Math.cos.bind(Math), Array); | 152 testArrayFrom(Math.cos.bind(Math), Array); |
122 testArrayFrom(boundFn, boundFn); | 153 testArrayFrom(boundFn, boundFn); |
123 | 154 |
124 })(); | 155 })(); |
OLD | NEW |