mirror of https://github.com/tc39/test262.git
Merge pull request #165 from bocoup/iterators
Add Array Iteration Tests
This commit is contained in:
commit
42dc0f6b35
|
@ -0,0 +1,14 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should exist on the Array prototype, and it should be writable
|
||||
and configurable, but not enumerable.
|
||||
includes: [propertyHelper.js]
|
||||
es6id: 22.1.3.4
|
||||
---*/
|
||||
|
||||
verifyNotEnumerable(Array.prototype, Symbol.iterator);
|
||||
verifyWritable(Array.prototype, Symbol.iterator);
|
||||
verifyConfigurable(Array.prototype, Symbol.iterator);
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
`Object.prototype.getOwnPropertyDescriptor` should reflect the value and
|
||||
writability of the @@toStringTag attribute.
|
||||
includes: [propertyHelper.js]
|
||||
es6id: 22.1.5.2.2
|
||||
---*/
|
||||
|
||||
var ArrayIteratorProto = Object.getPrototypeOf([][Symbol.iterator]());
|
||||
|
||||
assert.sameValue("Array Iterator", ArrayIteratorProto[Symbol.toStringTag]);
|
||||
|
||||
verifyNotEnumerable(ArrayIteratorProto, Symbol.toStringTag);
|
||||
verifyNotWritable(ArrayIteratorProto, Symbol.toStringTag);
|
||||
verifyConfigurable(ArrayIteratorProto, Symbol.toStringTag);
|
12
test/built-ins/Array/prototype/Symbol.iterator/prototype/Symbol.toStringTag/value-direct.js
vendored
Normal file
12
test/built-ins/Array/prototype/Symbol.iterator/prototype/Symbol.toStringTag/value-direct.js
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The @@toStringTag attribute should be defined directly on the prototype.
|
||||
es6id: 22.1.5.2.2
|
||||
---*/
|
||||
|
||||
var ArrayIteratorProto = Object.getPrototypeOf([][Symbol.iterator]());
|
||||
|
||||
assert.sameValue("Array Iterator", ArrayIteratorProto[Symbol.toStringTag]);
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
`Object.prototype.toString` should honor the value of the @@toStringTag
|
||||
attribute.
|
||||
es6id: 22.1.5.2.2
|
||||
---*/
|
||||
|
||||
var iter = [][Symbol.iterator]();
|
||||
|
||||
assert.sameValue("[object Array Iterator]", Object.prototype.toString.call(iter));
|
32
test/built-ins/Array/prototype/Symbol.iterator/prototype/next/iteration-mutable.js
vendored
Normal file
32
test/built-ins/Array/prototype/Symbol.iterator/prototype/next/iteration-mutable.js
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
When an item is added to the array after the iterator is created but
|
||||
before the iterator is "done" (as defined by 22.1.5.2.1), the new item
|
||||
should be accessible via iteration. When an item is added to the array
|
||||
after the iterator is is "done", the new item should not be accessible
|
||||
via iteration.
|
||||
es6id: 22.1.3.30
|
||||
---*/
|
||||
|
||||
var array = [];
|
||||
var iterator = array[Symbol.iterator]();
|
||||
var result;
|
||||
|
||||
array.push('a');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, false, 'First result `done` flag');
|
||||
assert.sameValue(result.value, 'a', 'First result `value`');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value`');
|
||||
|
||||
array.push('b');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag (after push)');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value (after push)');
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator with the context as the
|
||||
IteratedObject.
|
||||
es6id: 22.1.3.30
|
||||
---*/
|
||||
|
||||
var array = ['a', 'b', 'c'];
|
||||
var iterator = array[Symbol.iterator]();
|
||||
var result;
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, 'a', 'First result `value`');
|
||||
assert.sameValue(result.done, false, 'First result `done` flag');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, 'b', 'Second result `value`');
|
||||
assert.sameValue(result.done, false, 'Second result `done` flag');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, 'c', 'Third result `value`');
|
||||
assert.sameValue(result.done, false, 'Third result `done` flag`');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value`');
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag');
|
17
test/built-ins/Array/prototype/Symbol.iterator/prototype/next/non-own-slots.js
vendored
Normal file
17
test/built-ins/Array/prototype/Symbol.iterator/prototype/next/non-own-slots.js
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
If the `this` value does not have all of the internal slots of an Array
|
||||
Iterator Instance (22.1.5.3), throw a TypeError exception.
|
||||
es6id: 22.1.5.2.1
|
||||
---*/
|
||||
|
||||
var array = [0];
|
||||
var iterator = array[Symbol.iterator]();
|
||||
var object = Object.create(iterator);
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
object.next();
|
||||
});
|
16
test/built-ins/Array/prototype/Symbol.iterator/prototype/next/property-descriptor.js
vendored
Normal file
16
test/built-ins/Array/prototype/Symbol.iterator/prototype/next/property-descriptor.js
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should exist on the ArrayIterator prototype, and it should be
|
||||
writable and configurable, but not enumerable.
|
||||
includes: [propertyHelper.js]
|
||||
es6id: 17
|
||||
---*/
|
||||
|
||||
var ArrayIteratorProto = Object.getPrototypeOf([][Symbol.iterator]());
|
||||
|
||||
verifyNotEnumerable(ArrayIteratorProto, 'next');
|
||||
verifyWritable(ArrayIteratorProto, 'next');
|
||||
verifyConfigurable(ArrayIteratorProto, 'next');
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator with the context as the
|
||||
IteratedObject. When an item is added to the array after the iterator is
|
||||
created but before the iterator is "done" (as defined by 22.1.5.2.1) the
|
||||
new item should be accessible via iteration.
|
||||
es6id: 22.1.3.4
|
||||
---*/
|
||||
|
||||
var array = [];
|
||||
var iterator = array.entries();
|
||||
var result;
|
||||
|
||||
array.push('a');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, false, 'First result `done` flag');
|
||||
assert.sameValue(result.value[0], 0, 'First result `value` (array key)');
|
||||
assert.sameValue(result.value[1], 'a', 'First result `value (array value)');
|
||||
assert.sameValue(result.value.length, 2, 'First result `value` (length)');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value`');
|
||||
|
||||
array.push('b');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag (after push)');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value` (after push)');
|
|
@ -0,0 +1,38 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator with the context as the
|
||||
IteratedObject.
|
||||
es6id: 22.1.3.4
|
||||
---*/
|
||||
|
||||
var ArrayIteratorPrototype = Object.getPrototypeOf(Array.prototype[Symbol.iterator]());
|
||||
var array = ['a', 'b', 'c'];
|
||||
var iterator = array.entries();
|
||||
var result;
|
||||
|
||||
assert.sameValue(ArrayIteratorPrototype, Object.getPrototypeOf(array.entries()));
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, false, 'First result `done` flag');
|
||||
assert.sameValue(result.value[0], 0, 'First result `value` (array key)');
|
||||
assert.sameValue(result.value[1], 'a', 'First result `value` (array value)');
|
||||
assert.sameValue(result.value.length, 2, 'First result `value` (length)');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, false, 'Second result `done` flag');
|
||||
assert.sameValue(result.value[0], 1, 'Second result `value` (array key)');
|
||||
assert.sameValue(result.value[1], 'b', 'Second result `value` (array value)');
|
||||
assert.sameValue(result.value.length, 2, 'Second result `value` (length)');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, false, 'Third result `done` flag');
|
||||
assert.sameValue(result.value[0], 2, 'Third result `value` (array key)');
|
||||
assert.sameValue(result.value[1], 'c', 'Third result `value` (array value)');
|
||||
assert.sameValue(result.value.length, 2, 'Third result `value` (length)');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value`');
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should exist on the Array prototype, and it should be writable
|
||||
and configurable, but not enumerable.
|
||||
includes: [propertyHelper.js]
|
||||
es6id: 17
|
||||
---*/
|
||||
|
||||
verifyNotEnumerable(Array.prototype, 'entries');
|
||||
verifyWritable(Array.prototype, 'entries');
|
||||
verifyConfigurable(Array.prototype, 'entries');
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return an Iterator instance.
|
||||
es6id: 22.1.3.4
|
||||
---*/
|
||||
|
||||
var ArrayIteratorProto = Object.getPrototypeOf([][Symbol.iterator]());
|
||||
var iter = [].entries();
|
||||
|
||||
assert.sameValue(Object.getPrototypeOf(iter), ArrayIteratorProto);
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
When an item is added to the array after the iterator is created but
|
||||
before the iterator is "done" (as defined by 22.1.5.2.1), the new item's
|
||||
key should be accessible via iteration. When an item is added to the
|
||||
array after the iterator is is "done", the new item's key should not be
|
||||
accessible via iteration.
|
||||
es6id: 22.1.3.13
|
||||
---*/
|
||||
|
||||
var array = [];
|
||||
var iterator = array.keys();
|
||||
var result;
|
||||
|
||||
array.push('a');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, false , 'First result `done` flag');
|
||||
assert.sameValue(result.value, 0, 'First result `value`');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value`');
|
||||
|
||||
array.push('b');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag (after push)');
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value` (after push)');
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator with the array's numeric
|
||||
properties as the IteratedObject.
|
||||
es6id: 22.1.3.13
|
||||
---*/
|
||||
|
||||
var array = ['a', 'b', 'c'];
|
||||
var iterator = array.keys();
|
||||
var result;
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, 0, 'First result `value`');
|
||||
assert.sameValue(result.done, false, 'First result `done` flag');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, 1, 'Second result `value`');
|
||||
assert.sameValue(result.done, false, 'Second result `done` flag');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, 2, 'Third result `value`');
|
||||
assert.sameValue(result.done, false, 'Third result `done` flag');
|
||||
|
||||
result = iterator.next();
|
||||
assert.sameValue(result.value, undefined, 'Exhausted result `value`');
|
||||
assert.sameValue(result.done, true, 'Exhausted result `done` flag');
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should exist on the Array prototype, and it should be writable
|
||||
and configurable, but not enumerable.
|
||||
includes: [propertyHelper.js]
|
||||
es6id: 22.1.3.13
|
||||
---*/
|
||||
|
||||
verifyNotEnumerable(Array.prototype, 'keys');
|
||||
verifyWritable(Array.prototype, 'keys');
|
||||
verifyConfigurable(Array.prototype, 'keys');
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return an Iterator instance.
|
||||
es6id: 22.1.3.13
|
||||
---*/
|
||||
|
||||
var ArrayIteratorProto = Object.getPrototypeOf([][Symbol.iterator]());
|
||||
var iter = [].keys();
|
||||
|
||||
assert.sameValue(Object.getPrototypeOf(iter), ArrayIteratorProto);
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator that can be traversed using a
|
||||
`for...of` loop.
|
||||
es6id: 22.1.3.30
|
||||
---*/
|
||||
|
||||
var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
|
||||
var i = 0;
|
||||
|
||||
for (var value of array[Symbol.iterator]()) {
|
||||
assert.sameValue(value, array[i], 'element at index ' + i);
|
||||
i++;
|
||||
}
|
||||
|
||||
assert.sameValue(i, 8, 'Visits all elements');
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator that can be traversed using a
|
||||
`for...of` loop.
|
||||
es6id: 22.1.3.4
|
||||
---*/
|
||||
|
||||
var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
|
||||
var i = 0;
|
||||
|
||||
for (var value of array.entries()) {
|
||||
assert.sameValue(
|
||||
value[0], i, 'element at index ' + i + ': value (array key)'
|
||||
);
|
||||
assert.sameValue(
|
||||
value[1], array[i], 'element at index ' + i + ': value (array value)'
|
||||
);
|
||||
assert.sameValue(
|
||||
value.length, 2, 'element at index ' + i + ': value (array length)'
|
||||
);
|
||||
i++;
|
||||
}
|
||||
|
||||
assert.sameValue(i, 8, 'Visits all elements');
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
The method should return a valid iterator that can be traversed using a
|
||||
`for...of` loop.
|
||||
es6id: 22.1.3.13
|
||||
---*/
|
||||
|
||||
var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
|
||||
var i = 0;
|
||||
|
||||
for (var value of array.keys()) {
|
||||
assert.sameValue(value, i, 'element at index ' + i);
|
||||
i++;
|
||||
}
|
||||
|
||||
assert.sameValue(i, 8, 'Visits all elements');
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (C) 2013 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
|
||||
/*---
|
||||
description: >
|
||||
Array instances should be able to be traversed using a `for...of` loop.
|
||||
es6id: 13.6.4
|
||||
---*/
|
||||
|
||||
var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
|
||||
var i = 0;
|
||||
|
||||
for (var value of array) {
|
||||
assert.sameValue(value, array[i], 'element at index ' + i);
|
||||
i++;
|
||||
}
|
||||
|
||||
assert.sameValue(i, 8, 'Visits all elements');
|
Loading…
Reference in New Issue