OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-iteration --harmony-scoping | 28 // Flags: --harmony-iteration --harmony-scoping --harmony-generators |
29 | 29 |
30 // Test for-of syntax. | 30 // Test for-of syntax. |
31 | 31 |
32 "use strict"; | 32 "use strict"; |
33 | 33 |
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
| |
34 function f() { for (x of y) { } } | 34 function* values() { |
35 function f() { for (var x of y) { } } | 35 for (var i = 0; i < arguments.length; i++) { |
36 function f() { for (let x of y) { } } | 36 yield arguments[i]; |
37 } | |
38 } | |
37 | 39 |
38 assertThrows("function f() { for (x of) { } }", SyntaxError); | 40 function* integers_from(n) { |
39 assertThrows("function f() { for (x of y z) { } }", SyntaxError); | 41 while (1) yield n++; |
40 assertThrows("function f() { for (x of y;) { } }", SyntaxError); | 42 } |
41 | 43 |
42 assertThrows("function f() { for (var x of) { } }", SyntaxError); | 44 // A destructive append. |
43 assertThrows("function f() { for (var x of y z) { } }", SyntaxError); | 45 function append(x, tail) { |
44 assertThrows("function f() { for (var x of y;) { } }", SyntaxError); | 46 tail[tail.length] = x; |
47 return tail; | |
48 } | |
45 | 49 |
46 assertThrows("function f() { for (let x of) { } }", SyntaxError); | 50 function sum(x, tail) { |
47 assertThrows("function f() { for (let x of y z) { } }", SyntaxError); | 51 return x + tail; |
48 assertThrows("function f() { for (let x of y;) { } }", SyntaxError); | 52 } |
49 | 53 |
50 // Alack, this appears to be valid. | 54 // Normal termination. |
51 function f() { for (of of y) { } } | 55 function fold(cons, seed, iter) { |
52 function f() { for (let of of y) { } } | 56 for (var x of iter) { |
53 function f() { for (var of of y) { } } | 57 seed = cons(x, seed); |
58 } | |
59 return seed; | |
60 } | |
54 | 61 |
55 // This too, of course. | 62 // Tests "break". |
56 function f() { for (of in y) { } } | 63 function* take(iter, n) { |
57 function f() { for (var of in y) { } } | 64 if (n == 0) return; |
58 function f() { for (let of in y) { } } | 65 for (let x of iter) { |
66 yield x; | |
67 if (--n == 0) break; | |
68 } | |
69 } | |
70 | |
71 // Tests "continue". | |
72 function* skip_every(iter, n) { | |
73 var i = 0; | |
74 for (let x of iter) { | |
75 if (++i % n == 0) continue; | |
76 yield x; | |
77 } | |
78 } | |
79 | |
80 assertEquals([1, 2, 3], fold(append, [], values(1, 2, 3))); | |
81 assertEquals(45, fold(sum, 0, take(integers_from(0), 10))); | |
82 assertEquals(90, fold(sum, 0, take(skip_every(integers_from(0), 2), 10))); | |
83 | |
84 function* unreachable(iter) { | |
85 for (let x of iter) { | |
86 throw "not reached"; | |
87 } | |
88 } | |
89 | |
90 assertEquals(0, fold(sum, 0, unreachable(null))); | |
rossberg
2013/06/06 11:35:33
Sigh...
| |
91 assertEquals(0, fold(sum, 0, unreachable(undefined))); | |
92 | |
93 assertThrows('fold(sum, 0, unreachable({}))', TypeError); | |
94 assertThrows('fold(sum, 0, unreachable("foo"))', TypeError); | |
95 assertThrows('fold(sum, 0, unreachable(37))', TypeError); | |
OLD | NEW |