-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathes.iterator.for-each.js
38 lines (30 loc) · 1.45 KB
/
es.iterator.for-each.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createIterator } from '../helpers/helpers.js';
import { STRICT, STRICT_THIS } from '../helpers/constants.js';
QUnit.test('Iterator#forEach', assert => {
const { forEach } = Iterator.prototype;
assert.isFunction(forEach);
assert.arity(forEach, 1);
assert.name(forEach, 'forEach');
assert.looksNative(forEach);
assert.nonEnumerable(Iterator.prototype, 'forEach');
const array = [];
forEach.call(createIterator([1, 2, 3]), it => array.push(it));
assert.arrayEqual(array, [1, 2, 3], 'basic functionality');
forEach.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
if (STRICT) {
assert.throws(() => forEach.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => forEach.call(null, () => { /* empty */ }), TypeError);
}
assert.throws(() => forEach.call({}, () => { /* empty */ }), TypeError);
assert.throws(() => forEach.call([], () => { /* empty */ }), TypeError);
assert.throws(() => forEach.call(createIterator([1]), undefined), TypeError);
assert.throws(() => forEach.call(createIterator([1]), null), TypeError);
const it = createIterator([1], { return() { this.closed = true; } });
assert.throws(() => forEach.call(it, {}), TypeError);
assert.true(it.closed, "forEach doesn't close iterator on validation error");
});