| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="../../http/tests/inspector/resources/inspector-test.js"></script> |
| <script> |
| function test() |
| { |
| let suite = InspectorTest.createSyncSuite("Set"); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.find", |
| test() { |
| let set = new Set(["a", "b"]); |
| InspectorTest.expectEqual(set.find((item) => item === "b"), "b", "Set can find a item it holds."); |
| InspectorTest.expectEqual(set.find((item) => typeof item === "string"), "a", "Set finds the first item if the given predicate matches multiple items."); |
| InspectorTest.expectEqual(set.find((item) => typeof item === "number"), undefined, "Set returns 'undefined' when attempting to find an item if the given predicate doesn't match anything."); |
| }, |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.filter", |
| test() { |
| function test(set, callback, thisArg, expected, message) { |
| InspectorTest.expectShallowEqual(Array.from(set.filter(callback, thisArg)), expected, message); |
| } |
| |
| const set1 = new Set([1, 2, 3, 4]); |
| const set2 = new Set([2, 4]); |
| |
| test(set1, function(value) { return value % 2; }, null, [1, 3], "Set can filter based on the value."); |
| test(set1, function(value, key) { return key % 2; }, null, [1, 3], "Set can filter based on the key."); |
| test(set1, function(value, key, set) { return set === set1; }, null, [1, 2, 3, 4], "Set can filter based on the set object."); |
| test(set1, function(value, key, set) { return this.has(key); }, set2, [2, 4], "Set can filter with a different this."); |
| test(set1, function(value, key, set) { return this !== set; }, set2, [1, 2, 3, 4], "Set can filter based on the set object with a different this."); |
| }, |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.some", |
| test() { |
| function test(set, callback, thisArg, expected, message) { |
| InspectorTest.expectEqual(set.some(callback, thisArg), expected, message); |
| } |
| |
| const set1 = new Set([1, 2, 3, 4]); |
| const set2 = new Set([2, 4]); |
| |
| test(set1, function(value) { return value % 2; }, null, true, "Set can 'some' based on the value."); |
| test(set1, function(value, key) { return key === 5; }, null, false, "Set can 'some' based on the key."); |
| test(set1, function(value, key, set) { return set === set1; }, null, true, "Set can 'some' based on the set object."); |
| test(set1, function(value, key, set) { return this.has(key); }, set2, true, "Set can 'some' with a different this."); |
| test(set1, function(value, key, set) { return this === set; }, set2, false, "Set can 'some' based on the set object with a different this."); |
| }, |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.pushAll", |
| test() { |
| function test(iterable) { |
| let set = new Set([1, 2]); |
| |
| function stringify(object) |
| { |
| let string = JSON.stringify(Array.from(object), function(key, value) { |
| if (value instanceof Node) |
| return "<" + value.localName + ">"; |
| return value; |
| }); |
| return string.replace(/"(<[^>]*>)"/g, "$1"); |
| } |
| |
| let before = stringify(set); |
| set.addAll(iterable); |
| let after = stringify(set); |
| |
| InspectorTest.log(before + " => " + after); |
| } |
| |
| InspectorTest.log("Array:"); |
| test(["a1", "a2"]); |
| |
| InspectorTest.newline(); |
| |
| InspectorTest.log("Set:"); |
| const set = new Set(["s1", "s2"]); |
| test(set); |
| test(set.entries()); |
| test(set.keys()); |
| test(set.values()); |
| |
| InspectorTest.newline(); |
| |
| InspectorTest.log("Map:"); |
| const map = new Map([["m1k", "m1v"], ["m2k", "m2v"]]); |
| test(map); |
| test(map.entries()); |
| test(map.keys()); |
| test(map.values()); |
| |
| InspectorTest.newline(); |
| |
| InspectorTest.log("Object:"); |
| const object = { |
| o1k: "o1v", |
| o2k: "o2v", |
| }; |
| test(Object.entries(object)); |
| test(Object.keys(object)); |
| test(Object.values(object)); |
| |
| InspectorTest.newline(); |
| |
| InspectorTest.log("Generator:"); |
| function* generator() { |
| yield "g1"; |
| yield "g2"; |
| } |
| test(generator()); |
| |
| InspectorTest.newline(); |
| |
| InspectorTest.log("Node:"); |
| const node = document.createElement("div"); |
| node.appendChild(document.createElement("n1")); |
| node.appendChild(document.createElement("n2")); |
| test(node.children); |
| test(node.querySelectorAll("*")); |
| |
| InspectorTest.newline(); |
| |
| InspectorTest.log("Object (doesn't have [Symbol.iterator]):"); |
| InspectorTest.expectException(() => { |
| test(object); |
| }); |
| }, |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.take", |
| test() { |
| const key = "key"; |
| |
| let set = new Set; |
| set.add(key); |
| InspectorTest.expectEqual(set.take(key), key, "Set can take `key`."); |
| InspectorTest.expectFalse(set.has(key), "Set no longer has `key`."); |
| InspectorTest.expectEqual(set.take(key), undefined, "Set can NOT take `key`."); |
| InspectorTest.expectEqual(set.take("DNE"), undefined, "Set can NOT take `DNE`, as it does NOT exist."); |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.equals", |
| test() { |
| function testTrue(a, b, message) { |
| InspectorTest.expectThat((new Set(a)).equals(new Set(b)), message); |
| } |
| |
| function testFalse(a, b, message) { |
| InspectorTest.expectFalse((new Set(a)).equals(new Set(b)), message); |
| } |
| |
| const object1 = {a: 1}; |
| const object2 = {b: 2}; |
| const object3 = {c: 3}; |
| |
| testTrue([], [], "an empty set should be equal to another empty set."); |
| testTrue([1, "a", object1], [1, "a", object1], "a set should be equal to another set with the same values."); |
| testTrue([1, "a", object1], [object1, 1, "a"], "a set should be equal to another set with the same values in a different order."); |
| testFalse([1, "a", object1], [2, "b", object2], "a set should not be a equal to another set with different values."); |
| testFalse([1, 2, "a", "b", object1, object2], [1, 3, "a", "c", object1, object3], "a set should not be equal to another set with same and different values."); |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.firstValue", |
| test() { |
| function testFirstValue(values) { |
| let expected = values[0]; |
| InspectorTest.expectEqual(new Set(values).firstValue, expected, `Set with values [${values}] should have firstValue equal to ${expected}.`); |
| } |
| |
| testFirstValue([]); |
| testFirstValue([1, 2, 3]); |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "Set.prototype.lastValue", |
| test() { |
| function testLastValue(values) { |
| let expected = values.lastValue; |
| InspectorTest.expectEqual(new Set(values).lastValue, expected, `Set with values [${values}] should have lastValue equal to ${expected}.`); |
| } |
| |
| testLastValue([]); |
| testLastValue([1, 2, 3]); |
| } |
| }); |
| |
| suite.runTestCasesAndFinish(); |
| } |
| </script> |
| </head> |
| <body onLoad="runTest()"> |
| </body> |
| </html> |