Bug 1179866 - Allow numbers in commands. r=aleth
authorMartin Giger <martin@humanoids.be>
Thu, 01 Oct 2015 23:22:40 +0200
changeset 18476 34aa79c759051f4a26485e10364d04d1577e40c2
parent 18475 f1c3d32341ddb64e95b1a5b8e285f97497dfa652
child 18477 dd8c1bf3c8aa091094a7afcd3e6f4e02118ab56f
push id11308
push useraleth@instantbird.org
push dateFri, 02 Oct 2015 22:40:38 +0000
treeherdercomm-central@258f0a389e63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaleth
bugs1179866
Bug 1179866 - Allow numbers in commands. r=aleth
chat/components/src/imCommands.js
chat/components/src/test/test_commands.js
--- a/chat/components/src/imCommands.js
+++ b/chat/components/src/imCommands.js
@@ -225,17 +225,17 @@ CommandsService.prototype = {
     return cmdArray.sort((a, b) => b.priority - a.priority);
   },
   executeCommand: function(aMessage, aConversation, aReturnedConv) {
     if (!aMessage)
       throw Cr.NS_ERROR_INVALID_ARG;
 
     let matchResult;
     if (aMessage[0] != "/" ||
-        !(matchResult = /^\/([a-z]+)(?: |$)([\s\S]*)/.exec(aMessage)))
+        !(matchResult = /^\/([a-z0-9]+)(?: |$)([\s\S]*)/.exec(aMessage)))
       return false;
 
     let [, name, args] = matchResult;
 
     let cmdArray = this._findCommands(aConversation, name);
     if (!cmdArray.length)
       return false;
 
--- a/chat/components/src/test/test_commands.js
+++ b/chat/components/src/test/test_commands.js
@@ -21,16 +21,21 @@ const fakeDisconnectedAccount = {
   connected: false,
   protocol: {id: kPrplId}
 };
 const fakeAccount2 = {
   connected: true,
   protocol: {id: kPrplId2}
 };
 
+const fakeConversation = {
+  account: fakeAccount,
+  isChat: true
+};
+
 function fakeCommand(aName, aUsageContext) {
   this.name = aName;
   if (aUsageContext)
     this.usageContext = aUsageContext;
 }
 fakeCommand.prototype = {
   get helpString() { return ""; },
   usageContext: Ci.imICommand.CMD_CONTEXT_ALL,
@@ -52,16 +57,19 @@ function run_test() {
                           kPrplId);
 
   // Name clashes with global command.
   cmdserv.registerCommand(new fakeCommand("offline"), kPrplId);
 
   // Name starts with another command name.
   cmdserv.registerCommand(new fakeCommand("helpme"), kPrplId);
 
+  // Command name contains numbers.
+  cmdserv.registerCommand(new fakeCommand("r9kbeta"), kPrplId);
+
   // Array of (possibly partial) command names as entered by the user.
   let testCmds = ["x", "b", "ba", "bal", "back", "hel", "help", "off", "offline"];
 
   // We test an array of different possible conversations.
   // cmdlist lists all the available commands for the given conversation.
   // results is an array which for each testCmd provides an array containing
   // data with which the return value of _findCommands can be checked. In
   // particular, the name of the command and whether the first (i.e. preferred)
@@ -74,34 +82,34 @@ function run_test() {
       cmdlist: "away, back, busy, dnd, help, offline, raw, say",
       results: [[], [], ["back"], [], ["back"], ["help"], ["help"], ["offline"], ["offline"]]
     },
     {
       desc: "Disconnected conversation with fakeAccount.",
       conv: {
         account: fakeDisconnectedAccount
       },
-      cmdlist: "away, back, busy, dnd, help, helpme, offline, offline, raw, say",
+      cmdlist: "away, back, busy, dnd, help, helpme, offline, offline, r9kbeta, raw, say",
       results: [[], [], ["back"], [], ["back"], ["help"], ["help"], ["offline"], ["offline"]]
     },
     {
       desc: "Conversation with fakeAccount.",
       conv: {
         account: fakeAccount
       },
-      cmdlist: "away, back, busy, dnd, help, helpme, offline, offline, raw, say",
+      cmdlist: "away, back, busy, dnd, help, helpme, offline, offline, r9kbeta, raw, say",
       results: [[], [], ["back"], [], ["back"], [], ["help"], ["offline"], ["offline"]]
     },
     {
       desc: "MUC with fakeAccount.",
       conv: {
         account: fakeAccount,
         isChat: true
       },
-      cmdlist: "away, back, balderdash, busy, dnd, help, helpme, offline, offline, raw, say",
+      cmdlist: "away, back, balderdash, busy, dnd, help, helpme, offline, offline, r9kbeta, raw, say",
       results: [[], [], [], ["balderdash", true], ["back"], [], ["help"], ["offline"], ["offline"]]
     },
     {
       desc: "Conversation with fakeAccount2.",
       conv: {
         account: fakeAccount2
       },
       cmdlist: "away, back, baloney, banana, busy, dnd, help, offline, raw, say",
@@ -136,10 +144,40 @@ function run_test() {
         // Check if the right command was returned.
         do_check_eq(cmdArray[0].name, expectedResult[0]);
         do_check_eq(cmdArray[0].priority == Ci.imICommand.CMD_PRIORITY_PRPL,
                     !!expectedResult[1]);
       }
     }
   }
 
+  // Array of messages to test command execution of.
+  let testMessages = [
+    {
+      message: "/r9kbeta",
+      result: true,
+    },
+    {
+      message: "/helpme 2 arguments",
+      result: true
+    },
+    {
+      message: "nocommand",
+      result: false
+    },
+    {
+      message: "/-a",
+      result: false
+    },
+    {
+      message: "/notregistered",
+      result: false
+    }
+  ];
+
+  // Test command execution.
+  for (let executionTest of testMessages) {
+    do_print("Testing command execution for '" + executionTest.message + "'");
+    do_check_eq(cmdserv.executeCommand(executionTest.message, fakeConversation), executionTest.result);
+  }
+
   cmdserv.unInitCommands();
 }