Debian Codegraph @

js/src/tests/test262/built-ins/Iterator/from/get-next-method-only-once.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 Gets the next method from the underlying iterator only once
8 info: |
9 Iterator.from ( O )
10
11 2. Let iteratorRecord be ? GetIteratorFlattenable(O).
12
13 features: [iterator-helpers]
14 flags: []
15 ---*/
16 let nextGets = 0;
17 let nextCalls = 0;
18
19 class CountingIterator {
20 get next() {
21 ++nextGets;
22 let iter = (function* () {
23 for (let i = 1; i < 5; ++i) {
24 yield i;
25 }
26 })();
27 return function () {
28 ++nextCalls;
29 return iter.next();
30 };
31 }
32 }
33
34 let iterator = new CountingIterator();
35
36 assert.sameValue(nextGets, 0, 'The value of `nextGets` is 0');
37 assert.sameValue(nextCalls, 0, 'The value of `nextCalls` is 0');
38
39 iterator = Iterator.from(iterator);
40
41 assert.sameValue(nextGets, 1, 'The value of `nextGets` is 1');
42 assert.sameValue(nextCalls, 0, 'The value of `nextCalls` is 0');
43
44 iterator.toArray();
45
46 assert.sameValue(nextGets, 1, 'The value of `nextGets` is 1');
47 assert.sameValue(nextCalls, 5, 'The value of `nextCalls` is 5');
48
49 reportCompare(0, 0);