260 lines
9.5 KiB
HTML
260 lines
9.5 KiB
HTML
<!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.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.intersects",
|
|
test() {
|
|
function testTrue(a, b, message) {
|
|
InspectorTest.expectThat((new Set(a)).intersects(new Set(b)), message);
|
|
}
|
|
|
|
function testFalse(a, b, message) {
|
|
InspectorTest.expectFalse((new Set(a)).intersects(new Set(b)), message);
|
|
}
|
|
|
|
const object1 = {a: 1};
|
|
const object2 = {b: 2};
|
|
const object3 = {c: 3};
|
|
|
|
testFalse([], [], "an empty set should not intersect another empty set.");
|
|
testFalse([1, "a", object1], [], "a non-empty set should not intersect an empty set.");
|
|
testTrue([1, "a", object1], [1, "a", object1], "a set should intersect another set with the same values.");
|
|
testFalse([1, "a", object1], [2, "b", object2], "a set should not intersect another set with different values.");
|
|
testTrue([1, "a", object1], [1, 3, "a", "c", object1, object3], "a set should intersect another set with same and additional values.");
|
|
testTrue([1, 2, "a", "b", object1, object2], [1, 3, "a", "c", object1, object3], "a set should intersect another set with same and different values.");
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "Set.prototype.isSubsetOf",
|
|
test() {
|
|
function testTrue(a, b, message) {
|
|
InspectorTest.expectThat((new Set(a)).isSubsetOf(new Set(b)), message);
|
|
}
|
|
|
|
function testFalse(a, b, message) {
|
|
InspectorTest.expectFalse((new Set(a)).isSubsetOf(new Set(b)), message);
|
|
}
|
|
|
|
const object1 = {a: 1};
|
|
const object2 = {b: 2};
|
|
const object3 = {c: 3};
|
|
|
|
testTrue([], [], "an empty set should be a subset of another empty set.");
|
|
testTrue([], [2, "b", object2], "an empty set should be a subset of a non-empty set.");
|
|
testTrue([1, "a", object1], [1, "a", object1], "a set should be a subset of another set with the same values.");
|
|
testFalse([1, "a", object1], [2, "b", object2], "a set should not be a subset of another set with different values.");
|
|
testTrue([1, "a", object1], [1, 3, "a", "c", object1, object3], "a set should be a subset of another set with same and additional values.");
|
|
testFalse([1, 2, "a", "b", object1, object2], [1, 3, "a", "c", object1, object3], "a set should not be a subset of another set with same and different values.");
|
|
}
|
|
});
|
|
|
|
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.difference",
|
|
test() {
|
|
function testDifference({aValues, bValues, expectedDifference}) {
|
|
let a = new Set(aValues);
|
|
let b = new Set(bValues);
|
|
|
|
InspectorTest.log(`Given a Set with values [${aValues}], and another Set with values [${bValues}]:`);
|
|
|
|
let difference = a.difference(b);
|
|
InspectorTest.expectThat(difference.equals(new Set(expectedDifference)), `Set difference should be [${expectedDifference}].`);
|
|
InspectorTest.log("");
|
|
}
|
|
|
|
testDifference({
|
|
aValues: [],
|
|
bValues: [],
|
|
expectedDifference: [],
|
|
});
|
|
|
|
testDifference({
|
|
aValues: [1, 2, 3],
|
|
bValues: [],
|
|
expectedDifference: [1, 2, 3],
|
|
});
|
|
|
|
testDifference({
|
|
aValues: [],
|
|
bValues: [1, 2, 3],
|
|
expectedDifference: [],
|
|
});
|
|
|
|
testDifference({
|
|
aValues: [1, 2, 3],
|
|
bValues: [2, 3, 4],
|
|
expectedDifference: [1],
|
|
});
|
|
}
|
|
});
|
|
|
|
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>
|