|
1 /* test Set.prototype.forEach */ |
|
2 |
|
3 load(libdir + 'asserts.js'); |
|
4 load(libdir + 'iteration.js'); |
|
5 |
|
6 // testing success conditions of Set.prototype.forEach |
|
7 |
|
8 var testSet = new Set(); |
|
9 |
|
10 function callback(value, key, set) { |
|
11 assertEq(value, key); |
|
12 testSet.add(value); |
|
13 assertEq(set.has(key), true); |
|
14 } |
|
15 |
|
16 var initialSet = new Set(['a', 1, undefined]); |
|
17 initialSet.forEach(callback); |
|
18 |
|
19 // test that both the Sets are equal and are in same order |
|
20 var iterator = initialSet[std_iterator](); |
|
21 var count = 0; |
|
22 for (var v of testSet) { |
|
23 assertEq(initialSet.has(v), true); |
|
24 assertIteratorNext(iterator, v); |
|
25 count++; |
|
26 } |
|
27 |
|
28 //check both the Sets we have are equal in size |
|
29 assertEq(initialSet.size, testSet.size); |
|
30 assertEq(initialSet.size, count); |
|
31 |
|
32 var x = { abc: 'test'}; |
|
33 function callback2(value, key, set) { |
|
34 assertEq(x, this); |
|
35 } |
|
36 initialSet = new Set(['a']); |
|
37 initialSet.forEach(callback2, x); |
|
38 |
|
39 // testing failure conditions of Set.prototype.forEach |
|
40 |
|
41 var m = new Map([['a', 1], ['b', 2.3], ['c', undefined]]); |
|
42 assertThrowsInstanceOf(function() { |
|
43 Set.prototype.forEach.call(m, callback); |
|
44 }, TypeError, "Set.prototype.forEach should raise TypeError if not a used on a Set"); |
|
45 |
|
46 var fn = 2; |
|
47 assertThrowsInstanceOf(function() { |
|
48 initialSet.forEach(fn); |
|
49 }, TypeError, "Set.prototype.forEach should raise TypeError if callback is not a function"); |