array-iterator-generic.js (1058B)
1 // Array.prototype.iterator is generic. 2 // That is, it can be applied to arraylike objects and strings, not just arrays. 3 4 load(libdir + "asserts.js"); 5 load(libdir + "iteration.js"); 6 7 function test(obj) { 8 var it = Array.prototype[Symbol.iterator].call(obj); 9 var ki = Array.prototype.keys.call(obj); 10 var ei = Array.prototype.entries.call(obj); 11 for (var i = 0; i < (obj.length >>> 0); i++) { 12 assertIteratorNext(it, obj[i]); 13 assertIteratorNext(ki, i); 14 assertIteratorNext(ei, [i, obj[i]]); 15 } 16 assertIteratorDone(it, undefined); 17 assertIteratorDone(ki, undefined); 18 assertIteratorDone(ei, undefined); 19 } 20 21 test({length: 0}); 22 test({length: 0, 0: 'x', 1: 'y'}); 23 test({length: 2, 0: 'x', 1: 'y'}); 24 test(Object.create(['x', 'y', 'z'])); 25 test(Object.create({length: 2, 0: 'x', 1: 'y'})); 26 test(""); 27 test("ponies"); 28 29 // Perverse length values. 30 test({length: "011", 9: 9, 10: 10, 11: 11}); 31 test({length: -0}); 32 test({length: 2.7, 0: 0, 1: 1, 2: 2}); 33 test({length: {valueOf: function () { return 3; }}, 0: 0, 1: 1, 2: 2});