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 |
(...skipping 13 matching lines...) Expand all Loading... |
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-generators --expose-gc | 28 // Flags: --harmony-generators --expose-gc |
29 | 29 |
30 // Test generator iteration. | 30 // Test generator iteration. |
31 | 31 |
32 var GeneratorFunction = (function*(){yield 1;}).__proto__.constructor; | 32 var GeneratorFunction = (function*(){yield 1;}).__proto__.constructor; |
33 | 33 |
| 34 function TestGeneratorResultPrototype() { |
| 35 function* g() { yield 1; } |
| 36 var iter = g(); |
| 37 var result = iter.next(); |
| 38 |
| 39 assertSame(Object.prototype, Object.getPrototypeOf(result)); |
| 40 property_names = Object.getOwnPropertyNames(result); |
| 41 property_names.sort(); |
| 42 assertEquals(["done", "value"], property_names); |
| 43 assertEquals({ value: 1, done: false }, result); |
| 44 } |
| 45 TestGeneratorResultPrototype() |
| 46 |
34 function TestGenerator(g, expected_values_for_next, | 47 function TestGenerator(g, expected_values_for_next, |
35 send_val, expected_values_for_send) { | 48 send_val, expected_values_for_send) { |
36 function testNext(thunk) { | 49 function testNext(thunk) { |
37 var iter = thunk(); | 50 var iter = thunk(); |
38 for (var i = 0; i < expected_values_for_next.length; i++) { | 51 for (var i = 0; i < expected_values_for_next.length; i++) { |
39 assertEquals(expected_values_for_next[i], iter.next()); | 52 assertEquals({ value: expected_values_for_next[i], |
| 53 done: i == expected_values_for_next.length - 1 }, |
| 54 iter.next()); |
40 } | 55 } |
41 assertThrows(function() { iter.next(); }, Error); | 56 assertThrows(function() { iter.next(); }, Error); |
42 } | 57 } |
43 function testSend(thunk) { | 58 function testSend(thunk) { |
44 var iter = thunk(); | 59 var iter = thunk(); |
45 for (var i = 0; i < expected_values_for_send.length; i++) { | 60 for (var i = 0; i < expected_values_for_send.length; i++) { |
46 assertEquals(expected_values_for_send[i], iter.send(send_val)); | 61 assertEquals({ value: expected_values_for_send[i], |
| 62 done: i == expected_values_for_send.length - 1 }, |
| 63 iter.send(send_val)); |
47 } | 64 } |
48 assertThrows(function() { iter.send(send_val); }, Error); | 65 assertThrows(function() { iter.send(send_val); }, Error); |
49 } | 66 } |
50 function testThrow(thunk) { | 67 function testThrow(thunk) { |
51 for (var i = 0; i < expected_values_for_next.length; i++) { | 68 for (var i = 0; i < expected_values_for_next.length; i++) { |
52 var iter = thunk(); | 69 var iter = thunk(); |
53 for (var j = 0; j < i; j++) { | 70 for (var j = 0; j < i; j++) { |
54 assertEquals(expected_values_for_next[j], iter.next()); | 71 assertEquals({ value: expected_values_for_next[j], |
| 72 done: j == expected_values_for_next.length - 1 }, |
| 73 iter.next()); |
55 } | 74 } |
56 function Sentinel() {} | 75 function Sentinel() {} |
57 assertThrows(function () { iter.throw(new Sentinel); }, Sentinel); | 76 assertThrows(function () { iter.throw(new Sentinel); }, Sentinel); |
58 assertThrows(function () { iter.next(); }, Error); | 77 assertThrows(function () { iter.next(); }, Error); |
59 } | 78 } |
60 } | 79 } |
61 | 80 |
62 testNext(g); | 81 testNext(g); |
63 testSend(g); | 82 testSend(g); |
64 testThrow(g); | 83 testThrow(g); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 function TestThrowRecursion() { | 303 function TestThrowRecursion() { |
285 function* g() { yield iter.throw(1); } | 304 function* g() { yield iter.throw(1); } |
286 var iter = g(); | 305 var iter = g(); |
287 return iter.next(); | 306 return iter.next(); |
288 } | 307 } |
289 assertThrows(TestNextRecursion, Error); | 308 assertThrows(TestNextRecursion, Error); |
290 assertThrows(TestSendRecursion, Error); | 309 assertThrows(TestSendRecursion, Error); |
291 assertThrows(TestThrowRecursion, Error); | 310 assertThrows(TestThrowRecursion, Error); |
292 } | 311 } |
293 TestRecursion(); | 312 TestRecursion(); |
OLD | NEW |