OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
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. | |
27 | |
28 // Flags: --harmony-generators | |
29 | |
30 // Test generator iteration. | |
31 | |
32 function* g1() { } | |
33 function* g2() { yield 1; } | |
34 function* g3() { yield 1; yield 2; } | |
35 function* g4() { yield 1; yield 2; return 3; } | |
36 function* g5() { return 1; } | |
37 function* g6() { var x = yield 1; return x; } | |
38 function* g7() { var x = yield 1; yield 2; return x; } | |
39 function* g8() { for (var x = 0; x < 4; x++) { yield x; } } | |
40 | |
41 function TestNext() { | |
42 function assertIteratorValues(g, expected_values) { | |
43 var iter = g(); | |
44 for (var i = 0; i < expected_values.length; i++) { | |
45 assertEquals(expected_values[i], iter.next()); | |
46 } | |
47 assertThrows(function() { iter.next(); }, Error); | |
48 } | |
49 assertIteratorValues(g1, [undefined]); | |
50 assertIteratorValues(g2, [1, undefined]); | |
51 assertIteratorValues(g3, [1, 2, undefined]); | |
52 assertIteratorValues(g4, [1, 2, 3]); | |
53 assertIteratorValues(g5, [1]); | |
54 assertIteratorValues(g6, [1, undefined]); | |
55 assertIteratorValues(g7, [1, 2, undefined]); | |
56 assertIteratorValues(g8, [0, 1, 2, 3, undefined]); | |
57 } | |
58 TestNext(); | |
59 | |
Michael Starzinger
2013/04/21 22:45:21
nit: Two newlines between the test cases makes it
| |
60 function TestSend() { | |
61 function assertIteratorValues(g, send_val, expected_values) { | |
62 var iter = g(); | |
63 for (var i = 0; i < expected_values.length; i++) { | |
64 assertEquals(expected_values[i], iter.send(send_val)); | |
65 } | |
66 assertThrows(function() { iter.send(send_val); }, Error); | |
67 } | |
68 assertIteratorValues(g1, "foo", [undefined]); | |
69 assertIteratorValues(g2, "foo", [1, undefined]); | |
70 assertIteratorValues(g3, "foo", [1, 2, undefined]); | |
71 assertIteratorValues(g4, "foo", [1, 2, 3]); | |
72 assertIteratorValues(g5, "foo", [1]); | |
73 assertIteratorValues(g6, "foo", [1, "foo"]); | |
74 assertIteratorValues(g7, "foo", [1, 2, "foo"]); | |
75 assertIteratorValues(g8, "foo", [0, 1, 2, 3, undefined]); | |
76 } | |
77 TestSend(); | |
78 | |
79 function TestThrow() { | |
80 function assertIteratorValues(g, expected_values) { | |
81 for (var i = 0; i < expected_values.length; i++) { | |
82 var iter = g(); | |
83 for (var j = 0; j < i; j++) { | |
84 assertEquals(expected_values[j], iter.next()); | |
85 } | |
86 function Sentinel() {} | |
87 assertThrows(function () { iter.throw(new Sentinel); }, Sentinel); | |
88 assertThrows(function () { iter.next(); }, Error); | |
89 } | |
90 } | |
91 assertIteratorValues(g1, [undefined]); | |
92 assertIteratorValues(g2, [1, undefined]); | |
93 assertIteratorValues(g3, [1, 2, undefined]); | |
94 assertIteratorValues(g4, [1, 2, 3]); | |
95 assertIteratorValues(g5, [1]); | |
96 assertIteratorValues(g6, [1, undefined]); | |
97 assertIteratorValues(g7, [1, 2, undefined]); | |
98 assertIteratorValues(g8, [0, 1, 2, 3, undefined]); | |
99 } | |
100 TestThrow(); | |
101 | |
102 function TestRecursion() { | |
103 function TestNextRecursion() { | |
104 function* g() { yield iter.next(); } | |
Michael Starzinger
2013/04/21 22:45:21
This throws only because "iter" is undefined, not
wingo
2013/04/23 13:51:04
Good catch; fixed this and the two following cases
| |
105 return g().next(); | |
106 } | |
107 function TestSendRecursion() { | |
108 function* g() { yield iter.send(42); } | |
Michael Starzinger
2013/04/21 22:45:21
Likewise.
| |
109 return g().next(); | |
110 } | |
111 function TestThrowRecursion() { | |
112 function* g() { yield iter.throw(1); } | |
Michael Starzinger
2013/04/21 22:45:21
Likewise.
| |
113 return g().next(); | |
114 } | |
115 assertThrows(TestNextRecursion, Error); | |
Michael Starzinger
2013/04/21 22:45:21
Add a third argument to these three assertThrows c
wingo
2013/04/23 13:51:04
The error that is thrown is Error, which does not
| |
116 assertThrows(TestSendRecursion, Error); | |
117 assertThrows(TestThrowRecursion, Error); | |
118 } | |
119 TestRecursion(); | |
Michael Starzinger
2013/04/21 22:45:21
The existing test cases look good. But can we add
wingo
2013/04/23 13:51:04
Good ideas all. The receiver tests that I added a
| |
OLD | NEW |