-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathes.iterator.constructor.js
49 lines (40 loc) · 1.79 KB
/
es.iterator.constructor.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
39
40
41
42
43
44
45
46
47
48
49
import { createIterator, nativeSubclass } from '../helpers/helpers.js';
const { getPrototypeOf } = Object;
QUnit.test('Iterator', assert => {
assert.isFunction(Iterator);
assert.arity(Iterator, 0);
assert.name(Iterator, 'Iterator');
assert.looksNative(Iterator);
const generator = (() => {
try {
return Function('return function*(){}()')();
} catch { /* empty */ }
})();
if (generator) {
const proto = getPrototypeOf(getPrototypeOf(getPrototypeOf(generator)));
if (proto !== Object.prototype && proto !== null) {
assert.true(generator instanceof Iterator, 'Generator');
}
}
assert.true(''[Symbol.iterator]() instanceof Iterator, 'String Iterator');
assert.true([].values() instanceof Iterator, 'Array Iterator');
assert.true(new Set().values() instanceof Iterator, 'Set Iterator');
assert.true('abc'.matchAll(/./g) instanceof Iterator, 'MatchAll Iterator');
assert.true(Iterator.from(createIterator([1, 2, 3])) instanceof Iterator, 'From Proxy');
assert.true([].values().drop(1) instanceof Iterator, 'Drop Proxy');
if (nativeSubclass) {
const Sub = nativeSubclass(Iterator);
assert.true(new Sub() instanceof Iterator, 'abstract constructor');
}
assert.throws(() => new Iterator(), 'direct constructor throws');
assert.throws(() => Iterator(), 'throws w/o `new`');
});
QUnit.test('Iterator#constructor', assert => {
assert.same(Iterator.prototype.constructor, Iterator, 'Iterator#constructor is Iterator');
});
QUnit.test('Iterator#@@toStringTag', assert => {
assert.same(Iterator.prototype[Symbol.toStringTag], 'Iterator', 'Iterator::@@toStringTag is `Iterator`');
assert.same(String(Iterator.from({
next: () => ({ done: Math.random() > 0.9, value: Math.random() * 10 | 0 }),
})), '[object Iterator]', 'correct stringification');
});