|
1 // This file was written by Andy Wingo <wingo@igalia.com> and originally |
|
2 // contributed to V8 as generators-objects.js, available here: |
|
3 // |
|
4 // http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-objects.js |
|
5 |
|
6 // Test aspects of the generator runtime. |
|
7 |
|
8 // Test the properties and prototype of a generator object. |
|
9 function TestGeneratorObject() { |
|
10 function* g() { yield 1; } |
|
11 |
|
12 var iter = g(); |
|
13 assertEq(Object.getPrototypeOf(iter), g.prototype); |
|
14 assertTrue(iter instanceof g); |
|
15 assertEq(String(iter), "[object Generator]"); |
|
16 assertDeepEq(Object.getOwnPropertyNames(iter), []); |
|
17 assertNotEq(g(), iter); |
|
18 |
|
19 // g() is the same as new g(). |
|
20 iter = new g(); |
|
21 assertEq(Object.getPrototypeOf(iter), g.prototype); |
|
22 assertTrue(iter instanceof g); |
|
23 assertEq(String(iter), "[object Generator]"); |
|
24 assertDeepEq(Object.getOwnPropertyNames(iter), []); |
|
25 assertNotEq(new g(), iter); |
|
26 } |
|
27 TestGeneratorObject(); |
|
28 |
|
29 |
|
30 // Test the methods of generator objects. |
|
31 function TestGeneratorObjectMethods() { |
|
32 function* g() { yield 1; } |
|
33 var iter = g(); |
|
34 |
|
35 function TestNonGenerator(non_generator) { |
|
36 assertThrowsInstanceOf(function() { iter.next.call(non_generator); }, TypeError); |
|
37 assertThrowsInstanceOf(function() { iter.next.call(non_generator, 1); }, TypeError); |
|
38 assertThrowsInstanceOf(function() { iter.throw.call(non_generator, 1); }, TypeError); |
|
39 assertThrowsInstanceOf(function() { iter.close.call(non_generator); }, TypeError); |
|
40 } |
|
41 |
|
42 TestNonGenerator(1); |
|
43 TestNonGenerator({}); |
|
44 TestNonGenerator(function(){}); |
|
45 TestNonGenerator(g); |
|
46 TestNonGenerator(g.prototype); |
|
47 } |
|
48 TestGeneratorObjectMethods(); |
|
49 |
|
50 |
|
51 if (typeof reportCompare == "function") |
|
52 reportCompare(true, true); |