1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/collections/Map-forEach.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,59 @@ 1.4 +/* test Map.prototype.forEach */ 1.5 + 1.6 +load(libdir + 'asserts.js'); 1.7 +load(libdir + 'iteration.js'); 1.8 + 1.9 +// testing success conditions of Map.prototype.forEach 1.10 + 1.11 +var testMap = new Map(); 1.12 + 1.13 +function callback(value, key, map) { 1.14 + testMap.set(key, value); 1.15 + assertEq(map.has(key), true); 1.16 + assertEq(map.get(key), value); 1.17 +} 1.18 + 1.19 +var initialMap = new Map([['a', 1], ['b', 2.3], [false, undefined]]); 1.20 +initialMap.forEach(callback); 1.21 + 1.22 +// test that both the Maps are equal and are in same order 1.23 +var iterator = initialMap[std_iterator](); 1.24 +var count = 0; 1.25 +for (var [k, v] of testMap) { 1.26 + assertEq(initialMap.has(k), true); 1.27 + assertEq(initialMap.get(k), testMap.get(k)); 1.28 + assertIteratorNext(iterator, [k, testMap.get(k)]); 1.29 + count++; 1.30 +} 1.31 + 1.32 +//check both the Maps we have are equal in size 1.33 +assertEq(initialMap.size, testMap.size); 1.34 +assertEq(initialMap.size, count); 1.35 + 1.36 +var x = { abc: 'test'}; 1.37 +function callback2(value, key, map) { 1.38 + assertEq(x, this); 1.39 +} 1.40 +initialMap = new Map([['a', 1]]); 1.41 +initialMap.forEach(callback2, x); 1.42 + 1.43 +// testing failure conditions of Map.prototype.forEach 1.44 + 1.45 +var s = new Set([1, 2, 3]); 1.46 +assertThrowsInstanceOf(function() { 1.47 + Map.prototype.forEach.call(s, callback); 1.48 +}, TypeError, "Map.prototype.forEach should raise TypeError if not used on a Map"); 1.49 + 1.50 +var fn = 2; 1.51 +assertThrowsInstanceOf(function() { 1.52 + initialMap.forEach(fn); 1.53 +}, TypeError, "Map.prototype.forEach should raise TypeError if callback is not a function"); 1.54 + 1.55 +// testing that Map#forEach uses internal next() function and does not stop when 1.56 +// StopIteration exception is thrown 1.57 + 1.58 +var m = new Map([["one", 1]]); 1.59 +Object.getPrototypeOf(m[std_iterator]()).next = function () { throw "FAIL"; }; 1.60 +assertThrowsInstanceOf(function () { 1.61 + m.forEach(function () { throw StopIteration; }); 1.62 +}, StopIteration, "Map.prototype.forEach should use intrinsic next method.");