1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/Generators/objects.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,52 @@ 1.4 +// This file was written by Andy Wingo <wingo@igalia.com> and originally 1.5 +// contributed to V8 as generators-objects.js, available here: 1.6 +// 1.7 +// http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-objects.js 1.8 + 1.9 +// Test aspects of the generator runtime. 1.10 + 1.11 +// Test the properties and prototype of a generator object. 1.12 +function TestGeneratorObject() { 1.13 + function* g() { yield 1; } 1.14 + 1.15 + var iter = g(); 1.16 + assertEq(Object.getPrototypeOf(iter), g.prototype); 1.17 + assertTrue(iter instanceof g); 1.18 + assertEq(String(iter), "[object Generator]"); 1.19 + assertDeepEq(Object.getOwnPropertyNames(iter), []); 1.20 + assertNotEq(g(), iter); 1.21 + 1.22 + // g() is the same as new g(). 1.23 + iter = new g(); 1.24 + assertEq(Object.getPrototypeOf(iter), g.prototype); 1.25 + assertTrue(iter instanceof g); 1.26 + assertEq(String(iter), "[object Generator]"); 1.27 + assertDeepEq(Object.getOwnPropertyNames(iter), []); 1.28 + assertNotEq(new g(), iter); 1.29 +} 1.30 +TestGeneratorObject(); 1.31 + 1.32 + 1.33 +// Test the methods of generator objects. 1.34 +function TestGeneratorObjectMethods() { 1.35 + function* g() { yield 1; } 1.36 + var iter = g(); 1.37 + 1.38 + function TestNonGenerator(non_generator) { 1.39 + assertThrowsInstanceOf(function() { iter.next.call(non_generator); }, TypeError); 1.40 + assertThrowsInstanceOf(function() { iter.next.call(non_generator, 1); }, TypeError); 1.41 + assertThrowsInstanceOf(function() { iter.throw.call(non_generator, 1); }, TypeError); 1.42 + assertThrowsInstanceOf(function() { iter.close.call(non_generator); }, TypeError); 1.43 + } 1.44 + 1.45 + TestNonGenerator(1); 1.46 + TestNonGenerator({}); 1.47 + TestNonGenerator(function(){}); 1.48 + TestNonGenerator(g); 1.49 + TestNonGenerator(g.prototype); 1.50 +} 1.51 +TestGeneratorObjectMethods(); 1.52 + 1.53 + 1.54 +if (typeof reportCompare == "function") 1.55 + reportCompare(true, true);