From fc220d0363a5b447fc5adfe1d18adaa0e27018fd Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Fri, 11 Nov 2011 12:26:46 +0100 Subject: [PATCH] more command manager tests --- lib/ace/commands/command_manager.js | 4 +-- lib/ace/commands/command_manager_test.js | 31 +++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/ace/commands/command_manager.js b/lib/ace/commands/command_manager.js index 24a93368..2cae56f3 100644 --- a/lib/ace/commands/command_manager.js +++ b/lib/ace/commands/command_manager.js @@ -19,7 +19,7 @@ var CommandManager = function(platform, commands) { this.addCommand = function(command) { if (this.commands[command.name]) this.removeCommand(command); - + this.commands[command.name] = command; if (command.bindKey) { @@ -32,7 +32,7 @@ var CommandManager = function(platform, commands) { command = this.commands[name]; delete this.commands[name]; - // exaustive search is a little bit brute force but since removeCommand is + // exaustive search is brute force but since removeCommand is // not a performance critical operation this should be OK var ckb = this.commmandKeyBinding; for (var hashId in ckb) { diff --git a/lib/ace/commands/command_manager_test.js b/lib/ace/commands/command_manager_test.js index c5541747..7898eee1 100644 --- a/lib/ace/commands/command_manager_test.js +++ b/lib/ace/commands/command_manager_test.js @@ -83,7 +83,17 @@ module.exports = { var command = cm.findKeyCommand(keys.KEY_MODS.ctrl, "L"); assert.equal(command, this.command); }, - + + "test: remove command by object": function() { + this.cm.removeCommand(this.command); + + this.cm.exec("gotoline"); + assert.ok(!this.command.called); + + var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L"); + assert.equal(command, null); + }, + "test: remove command by name": function() { this.cm.removeCommand("gotoline"); @@ -92,6 +102,25 @@ module.exports = { var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L"); assert.equal(command, null); + }, + + "test: adding a new command with the same name as an existing one should remove the old one first": function() { + var command = { + name: "gotoline", + bindKey: { + mac: "Command-L", + win: "Ctrl-L" + }, + called: false, + exec: function(editor) { this.called = true; } + } + this.cm.addCommand(command); + + this.cm.exec("gotoline"); + assert.ok(command.called); + assert.ok(!this.command.called); + + assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "L"), command); } };