michael@0: /* test Set.prototype.forEach */ michael@0: michael@0: load(libdir + 'asserts.js'); michael@0: load(libdir + 'iteration.js'); michael@0: michael@0: // testing success conditions of Set.prototype.forEach michael@0: michael@0: var testSet = new Set(); michael@0: michael@0: function callback(value, key, set) { michael@0: assertEq(value, key); michael@0: testSet.add(value); michael@0: assertEq(set.has(key), true); michael@0: } michael@0: michael@0: var initialSet = new Set(['a', 1, undefined]); michael@0: initialSet.forEach(callback); michael@0: michael@0: // test that both the Sets are equal and are in same order michael@0: var iterator = initialSet[std_iterator](); michael@0: var count = 0; michael@0: for (var v of testSet) { michael@0: assertEq(initialSet.has(v), true); michael@0: assertIteratorNext(iterator, v); michael@0: count++; michael@0: } michael@0: michael@0: //check both the Sets we have are equal in size michael@0: assertEq(initialSet.size, testSet.size); michael@0: assertEq(initialSet.size, count); michael@0: michael@0: var x = { abc: 'test'}; michael@0: function callback2(value, key, set) { michael@0: assertEq(x, this); michael@0: } michael@0: initialSet = new Set(['a']); michael@0: initialSet.forEach(callback2, x); michael@0: michael@0: // testing failure conditions of Set.prototype.forEach michael@0: michael@0: var m = new Map([['a', 1], ['b', 2.3], ['c', undefined]]); michael@0: assertThrowsInstanceOf(function() { michael@0: Set.prototype.forEach.call(m, callback); michael@0: }, TypeError, "Set.prototype.forEach should raise TypeError if not a used on a Set"); michael@0: michael@0: var fn = 2; michael@0: assertThrowsInstanceOf(function() { michael@0: initialSet.forEach(fn); michael@0: }, TypeError, "Set.prototype.forEach should raise TypeError if callback is not a function");