Debian Codegraph @

js/src/tests/test262/built-ins/Iterator/from/iterable-to-iterator-fallback.js

raw
1 // |reftest| shell-option(--enable-iterator-helpers) skip-if(!this.hasOwnProperty('Iterator')||!xulRuntime.shell) -- iterator-helpers is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2023 Michael Ficarra. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4 /*---
5 esid: sec-iterator.from
6 description: >
7 Iterator.from falls back to treating its parameter as an iterator if the Symbol.iterator property is null/undefined
8 info: |
9 Iterator.from ( O )
10
11 includes: [compareArray.js]
12 features: [iterator-helpers]
13 flags: []
14 ---*/
15
16 function* g() {
17 yield 0;
18 yield 1;
19 yield 2;
20 }
21
22 let iter = (function () {
23 let n = g();
24 return {
25 [Symbol.iterator]: 0,
26 next: () => n.next(),
27 };
28 })();
29
30 assert.throws(TypeError, function () {
31 Iterator.from(iter);
32 });
33
34 iter = (function () {
35 let n = g();
36 return {
37 [Symbol.iterator]: null,
38 next: () => n.next(),
39 };
40 })();
41
42 assert.compareArray(Array.from(Iterator.from(iter)), [0, 1, 2]);
43
44 iter = (function () {
45 let n = g();
46 return {
47 [Symbol.iterator]: undefined,
48 next: () => n.next(),
49 };
50 })();
51
52 assert.compareArray(Array.from(Iterator.from(iter)), [0, 1, 2]);
53
54 reportCompare(0, 0);