Path: blob/main/src/vs/platform/commands/test/common/commands.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import { combinedDisposable } from '../../../../base/common/lifecycle.js';6import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';7import { CommandsRegistry } from '../../common/commands.js';89suite('Command Tests', function () {1011ensureNoDisposablesAreLeakedInTestSuite();1213test('register command - no handler', function () {14assert.throws(() => CommandsRegistry.registerCommand('foo', null!));15});1617test('register/dispose', () => {18const command = function () { };19const reg = CommandsRegistry.registerCommand('foo', command);20assert.ok(CommandsRegistry.getCommand('foo')!.handler === command);21reg.dispose();22assert.ok(CommandsRegistry.getCommand('foo') === undefined);23});2425test('register/register/dispose', () => {26const command1 = function () { };27const command2 = function () { };2829// dispose overriding command30let reg1 = CommandsRegistry.registerCommand('foo', command1);31assert.ok(CommandsRegistry.getCommand('foo')!.handler === command1);3233let reg2 = CommandsRegistry.registerCommand('foo', command2);34assert.ok(CommandsRegistry.getCommand('foo')!.handler === command2);35reg2.dispose();3637assert.ok(CommandsRegistry.getCommand('foo')!.handler === command1);38reg1.dispose();39assert.ok(CommandsRegistry.getCommand('foo') === undefined);4041// dispose override command first42reg1 = CommandsRegistry.registerCommand('foo', command1);43reg2 = CommandsRegistry.registerCommand('foo', command2);44assert.ok(CommandsRegistry.getCommand('foo')!.handler === command2);4546reg1.dispose();47assert.ok(CommandsRegistry.getCommand('foo')!.handler === command2);4849reg2.dispose();50assert.ok(CommandsRegistry.getCommand('foo') === undefined);51});5253test('command with description', function () {5455const r1 = CommandsRegistry.registerCommand('test', function (accessor, args) {56assert.ok(typeof args === 'string');57});5859const r2 = CommandsRegistry.registerCommand('test2', function (accessor, args) {60assert.ok(typeof args === 'string');61});6263const r3 = CommandsRegistry.registerCommand({64id: 'test3',65handler: function (accessor, args) {66return true;67},68metadata: {69description: 'a command',70args: [{ name: 'value', constraint: Number }]71}72});7374CommandsRegistry.getCommands().get('test')!.handler.apply(undefined, [undefined!, 'string']);75CommandsRegistry.getCommands().get('test2')!.handler.apply(undefined, [undefined!, 'string']);76assert.throws(() => CommandsRegistry.getCommands().get('test3')!.handler.apply(undefined, [undefined!, 'string']));77assert.strictEqual(CommandsRegistry.getCommands().get('test3')!.handler.apply(undefined, [undefined!, 1]), true);7879combinedDisposable(r1, r2, r3).dispose();80});81});828384