Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Web Inspector: implement Multimap.prototype.take()
https://bugs.webkit.org/show_bug.cgi?id=219231

Reviewed by Devin Rousso.

Source/WebInspectorUI:

* UserInterface/Base/Multimap.js:
(Multimap.prototype.take):
* UserInterface/Base/Utilities.js:
(value):

LayoutTests:

* inspector/unit-tests/multimap-expected.txt:
* inspector/unit-tests/multimap.html:
* inspector/unit-tests/set-utilities.html:


Canonical link: https://commits.webkit.org/231858@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@270149 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
burg committed Nov 21, 2020
1 parent d68dafa commit 93cc6c3
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 7 deletions.
11 changes: 11 additions & 0 deletions LayoutTests/ChangeLog
@@ -1,3 +1,14 @@
2020-11-21 Brian Burg <bburg@apple.com>

Web Inspector: implement Multimap.prototype.take()
https://bugs.webkit.org/show_bug.cgi?id=219231

Reviewed by Devin Rousso.

* inspector/unit-tests/multimap-expected.txt:
* inspector/unit-tests/multimap.html:
* inspector/unit-tests/set-utilities.html:

2020-11-21 Aditya Keerthi <akeerthi@apple.com>

Space between minute and meridiem fields in time inputs is too large
Expand Down
29 changes: 29 additions & 0 deletions LayoutTests/inspector/unit-tests/multimap-expected.txt
Expand Up @@ -57,6 +57,35 @@ PASS: Nothing was removed for key "badger".
PASS: Values were removed for key "opossum".
[["raccoon","opossum"]]

-- Running test case: Multimap.prototype.take.SingleKeyAndValue
[[0,1],[2,3],[2,4]]
1
PASS: The key 0 and the value 1 were taken.
PASS: Only one key should remain.
[[2,3],[2,4]]
undefined
PASS: Nothing should have been taken.
PASS: Only one key should remain.
[[2,3],[2,4]]
3
PASS: The key 2 and the value 3 were taken.
PASS: Only one key should remain.
[[2,4]]
4
PASS: The key 2 and the value 4 were taken.
PASS: No more keys should remain.
[]

-- Running test case: Multimap.prototype.take.AllValuesForKey
[["opossum","badger"],["opossum","raccoon"],["raccoon","opossum"]]
PASS: Nothing was removed for key "badger".
[["opossum","badger"],["opossum","raccoon"],["raccoon","opossum"]]
PASS: Only one key should remain.
PASS: Two values from the key "opossum" should be taken.
PASS: Result should include "badger".
PASS: Result should include "raccoon".
[["raccoon","opossum"]]

-- Running test case: Multimap.prototype.clear
[["one","two"],["one","five"],["three","four"],["three","six"]]
[]
Expand Down
66 changes: 66 additions & 0 deletions LayoutTests/inspector/unit-tests/multimap.html
Expand Up @@ -164,6 +164,72 @@
},
});

suite.addTestCase({
name: "Multimap.prototype.take.SingleKeyAndValue",
test() {
let multimap = new Multimap;
let result;

multimap.add(0, 1);
multimap.add(2, 3);
multimap.add(2, 4);

InspectorTest.log(multimap);

result = multimap.take(0, 1);
InspectorTest.log(result);
InspectorTest.expectEqual(result, 1, "The key 0 and the value 1 were taken.");
InspectorTest.expectEqual(multimap.size, 1, "Only one key should remain.");

InspectorTest.log(multimap);

result = multimap.take(5, 1);
InspectorTest.log(result);
InspectorTest.expectEqual(result, undefined, "Nothing should have been taken.");
InspectorTest.expectEqual(multimap.size, 1, "Only one key should remain.");

InspectorTest.log(multimap);

result = multimap.take(2, 3)
InspectorTest.log(result);
InspectorTest.expectEqual(result, 3, "The key 2 and the value 3 were taken.");
InspectorTest.expectEqual(multimap.size, 1, "Only one key should remain.");

InspectorTest.log(multimap);

result = multimap.take(2, 4)
InspectorTest.log(result);
InspectorTest.expectEqual(result, 4, "The key 2 and the value 4 were taken.");
InspectorTest.expectEqual(multimap.size, 0, "No more keys should remain.");

InspectorTest.log(multimap);
},
});

suite.addTestCase({
name: "Multimap.prototype.take.AllValuesForKey",
test() {
let multimap = new Multimap;

multimap.add("opossum", "badger");
multimap.add("opossum", "raccoon");
multimap.add("raccoon", "opossum");

InspectorTest.log(multimap);
InspectorTest.expectFalse(multimap.take("badger"), `Nothing was removed for key "badger".`);

InspectorTest.log(multimap);

let result = multimap.take("opossum");
InspectorTest.expectEqual(multimap.size, 1, `Only one key should remain.`);
InspectorTest.expectEqual(result.size, 2, `Two values from the key "opossum" should be taken.`);
InspectorTest.expectThat(result.has("badger"), `Result should include "badger".`);
InspectorTest.expectThat(result.has("raccoon"), `Result should include "raccoon".`);

InspectorTest.log(multimap);
},
});

suite.addTestCase({
name: "Multimap.prototype.clear",
test() {
Expand Down
6 changes: 3 additions & 3 deletions LayoutTests/inspector/unit-tests/set-utilities.html
Expand Up @@ -106,10 +106,10 @@

let set = new Set;
set.add(key);
InspectorTest.expectTrue(set.take(key), "Set can take `key`.");
InspectorTest.expectEqual(set.take(key), key, "Set can take `key`.");
InspectorTest.expectFalse(set.has(key), "Set no longer has `key`.");
InspectorTest.expectFalse(set.take(key), "Set can NOT take `key`.");
InspectorTest.expectFalse(set.take("DNE"), "Set can NOT take `DNE`, as it does NOT exist.");
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.");
}
});

Expand Down
12 changes: 12 additions & 0 deletions Source/WebInspectorUI/ChangeLog
@@ -1,3 +1,15 @@
2020-11-21 Brian Burg <bburg@apple.com>

Web Inspector: implement Multimap.prototype.take()
https://bugs.webkit.org/show_bug.cgi?id=219231

Reviewed by Devin Rousso.

* UserInterface/Base/Multimap.js:
(Multimap.prototype.take):
* UserInterface/Base/Utilities.js:
(value):

2020-11-20 Devin Rousso <drousso@apple.com>

Web Inspector: drop `shown`/`hidden` in favor of `attached`/`detached`
Expand Down
18 changes: 17 additions & 1 deletion Source/WebInspectorUI/UserInterface/Base/Multimap.js
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Apple Inc. All rights reserved.
* Copyright (C) 2018-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -83,6 +83,22 @@ class Multimap
return deleted;
}

take(key, value)
{
// Allow an entire key to be removed by not passing a value.
if (arguments.length === 1)
return this._map.take(key);

let valueSet = this._map.get(key);
if (!valueSet)
return undefined;

let result = valueSet.take(value);
if (!valueSet.size)
this._map.delete(key);
return result;
}

clear()
{
this._map.clear();
Expand Down
8 changes: 5 additions & 3 deletions Source/WebInspectorUI/UserInterface/Base/Utilities.js
Expand Up @@ -178,10 +178,12 @@ Object.defineProperty(Set.prototype, "take",
{
value(key)
{
let exists = this.has(key);
if (exists)
if (this.has(key)) {
this.delete(key);
return exists;
return key;
}

return undefined;
}
});

Expand Down

0 comments on commit 93cc6c3

Please sign in to comment.