michael@0: /* test Map.prototype.forEach */ michael@0: michael@0: load(libdir + 'asserts.js'); michael@0: load(libdir + 'iteration.js'); michael@0: michael@0: // testing success conditions of Map.prototype.forEach michael@0: michael@0: var testMap = new Map(); michael@0: michael@0: function callback(value, key, map) { michael@0: testMap.set(key, value); michael@0: assertEq(map.has(key), true); michael@0: assertEq(map.get(key), value); michael@0: } michael@0: michael@0: var initialMap = new Map([['a', 1], ['b', 2.3], [false, undefined]]); michael@0: initialMap.forEach(callback); michael@0: michael@0: // test that both the Maps are equal and are in same order michael@0: var iterator = initialMap[std_iterator](); michael@0: var count = 0; michael@0: for (var [k, v] of testMap) { michael@0: assertEq(initialMap.has(k), true); michael@0: assertEq(initialMap.get(k), testMap.get(k)); michael@0: assertIteratorNext(iterator, [k, testMap.get(k)]); michael@0: count++; michael@0: } michael@0: michael@0: //check both the Maps we have are equal in size michael@0: assertEq(initialMap.size, testMap.size); michael@0: assertEq(initialMap.size, count); michael@0: michael@0: var x = { abc: 'test'}; michael@0: function callback2(value, key, map) { michael@0: assertEq(x, this); michael@0: } michael@0: initialMap = new Map([['a', 1]]); michael@0: initialMap.forEach(callback2, x); michael@0: michael@0: // testing failure conditions of Map.prototype.forEach michael@0: michael@0: var s = new Set([1, 2, 3]); michael@0: assertThrowsInstanceOf(function() { michael@0: Map.prototype.forEach.call(s, callback); michael@0: }, TypeError, "Map.prototype.forEach should raise TypeError if not used on a Map"); michael@0: michael@0: var fn = 2; michael@0: assertThrowsInstanceOf(function() { michael@0: initialMap.forEach(fn); michael@0: }, TypeError, "Map.prototype.forEach should raise TypeError if callback is not a function"); michael@0: michael@0: // testing that Map#forEach uses internal next() function and does not stop when michael@0: // StopIteration exception is thrown michael@0: michael@0: var m = new Map([["one", 1]]); michael@0: Object.getPrototypeOf(m[std_iterator]()).next = function () { throw "FAIL"; }; michael@0: assertThrowsInstanceOf(function () { michael@0: m.forEach(function () { throw StopIteration; }); michael@0: }, StopIteration, "Map.prototype.forEach should use intrinsic next method.");