-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathMapUtilities.test.ts
50 lines (43 loc) · 1.65 KB
/
MapUtilities.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as Platform from './platform.js';
describe('MapUtilities', () => {
describe('inverse', () => {
it('inverts the map returning a multimap with the map\'s values as keys and the map\'s keys as values', () => {
const pairs: ReadonlyArray<readonly[string, number]> = [
['a', 1],
['b', 2],
['c', 3],
['d', 1],
];
const map = new Map(pairs);
const inverse = Platform.MapUtilities.inverse(map);
for (const [, value] of pairs) {
assert.sameMembers([...inverse.get(value)], [...getKeys(value)]);
}
function getKeys(lookupValue: number): Set<string> {
const keys = new Set<string>();
for (const [key, value] of pairs) {
if (value === lookupValue) {
keys.add(key);
}
}
return keys;
}
});
});
describe('getWithDefault', () => {
it('returns the default when it has no value', () => {
const expected = new Set();
const returned = Platform.MapUtilities.getWithDefault(new Map(), 'foo', () => expected);
assert.strictEqual(expected, returned);
});
it('returns the same item on successive calls', () => {
const data = new Map<string, Set<void>>();
const returnedFirst = Platform.MapUtilities.getWithDefault(data, 'foo', () => new Set());
const returnedSecond = Platform.MapUtilities.getWithDefault(data, 'foo', () => new Set());
assert.strictEqual(returnedFirst, returnedSecond);
});
});
});