diff --git a/src/vs/base/test/common/collections.test.ts b/src/vs/base/test/common/collections.test.ts index 8db0058c7cdc1d2b1847427982f3dded09390594..1b9d41b109660889289e6231f1847d93a9e98037 100644 --- a/src/vs/base/test/common/collections.test.ts +++ b/src/vs/base/test/common/collections.test.ts @@ -31,6 +31,69 @@ suite('Collections', () => { dict['toString'] = 123; collections.forEach(dict, () => count++); assert.equal(count, 1); + + collections.forEach(dict, () => false); + + collections.forEach(dict, (x, remove) => remove()); + assert.equal(dict['toString'], null); + + // don't iterate over properties that are not on the object itself + let test = Object.create({ 'derived': true }); + collections.forEach(test, () => assert(false)); + }); + + test('lookupOrInsert - should not insert if found', () => { + const property = 123; + + let from = collections.createNumberDictionary(); + from[property] = 'whatever'; + + collections.lookupOrInsert(from, property, () => assert(false)); + }); + + test('lookupOrInsert - should insert if not found', () => { + + const expected = 'alternate', property = 'test'; + + let fromWithValue = collections.createStringDictionary(); + collections.lookupOrInsert(fromWithValue, property, expected); + assert.equal(fromWithValue[property], expected); + + let fromWithCallback = collections.createStringDictionary(); + collections.lookupOrInsert(fromWithCallback, property, () => expected); + assert.equal(fromWithCallback[property], expected); + }); + + test('groupBy', () => { + + const group1 = 'a', group2 = 'b'; + const value1 = 1, value2 = 2, value3 = 3; + let source = [ + { key: group1, value: value1 }, + { key: group1, value: value2 }, + { key: group2, value: value3 }, + ]; + + let grouped = collections.groupBy(source, x => x.key); + + // Group 1 + assert.equal(grouped[group1].length, 2); + assert.equal(grouped[group1][0].value, value1); + assert.equal(grouped[group1][1].value, value2); + + // Group 2 + assert.equal(grouped[group2].length, 1); + assert.equal(grouped[group2][0].value, value3); + }); + + test('insert', () => { + + const expected = 'value', hashFn = x => x.toString(); + + let into = collections.createStringDictionary(); + collections.insert(into, expected, hashFn); + + assert.equal(into[expected], expected); }); test('remove', () => {