Path: blob/main/src/vs/base/test/common/collections.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import * as collections from '../../common/collections.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';89suite('Collections', () => {1011ensureNoDisposablesAreLeakedInTestSuite();1213test('groupBy', () => {1415const group1 = 'a', group2 = 'b';16const value1 = 1, value2 = 2, value3 = 3;17const source = [18{ key: group1, value: value1 },19{ key: group1, value: value2 },20{ key: group2, value: value3 },21];2223const grouped = collections.groupBy(source, x => x.key);2425// Group 126assert.strictEqual(grouped[group1].length, 2);27assert.strictEqual(grouped[group1][0].value, value1);28assert.strictEqual(grouped[group1][1].value, value2);2930// Group 231assert.strictEqual(grouped[group2].length, 1);32assert.strictEqual(grouped[group2][0].value, value3);33});3435suite('SetWithKey', () => {36let setWithKey: collections.SetWithKey<{ someProp: string }>;3738const initialValues = ['a', 'b', 'c'].map(s => ({ someProp: s }));39setup(() => {40setWithKey = new collections.SetWithKey<{ someProp: string }>(initialValues, value => value.someProp);41});4243test('size', () => {44assert.strictEqual(setWithKey.size, 3);45});4647test('add', () => {48setWithKey.add({ someProp: 'd' });49assert.strictEqual(setWithKey.size, 4);50assert.strictEqual(setWithKey.has({ someProp: 'd' }), true);51});5253test('delete', () => {54assert.strictEqual(setWithKey.has({ someProp: 'b' }), true);55setWithKey.delete({ someProp: 'b' });56assert.strictEqual(setWithKey.size, 2);57assert.strictEqual(setWithKey.has({ someProp: 'b' }), false);58});5960test('has', () => {61assert.strictEqual(setWithKey.has({ someProp: 'a' }), true);62assert.strictEqual(setWithKey.has({ someProp: 'b' }), true);63});6465test('entries', () => {66const entries = Array.from(setWithKey.entries());67assert.deepStrictEqual(entries, initialValues.map(value => [value, value]));68});6970test('keys and values', () => {71const keys = Array.from(setWithKey.keys());72const values = Array.from(setWithKey.values());73assert.deepStrictEqual(keys, initialValues);74assert.deepStrictEqual(values, initialValues);75});7677test('clear', () => {78setWithKey.clear();79assert.strictEqual(setWithKey.size, 0);80});8182test('forEach', () => {83const values: any[] = [];84setWithKey.forEach(value => values.push(value));85assert.deepStrictEqual(values, initialValues);86});8788test('iterator', () => {89const values: any[] = [];90for (const value of setWithKey) {91values.push(value);92}93assert.deepStrictEqual(values, initialValues);94});9596test('toStringTag', () => {97assert.strictEqual(setWithKey[Symbol.toStringTag], 'SetWithKey');98});99});100});101102103