Index: test/mjsunit/harmony/iteration-semantics.js |
diff --git a/test/mjsunit/harmony/iteration-syntax.js b/test/mjsunit/harmony/iteration-semantics.js |
similarity index 55% |
copy from test/mjsunit/harmony/iteration-syntax.js |
copy to test/mjsunit/harmony/iteration-semantics.js |
index 64356478d13012e25cd833a5af498f40d2152676..e9a4ba6a39e4de7d2dbfbaa2a9ce9a0ed045e8be 100644 |
--- a/test/mjsunit/harmony/iteration-syntax.js |
+++ b/test/mjsunit/harmony/iteration-semantics.js |
@@ -25,34 +25,71 @@ |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
-// Flags: --harmony-iteration --harmony-scoping |
+// Flags: --harmony-iteration --harmony-scoping --harmony-generators |
// Test for-of syntax. |
"use strict"; |
Michael Starzinger
2013/06/06 11:09:09
Can we increase the test coverage for iterations.
rossberg
2013/06/06 11:35:33
Also:
- interaction with exceptions
- ...and othe
wingo
2013/06/06 17:10:06
Done.
wingo
2013/06/06 17:10:06
Not sure what you mean here. Errors do seem to be
rossberg
2013/06/06 18:14:10
Yes, some combinations of cases where an exception
|
-function f() { for (x of y) { } } |
-function f() { for (var x of y) { } } |
-function f() { for (let x of y) { } } |
+function* values() { |
+ for (var i = 0; i < arguments.length; i++) { |
+ yield arguments[i]; |
+ } |
+} |
-assertThrows("function f() { for (x of) { } }", SyntaxError); |
-assertThrows("function f() { for (x of y z) { } }", SyntaxError); |
-assertThrows("function f() { for (x of y;) { } }", SyntaxError); |
+function* integers_from(n) { |
+ while (1) yield n++; |
+} |
-assertThrows("function f() { for (var x of) { } }", SyntaxError); |
-assertThrows("function f() { for (var x of y z) { } }", SyntaxError); |
-assertThrows("function f() { for (var x of y;) { } }", SyntaxError); |
+// A destructive append. |
+function append(x, tail) { |
+ tail[tail.length] = x; |
+ return tail; |
+} |
-assertThrows("function f() { for (let x of) { } }", SyntaxError); |
-assertThrows("function f() { for (let x of y z) { } }", SyntaxError); |
-assertThrows("function f() { for (let x of y;) { } }", SyntaxError); |
+function sum(x, tail) { |
+ return x + tail; |
+} |
-// Alack, this appears to be valid. |
-function f() { for (of of y) { } } |
-function f() { for (let of of y) { } } |
-function f() { for (var of of y) { } } |
+// Normal termination. |
+function fold(cons, seed, iter) { |
+ for (var x of iter) { |
+ seed = cons(x, seed); |
+ } |
+ return seed; |
+} |
-// This too, of course. |
-function f() { for (of in y) { } } |
-function f() { for (var of in y) { } } |
-function f() { for (let of in y) { } } |
+// Tests "break". |
+function* take(iter, n) { |
+ if (n == 0) return; |
+ for (let x of iter) { |
+ yield x; |
+ if (--n == 0) break; |
+ } |
+} |
+ |
+// Tests "continue". |
+function* skip_every(iter, n) { |
+ var i = 0; |
+ for (let x of iter) { |
+ if (++i % n == 0) continue; |
+ yield x; |
+ } |
+} |
+ |
+assertEquals([1, 2, 3], fold(append, [], values(1, 2, 3))); |
+assertEquals(45, fold(sum, 0, take(integers_from(0), 10))); |
+assertEquals(90, fold(sum, 0, take(skip_every(integers_from(0), 2), 10))); |
+ |
+function* unreachable(iter) { |
+ for (let x of iter) { |
+ throw "not reached"; |
+ } |
+} |
+ |
+assertEquals(0, fold(sum, 0, unreachable(null))); |
rossberg
2013/06/06 11:35:33
Sigh...
|
+assertEquals(0, fold(sum, 0, unreachable(undefined))); |
+ |
+assertThrows('fold(sum, 0, unreachable({}))', TypeError); |
+assertThrows('fold(sum, 0, unreachable("foo"))', TypeError); |
+assertThrows('fold(sum, 0, unreachable(37))', TypeError); |