Skip to content

Commit

Permalink
Merge pull request #3 from csprance/default-command-callback
Browse files Browse the repository at this point in the history
PR for #2 default command callback
  • Loading branch information
rohanchandra authored Oct 6, 2018
2 parents 0b527c0 + c996880 commit 89f9326
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/emulator/command-runner.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ const makeRunnerErrorOutput = (errorType) => {
* @param {Map} commandMapping command mapping from emulator state
* @param {string} commandName name of command to run
* @param {array} commandArgs commands to provide to the command function
* @param {function} notFoundCallback a default function to be run if no command is found
* @return {object} outputs and/or new state of the emulator
*/
export const run = (commandMapping, commandName, commandArgs) => {
export const run = (commandMapping, commandName, commandArgs, notFoundCallback = () => ({
output: makeRunnerErrorOutput(emulatorErrorType.COMMAND_NOT_FOUND)
})) => {
if (!CommandMappingUtil.isCommandSet(commandMapping, commandName)) {
return {
output: makeRunnerErrorOutput(emulatorErrorType.COMMAND_NOT_FOUND)
};
return notFoundCallback(...commandArgs);
}

const command = CommandMappingUtil.getCommandFn(commandMapping, commandName);
Expand Down
8 changes: 8 additions & 0 deletions test/emulator/command-runner.spec.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ describe('command-runner', () => {

chai.expect(output.content).to.include(emulatorErrorType.COMMAND_NOT_FOUND);
});

it('should run a notFoundCallback command if command not in mapping and notFoundCallback provided', () => {
const commandMapping = createCommandMapping({});
const notFoundCallback = ()=> true;

chai.expect(run(commandMapping, 'noSuchKey', [], notFoundCallback)).to.equal(true);
});

});
});

0 comments on commit 89f9326

Please sign in to comment.