blob: eed22f575c75e32d19973c592e51c1333040495a [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
let suite = InspectorTest.createSyncSuite("Map");
suite.addTestCase({
name: "Map.fromObject",
test() {
const key1 = "key1";
const key2 = "key2";
const value1 = "value1";
const value2 = 200;
const object = {
[key1]: value1,
[key2]: value2,
};
let map = Map.fromObject(object);
InspectorTest.expectEqual(map.size, 2, "Map from simple object should have size 2.");
InspectorTest.expectThat(Array.shallowEqual(Array.from(map.keys()), [key1, key2]), "Map from simple object should have keys: key1 and key2.");
InspectorTest.expectThat(Array.shallowEqual(Array.from(map.values()), [value1, value2]), "Map from simple object should have values: value1 and value2.");
let emptyMap = Map.fromObject({});
InspectorTest.expectEqual(emptyMap.size, 0, "Map from empty object should be empty.");
}
});
suite.addTestCase({
name: "Map.prototype.take",
test() {
const key = "key";
const value = "value";
let map = new Map;
map.set(key, value);
InspectorTest.expectTrue(map.has(key), "Map has `key`.");
InspectorTest.expectEqual(map.get(key), value, "Map has `key` => `value`.");
InspectorTest.expectEqual(map.take(key), value, "Map take `key` => `value`.");
InspectorTest.expectFalse(map.has(key), "Map does not have `key`.");
InspectorTest.expectEqual(map.get(key), undefined, "Map has `key` => `undefined`.");
InspectorTest.expectEqual(map.take("doesNotExistKey"), undefined, "Map take `doesNotExistKey` => `undefined`.");
}
});
suite.addTestCase({
name: "Map.prototype.getOrInitialize.Raw",
test() {
const key = "key";
const value = "value";
const value2 = "value2";
let map = new Map;
InspectorTest.expectEqual(map.get(key), undefined, "Map does not have `key`.");
InspectorTest.expectEqual(map.getOrInitialize(key, value), value, "Map should have initialized `key` with `value`.");
InspectorTest.expectEqual(map.get(key), value, "Map does have `key` => `value`.");
InspectorTest.expectEqual(map.getOrInitialize(key, value2), value, "Map should get `key` with `value` without re-initializing.");
InspectorTest.expectEqual(map.get(key), value, "Map still has `key` => `value`.");
}
});
suite.addTestCase({
name: "Map.prototype.getOrInitialize.Function",
test() {
const key = "key";
const value = () => "value";
const value2 = () => InspectorTest.fail("Should not be reached.");
let map = new Map;
InspectorTest.expectEqual(map.get(key), undefined, "Map does not have `key`.");
InspectorTest.expectEqual(map.getOrInitialize(key, value), value(), "Map should have initialized `key` with `value`.");
InspectorTest.expectEqual(map.get(key), value(), "Map does have `key` => `value`.");
InspectorTest.expectEqual(map.getOrInitialize(key, value2), value(), "Map should get `key` with `value` without re-initializing.");
InspectorTest.expectEqual(map.get(key), value(), "Map still has `key` => `value`.");
}
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onLoad="runTest()">
</body>
</html>