Skip to content

Commit

Permalink
Merge pull request #398 from SockDrawer/feat-fix-this-#397
Browse files Browse the repository at this point in the history
Feat fix this #397
  • Loading branch information
AccaliaDeElementia authored Dec 15, 2016
2 parents 6a29d6e + 199a977 commit 23c064f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ language: node_js
node_js:
- 'node'
- '4'
- '6.2'
- '6.3'
- '6'
- '7'
branches:
except:
- /^(?i:v)\d+[.]\d+[.]\d+$/
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ environment:
- nodejs_version: ''
- nodejs_version: '6'
- nodejs_version: '4'
- nodejs_version: '7'
platform:
- x64
install:
Expand Down
14 changes: 8 additions & 6 deletions docs/api/lib/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ NodeBB provider module User class
* [.execute()](#sockbot.lib.module_commands..Commands+execute) ⇒ <code>Promise.&lt;Commands&gt;</code>
* _static_
* [.get(notification, postBody, handler)](#sockbot.lib.module_commands..Commands.get) ⇒ <code>Promise.&lt;Commands&gt;</code>
* [.add(command, helpText, handler)](#sockbot.lib.module_commands..Commands.add) ⇒ <code>Promise</code>
* [.addAlias(command, handler)](#sockbot.lib.module_commands..Commands.addAlias) ⇒ <code>Promise</code>
* [.add(command, helpText, handler, [context])](#sockbot.lib.module_commands..Commands.add) ⇒ <code>Promise</code>
* [.addAlias(command, handler, [context])](#sockbot.lib.module_commands..Commands.addAlias) ⇒ <code>Promise</code>
* [.addExtendedHelp(command, text)](#sockbot.lib.module_commands..Commands.addExtendedHelp) ⇒ <code>Promise</code>
* [.forbidCommand(command)](#sockbot.lib.module_commands..Commands.forbidCommand) ⇒ <code>boolean</code>

Expand Down Expand Up @@ -297,8 +297,8 @@ Commands class. Represents all commands for a Notification
* [.execute()](#sockbot.lib.module_commands..Commands+execute) ⇒ <code>Promise.&lt;Commands&gt;</code>
* _static_
* [.get(notification, postBody, handler)](#sockbot.lib.module_commands..Commands.get) ⇒ <code>Promise.&lt;Commands&gt;</code>
* [.add(command, helpText, handler)](#sockbot.lib.module_commands..Commands.add) ⇒ <code>Promise</code>
* [.addAlias(command, handler)](#sockbot.lib.module_commands..Commands.addAlias) ⇒ <code>Promise</code>
* [.add(command, helpText, handler, [context])](#sockbot.lib.module_commands..Commands.add) ⇒ <code>Promise</code>
* [.addAlias(command, handler, [context])](#sockbot.lib.module_commands..Commands.addAlias) ⇒ <code>Promise</code>
* [.addExtendedHelp(command, text)](#sockbot.lib.module_commands..Commands.addExtendedHelp) ⇒ <code>Promise</code>
* [.forbidCommand(command)](#sockbot.lib.module_commands..Commands.forbidCommand) ⇒ <code>boolean</code>

Expand Down Expand Up @@ -440,7 +440,7 @@ Get Commands from a notification

<a name="sockbot.lib.module_commands..Commands.add"></a>

#### Commands.add(command, helpText, handler) ⇒ <code>Promise</code>
#### Commands.add(command, helpText, handler, [context]) ⇒ <code>Promise</code>
Add a command to this forum instance

**Kind**: static method of <code>[Commands](#sockbot.lib.module_commands..Commands)</code>
Expand All @@ -452,10 +452,11 @@ Add a command to this forum instance
| command | <code>string</code> | Command to be added |
| helpText | <code>string</code> | Short help text for command |
| handler | <code>CommandHandler</code> | Function to handle the command |
| [context] | <code>object</code> | `this` context for the command. If not provided context will be a bare Object. |

<a name="sockbot.lib.module_commands..Commands.addAlias"></a>

#### Commands.addAlias(command, handler) ⇒ <code>Promise</code>
#### Commands.addAlias(command, handler, [context]) ⇒ <code>Promise</code>
Add a command alias to this forum instance

**Kind**: static method of <code>[Commands](#sockbot.lib.module_commands..Commands)</code>
Expand All @@ -466,6 +467,7 @@ Add a command alias to this forum instance
| --- | --- | --- |
| command | <code>string</code> | Command alias to be added |
| handler | <code>CommandHandler</code> | Function to handle the command |
| [context] | <code>object</code> | `this` context for the command. If not provided context will be a bare Object. |

<a name="sockbot.lib.module_commands..Commands.addExtendedHelp"></a>

Expand Down
16 changes: 12 additions & 4 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,10 @@ exports.bindCommands = function bindCommands(forum) {
* @param {string} command Command to be added
* @param {string} helpText Short help text for command
* @param {CommandHandler} handler Function to handle the command
* @param {object} [context] `this` context for the command. If not provided context will be a bare Object.
* @returns {Promise} Resolves when command has been added
*/
static add(command, helpText, handler) {
static add(command, helpText, handler, context) {
return new Promise((resolve, reject) => {
debug(`Registering command: ${command}`);
const cmd = command.toLowerCase(),
Expand All @@ -547,10 +548,13 @@ exports.bindCommands = function bindCommands(forum) {
if (!checkAvailable(forbiddenCmds, cmd, 'error', forbiddenMsg)) {
return reject(new Error('E_FORBIDDEN_COMMAND'));
}
if (context === undefined) {
context = new Object();
}
handlers[cmd] = {
commandText: command,
help: helpText,
handler: handler
handler: handler.bind(context)
};
return resolve(this);
});
Expand All @@ -564,9 +568,10 @@ exports.bindCommands = function bindCommands(forum) {
*
* @param {string} command Command alias to be added
* @param {CommandHandler} handler Function to handle the command
* @param {object} [context] `this` context for the command. If not provided context will be a bare Object.
* @returns {Promise} Resolves when command has been added
*/
static addAlias(command, handler) {
static addAlias(command, handler, context) {
return new Promise((resolve, reject) => {
debug(`Registering command alias: ${command}`);
const cmd = command.toLowerCase(),
Expand All @@ -580,7 +585,10 @@ exports.bindCommands = function bindCommands(forum) {
if (!checkAvailable(forbiddenCmds, cmd, 'error', forbiddenMsg)) {
return reject(new Error('E_FORBIDDEN_COMMAND'));
}
shadowHandlers[cmd] = handler;
if (context === undefined) {
context = new Object();
}
shadowHandlers[cmd] = handler.bind(context);
return resolve(this);
});
}
Expand Down
66 changes: 59 additions & 7 deletions test/lib/commandsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ describe('lib/config', () => {
spy.firstCall.args[0].should.include('help: print command help listing');
spy.firstCall.args[0].should.not.include('help: print command help listing *');
});

it('should indicate extended help without short help available', () => {
commands.internals.helpTopics.shutup = 'foobar';
const spy = sinon.spy();
Expand Down Expand Up @@ -1114,11 +1114,37 @@ describe('lib/config', () => {
handler = sinon.spy();
expect(commands.internals.handlers[cmd]).to.be.not.ok;
return commands.internals.Commands.add(cmd, text, handler).then(() => {
commands.internals.handlers[cmd].should.eql({
commandText: cmd,
handler: handler,
help: text
});
const obj = commands.internals.handlers[cmd];
obj.commandText.should.equal(cmd);
obj.handler.should.be.a('function');
obj.help.should.equal(text);
handler.callCount.should.equal(0);
obj.handler();
handler.callCount.should.equal(1);
});
});
it('should use default `this` when context not provided', () => {
const cmd = `a${Math.random()}a`,
text = `b${Math.random()}b`,
handler = sinon.spy();
expect(commands.internals.handlers[cmd]).to.be.not.ok;
return commands.internals.Commands.add(cmd, text, handler).then(() => {
commands.internals.handlers[cmd].handler();
handler.firstCall.thisValue.should.deep.equal({});
});
});
it('should use provided `this` context', () => {
const cmd = `a${Math.random()}a`,
text = `b${Math.random()}b`,
handler = sinon.spy(),
context = {
foo: Math.random(),
bar: Math.random()
};
expect(commands.internals.handlers[cmd]).to.be.not.ok;
return commands.internals.Commands.add(cmd, text, handler, context).then(() => {
commands.internals.handlers[cmd].handler();
handler.firstCall.thisValue.should.equal(context);
});
});
it('should normalize command case for matching', () => {
Expand Down Expand Up @@ -1180,7 +1206,33 @@ describe('lib/config', () => {
handler = sinon.spy();
expect(commands.internals.shadowHandlers[cmd]).to.be.not.ok;
return commands.internals.Commands.addAlias(cmd, handler).then(() => {
commands.internals.shadowHandlers[cmd].should.equal(handler);
const func = commands.internals.shadowHandlers[cmd];
func.should.be.a('function');
handler.callCount.should.equal(0);
func();
handler.callCount.should.equal(1);
});
});
it('should use default `this` when context not provided', () => {
const cmd = `a${Math.random()}a`,
handler = sinon.spy();
expect(commands.internals.shadowHandlers[cmd]).to.be.not.ok;
return commands.internals.Commands.addAlias(cmd, handler).then(() => {
commands.internals.shadowHandlers[cmd]();
handler.firstCall.thisValue.should.deep.equal({});
});
});
it('should use provided `this` context', () => {
const cmd = `a${Math.random()}a`,
handler = sinon.spy(),
context = {
foo: Math.random(),
bar: Math.random()
};
expect(commands.internals.shadowHandlers[cmd]).to.be.not.ok;
return commands.internals.Commands.addAlias(cmd, handler, context).then(() => {
commands.internals.shadowHandlers[cmd]();
handler.firstCall.thisValue.should.equal(context);
});
});
it('should normalize command case for matching', () => {
Expand Down

0 comments on commit 23c064f

Please sign in to comment.