Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Unified Diff: test/mjsunit/harmony/iterator-close.js

Issue 1695393003: [es6] Implement for-of iterator finalization (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove some debug left-overs Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/harmony/iterator-close.js
diff --git a/test/mjsunit/harmony/iterator-close.js b/test/mjsunit/harmony/iterator-close.js
new file mode 100644
index 0000000000000000000000000000000000000000..21d7347a1bafb26fba1cfbaee950338b5fac1ecd
--- /dev/null
+++ b/test/mjsunit/harmony/iterator-close.js
@@ -0,0 +1,226 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+
+function* g() { yield 42; return 88 };
+
+
+// Return method is "undefined".
+{
+ g.prototype.return = null;
+
+ assertEquals(undefined, (() => {
+ for (let x of g()) { break; }
+ })());
+
+ assertEquals(undefined, (() => {
+ for (x of g()) { break; }
+ })());
+
+ assertThrowsEquals(() => {
+ for (let x of g()) { throw 42; }
+ }, 42);
+
+ assertThrowsEquals(() => {
+ for (x of g()) { throw 42; }
+ }, 42);
+
+ assertEquals(42, (() => {
+ for (let x of g()) { return 42; }
+ })());
+
+ assertEquals(42, (() => {
+ for (x of g()) { return 42; }
+ })());
+
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+}
+
+
+// Return method is not callable.
+{
+ g.prototype.return = 666;
+
+ assertThrows(() => {
+ for (let x of g()) { break; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (x of g()) { break; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (let x of g()) { throw 666; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (x of g()) { throw 666; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (let x of g()) { return 666; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (x of g()) { return 666; }
+ }, TypeError);
+
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+}
+
+
+// Return method does not return an object.
+{
+ g.prototype.return = () => 666;
+
+ assertThrows(() => {
+ for (let x of g()) { break; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (x of g()) { break; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (let x of g()) { throw 666; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (x of g()) { throw 666; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (let x of g()) { return 666; }
+ }, TypeError);
+
+ assertThrows(() => {
+ for (x of g()) { return 666; }
+ }, TypeError);
+
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+
+ assertEquals(42, eval('for (x of g()) { x; }'));
+}
+
+
+// Return method returns an object.
+{
+ let log = [];
+ g.prototype.return = (...args) => { log.push(args); return {} };
+
+ log.length = 0;
+ for (let x of g()) { break; }
+ assertEquals([[]], log);
+
+ log.length = 0;
+ for (x of g()) { break; }
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (let x of g()) { throw 42; }
+ }, 42);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (x of g()) { throw 42; }
+ }, 42);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertEquals(42, (() => {
+ for (let x of g()) { return 42; }
+ })());
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertEquals(42, (() => {
+ for (x of g()) { return 42; }
+ })());
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+ assertEquals([], log);
+
+ log.length = 0;
+ assertEquals(42, eval('for (x of g()) { x; }'));
+ assertEquals([], log);
+}
+
+
+// Return method throws.
+{
+ let log = [];
+ g.prototype.return = (...args) => { log.push(args); throw 23 };
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (let x of g()) { break; }
+ }, 23);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (x of g()) { break; }
+ }, 23);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (let x of g()) { throw 42; }
+ }, 42);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (x of g()) { throw 42; }
+ }, 42);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (let x of g()) { return 42; }
+ }, 23);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (x of g()) { return 42; }
+ }, 23);
+ assertEquals([[]], log);
+
+ log.length = 0;
+ assertEquals(42, eval('for (let x of g()) { x; }'));
+ assertEquals([], log);
+
+ log.length = 0;
+ assertEquals(42, eval('for (x of g()) { x; }'));
+ assertEquals([], log);
+}
+
+
+// Next method throws.
+{
+ let log = [];
+ g.prototype.next = (...args) => { log.push(1, args); throw 666; };
+ g.prototype.return = (...args) => { log.push(2, args); return {} };
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (let x of g()) {}
+ }, 666);
+ assertEquals([1, [], 2, []], log);
+
+ log.length = 0;
+ assertThrowsEquals(() => {
+ for (x of g()) {}
+ }, 666);
+ assertEquals([1, [], 2, []], log);
+}
Dan Ehrenberg 2016/02/16 21:18:30 Can you write some tests to ensure that at least s
rossberg 2016/02/17 12:54:45 Done.

Powered by Google App Engine
This is Rietveld 408576698