generator-close-via-break.js (1277B)
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 Generators should be closed via their `return` method when iteration is 7 interrupted via a `break` statement. 8 features: [generators] 9 ---*/ 10 11 var startedCount = 0; 12 var finallyCount = 0; 13 var iterationCount = 0; 14 function* values() { 15 startedCount += 1; 16 try { 17 yield; 18 throw new Test262Error('This code is unreachable (within `try` block)'); 19 } finally { 20 finallyCount += 1; 21 } 22 throw new Test262Error('This code is unreachable (following `try` statement)'); 23 } 24 var iterable = values(); 25 26 assert.sameValue( 27 startedCount, 0, 'Generator is initialized in suspended state' 28 ); 29 30 for (var x of iterable) { 31 assert.sameValue( 32 startedCount, 1, 'Generator executes prior to first iteration' 33 ); 34 assert.sameValue( 35 finallyCount, 0, 'Generator is paused during first iteration' 36 ); 37 iterationCount += 1; 38 break; 39 } 40 41 assert.sameValue( 42 startedCount, 1, 'Generator does not restart following interruption' 43 ); 44 assert.sameValue(iterationCount, 1, 'A single iteration occurs'); 45 assert.sameValue( 46 finallyCount, 1, 'Generator is closed after `break` statement' 47 ); 48 49 reportCompare(0, 0);