-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
lazy.test.ts
30 lines (24 loc) · 1.08 KB
/
lazy.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Lazy } from 'vs/base/common/lazy';
suite('Lazy', () => {
test('lazy values should only be resolved once', () => {
let counter = 0;
const value = new Lazy(() => ++counter);
assert.strictEqual(value.hasValue, false);
assert.strictEqual(value.value, 1);
assert.strictEqual(value.hasValue, true);
assert.strictEqual(value.value, 1); // make sure we did not evaluate again
});
test('lazy values handle error case', () => {
let counter = 0;
const value = new Lazy(() => { throw new Error(`${++counter}`); });
assert.strictEqual(value.hasValue, false);
assert.throws(() => value.value, /\b1\b/);
assert.strictEqual(value.hasValue, true);
assert.throws(() => value.value, /\b1\b/);
});
});