-
Notifications
You must be signed in to change notification settings - Fork 30k
/
Copy pathcommands.test.ts
97 lines (76 loc) · 3.33 KB
/
commands.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
suite('Command Tests', function () {
test('register command - no handler', function () {
assert.throws(() => CommandsRegistry.registerCommand('foo', null));
});
test('register/dispose', function () {
const command = function () { };
const reg = CommandsRegistry.registerCommand('foo', command);
assert.ok(CommandsRegistry.getCommand('foo').handler === command);
reg.dispose();
assert.ok(CommandsRegistry.getCommand('foo') === undefined);
});
test('register/register/dispose', function () {
const command1 = function () { };
const command2 = function () { };
// dispose overriding command
let reg1 = CommandsRegistry.registerCommand('foo', command1);
assert.ok(CommandsRegistry.getCommand('foo').handler === command1);
let reg2 = CommandsRegistry.registerCommand('foo', command2);
assert.ok(CommandsRegistry.getCommand('foo').handler === command2);
reg2.dispose();
assert.ok(CommandsRegistry.getCommand('foo').handler === command1);
reg1.dispose();
assert.ok(CommandsRegistry.getCommand('foo') === void 0);
// dispose override command first
reg1 = CommandsRegistry.registerCommand('foo', command1);
reg2 = CommandsRegistry.registerCommand('foo', command2);
assert.ok(CommandsRegistry.getCommand('foo').handler === command2);
reg1.dispose();
assert.ok(CommandsRegistry.getCommand('foo').handler === command2);
reg2.dispose();
assert.ok(CommandsRegistry.getCommand('foo') === void 0);
});
test('command with description', function () {
CommandsRegistry.registerCommand('test', function (accessor, args) {
assert.ok(typeof args === 'string');
});
CommandsRegistry.registerCommand('test2', function (accessor, args) {
assert.ok(typeof args === 'string');
});
CommandsRegistry.registerCommand({
id: 'test3',
handler: function (accessor, args) {
return true;
},
description: {
description: 'a command',
args: [{ name: 'value', constraint: Number }]
}
});
CommandsRegistry.getCommands()['test'].handler.apply(undefined, [undefined, 'string']);
CommandsRegistry.getCommands()['test2'].handler.apply(undefined, [undefined, 'string']);
assert.throws(() => CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 'string']));
assert.equal(CommandsRegistry.getCommands()['test3'].handler.apply(undefined, [undefined, 1]), true);
});
test('CommandsRegistry with precondition', function () {
let r1 = CommandsRegistry.registerCommand('foo', () => { });
const precondition = new RawContextKey<boolean>('ddd', false);
let r2 = CommandsRegistry.registerCommand({
id: 'bar',
handler: () => { },
precondition
});
assert.ok(CommandsRegistry.getCommand('bar').precondition === precondition);
assert.equal(CommandsRegistry.getCommand('foo').precondition, undefined);
r1.dispose();
r2.dispose();
});
});