Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/commands/test/common/commands.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
import assert from 'assert';
6
import { combinedDisposable } from '../../../../base/common/lifecycle.js';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
8
import { CommandsRegistry } from '../../common/commands.js';
9
10
suite('Command Tests', function () {
11
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
test('register command - no handler', function () {
15
assert.throws(() => CommandsRegistry.registerCommand('foo', null!));
16
});
17
18
test('register/dispose', () => {
19
const command = function () { };
20
const reg = CommandsRegistry.registerCommand('foo', command);
21
assert.ok(CommandsRegistry.getCommand('foo')!.handler === command);
22
reg.dispose();
23
assert.ok(CommandsRegistry.getCommand('foo') === undefined);
24
});
25
26
test('register/register/dispose', () => {
27
const command1 = function () { };
28
const command2 = function () { };
29
30
// dispose overriding command
31
let reg1 = CommandsRegistry.registerCommand('foo', command1);
32
assert.ok(CommandsRegistry.getCommand('foo')!.handler === command1);
33
34
let reg2 = CommandsRegistry.registerCommand('foo', command2);
35
assert.ok(CommandsRegistry.getCommand('foo')!.handler === command2);
36
reg2.dispose();
37
38
assert.ok(CommandsRegistry.getCommand('foo')!.handler === command1);
39
reg1.dispose();
40
assert.ok(CommandsRegistry.getCommand('foo') === undefined);
41
42
// dispose override command first
43
reg1 = CommandsRegistry.registerCommand('foo', command1);
44
reg2 = CommandsRegistry.registerCommand('foo', command2);
45
assert.ok(CommandsRegistry.getCommand('foo')!.handler === command2);
46
47
reg1.dispose();
48
assert.ok(CommandsRegistry.getCommand('foo')!.handler === command2);
49
50
reg2.dispose();
51
assert.ok(CommandsRegistry.getCommand('foo') === undefined);
52
});
53
54
test('command with description', function () {
55
56
const r1 = CommandsRegistry.registerCommand('test', function (accessor, args) {
57
assert.ok(typeof args === 'string');
58
});
59
60
const r2 = CommandsRegistry.registerCommand('test2', function (accessor, args) {
61
assert.ok(typeof args === 'string');
62
});
63
64
const r3 = CommandsRegistry.registerCommand({
65
id: 'test3',
66
handler: function (accessor, args) {
67
return true;
68
},
69
metadata: {
70
description: 'a command',
71
args: [{ name: 'value', constraint: Number }]
72
}
73
});
74
75
CommandsRegistry.getCommands().get('test')!.handler.apply(undefined, [undefined!, 'string']);
76
CommandsRegistry.getCommands().get('test2')!.handler.apply(undefined, [undefined!, 'string']);
77
assert.throws(() => CommandsRegistry.getCommands().get('test3')!.handler.apply(undefined, [undefined!, 'string']));
78
assert.strictEqual(CommandsRegistry.getCommands().get('test3')!.handler.apply(undefined, [undefined!, 1]), true);
79
80
combinedDisposable(r1, r2, r3).dispose();
81
});
82
});
83
84