Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/lazy.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 { Lazy } from '../../common/lazy.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
9
10
suite('Lazy', () => {
11
12
test('lazy values should only be resolved once', () => {
13
let counter = 0;
14
const value = new Lazy(() => ++counter);
15
16
assert.strictEqual(value.hasValue, false);
17
assert.strictEqual(value.value, 1);
18
assert.strictEqual(value.hasValue, true);
19
assert.strictEqual(value.value, 1); // make sure we did not evaluate again
20
});
21
22
test('lazy values handle error case', () => {
23
let counter = 0;
24
const value = new Lazy(() => { throw new Error(`${++counter}`); });
25
26
assert.strictEqual(value.hasValue, false);
27
assert.throws(() => value.value, /\b1\b/);
28
assert.strictEqual(value.hasValue, true);
29
assert.throws(() => value.value, /\b1\b/);
30
});
31
32
test('Should throw when accessing lazy value in initializer', () => {
33
const value = new Lazy<string>((): string => { return value.value; });
34
35
assert.throws(() => value.value, /Cannot read the value of a lazy that is being initialized/);
36
});
37
38
ensureNoDisposablesAreLeakedInTestSuite();
39
});
40
41