Debian Codegraph @

js/src/tests/test262/language/statements/for-of/iterator-close-via-break.js

raw
1 // Copyright (C) 2015 the V8 project authors. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3 /*---
4 es6id: 13.6.4.13
5 description: >
6 Iterators should be closed via their `return` method when iteration is
7 interrupted via a `break` statement.
8 features: [Symbol.iterator]
9 ---*/
10
11 var startedCount = 0;
12 var returnCount = 0;
13 var iterationCount = 0;
14 var iterable = {};
15
16 iterable[Symbol.iterator] = function() {
17 return {
18 next: function() {
19 startedCount += 1;
20 return { done: false, value: null };
21 },
22 return: function() {
23 returnCount += 1;
24 return {};
25 }
26 };
27 };
28
29 for (var x of iterable) {
30 assert.sameValue(
31 startedCount, 1, 'Value is retrieved'
32 );
33 assert.sameValue(
34 returnCount, 0, 'Iterator is not closed'
35 );
36 iterationCount += 1;
37 break;
38 }
39
40 assert.sameValue(
41 startedCount, 1, 'Iterator does not restart following interruption'
42 );
43 assert.sameValue(iterationCount, 1, 'A single iteration occurs');
44 assert.sameValue(
45 returnCount, 1, 'Iterator is closed after `break` statement'
46 );
47
48 reportCompare(0, 0);