Skip to content
Snippets Groups Projects
Commit 42dc0f6b authored by Brian Terlson's avatar Brian Terlson
Browse files

Merge pull request #165 from bocoup/iterators

Add Array Iteration Tests
parents 2eca2c71 6565c89d
No related branches found
No related tags found
No related merge requests found
Showing
with 420 additions and 0 deletions
// 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);
// 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);
// 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]);
// 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));
// 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)');
// 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');
// 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();
});
// 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');
// 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)');
// 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`');
// 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');
// 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);
// 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)');
// 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');
// 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');
// 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);
// 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');
// 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');
// 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');
// 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');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment