Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/collections.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import * as collections from '../../common/collections.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
9
10
suite('Collections', () => {
11
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
test('groupBy', () => {
15
16
const group1 = 'a', group2 = 'b';
17
const value1 = 1, value2 = 2, value3 = 3;
18
const source = [
19
{ key: group1, value: value1 },
20
{ key: group1, value: value2 },
21
{ key: group2, value: value3 },
22
];
23
24
const grouped = collections.groupBy(source, x => x.key);
25
26
// Group 1
27
assert.strictEqual(grouped[group1].length, 2);
28
assert.strictEqual(grouped[group1][0].value, value1);
29
assert.strictEqual(grouped[group1][1].value, value2);
30
31
// Group 2
32
assert.strictEqual(grouped[group2].length, 1);
33
assert.strictEqual(grouped[group2][0].value, value3);
34
});
35
36
suite('SetWithKey', () => {
37
let setWithKey: collections.SetWithKey<{ someProp: string }>;
38
39
const initialValues = ['a', 'b', 'c'].map(s => ({ someProp: s }));
40
setup(() => {
41
setWithKey = new collections.SetWithKey<{ someProp: string }>(initialValues, value => value.someProp);
42
});
43
44
test('size', () => {
45
assert.strictEqual(setWithKey.size, 3);
46
});
47
48
test('add', () => {
49
setWithKey.add({ someProp: 'd' });
50
assert.strictEqual(setWithKey.size, 4);
51
assert.strictEqual(setWithKey.has({ someProp: 'd' }), true);
52
});
53
54
test('delete', () => {
55
assert.strictEqual(setWithKey.has({ someProp: 'b' }), true);
56
setWithKey.delete({ someProp: 'b' });
57
assert.strictEqual(setWithKey.size, 2);
58
assert.strictEqual(setWithKey.has({ someProp: 'b' }), false);
59
});
60
61
test('has', () => {
62
assert.strictEqual(setWithKey.has({ someProp: 'a' }), true);
63
assert.strictEqual(setWithKey.has({ someProp: 'b' }), true);
64
});
65
66
test('entries', () => {
67
const entries = Array.from(setWithKey.entries());
68
assert.deepStrictEqual(entries, initialValues.map(value => [value, value]));
69
});
70
71
test('keys and values', () => {
72
const keys = Array.from(setWithKey.keys());
73
const values = Array.from(setWithKey.values());
74
assert.deepStrictEqual(keys, initialValues);
75
assert.deepStrictEqual(values, initialValues);
76
});
77
78
test('clear', () => {
79
setWithKey.clear();
80
assert.strictEqual(setWithKey.size, 0);
81
});
82
83
test('forEach', () => {
84
const values: any[] = [];
85
setWithKey.forEach(value => values.push(value));
86
assert.deepStrictEqual(values, initialValues);
87
});
88
89
test('iterator', () => {
90
const values: any[] = [];
91
for (const value of setWithKey) {
92
values.push(value);
93
}
94
assert.deepStrictEqual(values, initialValues);
95
});
96
97
test('toStringTag', () => {
98
assert.strictEqual(setWithKey[Symbol.toStringTag], 'SetWithKey');
99
});
100
});
101
});
102
103