sirrobert-shell-cmd
Version:
A base class for nodejs-shell commands.
453 lines (380 loc) • 15.2 kB
JavaScript
"use strict";
const Test = require("sirrobert-test-vows");
const Command = require("src/Command.js");
const Context = require("nodejs-shell-cmd-context");
new Test(
"the Commands base class",
{ when: "creating commands",
"creating with one string name": () => {
var cmd = new Command({
names: "open",
handler: function () { return "handler output" },
});
Test.is.equal(cmd.names.length, 1);
Test.is.equal(cmd.names[0], "open");
},
"creating with one array name": () => {
var cmd = new Command({
names: ["open"],
handler: function () { return "handler output" },
});
Test.is.equal(cmd.names.length, 1);
Test.is.equal(cmd.names[0], "open");
},
"creating with more than one string name": () => {
var cmd = new Command({
names: ["open", "close"],
handler: function () { return "handler output" },
});
Test.is.equal(cmd.names.length, 2);
Test.is.equal(cmd.names[0], "open");
Test.is.equal(cmd.names[1], "close");
},
"testing the handler function directly": () => {
var cmd = new Command({
names: "open",
handler: function () { return "handler output" },
});
Test.is.equal(cmd.handler(), "handler output");
},
"interpret() a help (style 1: help ...)": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
handler : function () { return "handler output" },
help : [{
token: "cmd.open",
file: "open.help",
aliases: ["open"]
}]
});
cmd.interpret("help open", ctx);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.open");
Test.is.equal(ctx.requestedCommands[0].params.section, undefined);
},
"interpret() a help (style 2: ? open)": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
handler : function () { return "handler output" },
help : [{
token: "cmd.open",
file: "open.help",
aliases: ["open"]
}]
});
cmd.interpret("? open", ctx);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.open");
},
"interpret() a help (style 3: open --help)": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
handler : function () { return "handler output" },
help : [{
token: "cmd.open",
file: "open.help",
aliases: ["open"]
}]
});
cmd.interpret("open --help", ctx);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.open");
},
"interpret() a help (style 4: open -h)": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
handler : function () { return "handler output" },
help : [{
token: "cmd.open",
file: "open.help",
aliases: ["open"]
}]
});
cmd.interpret("open -h", ctx);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.open");
},
"interpret() a help (style 5: open -?)": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
handler : function () { return "handler output" },
help : [{
token: "cmd.open",
file: "open.help",
aliases: ["open"]
}]
});
cmd.interpret("open -?", ctx);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.open");
},
"interpret() a help (style 4: open -h)": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
handler : function () { return "handler output" },
help : [{
token: "cmd.open",
file: "open.help",
aliases: ["open"]
}]
});
cmd.interpret("open -h", ctx);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.open");
},
"interpret() a command with two params": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.equal(cmd.interpret("open", ctx), "handler output");
},
"interpret() a command with three params": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: (a,b,c) => [a,b,c]});
Test.is.equal(cmd.interpret("open", "potat", ctx)[0], "open");
Test.is.equal(cmd.interpret("open", "potat", ctx)[1], "potat");
Test.is.equal(cmd.interpret("open", "potat", ctx)[2], ctx);
},
"activate() and deactivate()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.ok( cmd.active, "command is active by default.")
cmd.deactivate();
Test.is.ok(!cmd.active, "command is deactivated by .deactivate()")
cmd.activate();
Test.is.ok( cmd.active, "command is activated by .activate()")
},
"deactivate() on construction": () => {
var ctx = new Context();
var cmd = new Command({
names : "open",
active : false,
handler : () => "handler output",
});
Test.is.ok(!cmd.active, "command is inactive on construction.")
cmd.activate();
Test.is.ok( cmd.active, "command is activated by .activate()")
cmd.deactivate();
Test.is.ok(!cmd.active, "command is deactivated by .deactivate()")
},
//---------------------------
"setHandler()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.equal(cmd.interpret("",ctx), "handler output");
cmd.setHandler(() => "handler revised");
Test.is.equal(cmd.interpret("",ctx), "handler revised");
},
"setExpandStr": () => {
var ctx = new Context();
var cmd = new Command({
names: "open",
handler: () => "handler output",
expandStr: () => "expander output",
});
Test.is.equal(cmd.expandStr(), "expander output");
cmd.setExpandStr(() => "expander revised");
Test.is.equal(cmd.expandStr(), "expander revised");
},
"expandStr()": () => {
var ctx = new Context();
var cmd = new Command({
names: "open",
handler: () => "handler output",
expandStr: () => "expander output",
});
Test.is.equal(cmd.expandStr(), "expander output");
},
"addName()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
cmd.addName("close");
Test.is.equal(cmd.names.length, 2);
Test.is.equal(cmd.names[0], "open");
Test.is.equal(cmd.names[1], "close");
},
"addNames()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.equal(cmd.names.length, 1);
Test.is.equal(cmd.names[0], "open");
cmd.addNames("close", ["white", "yellow"], ["green"]);
Test.is.equal(cmd.names.length, 5);
Test.is.equal(cmd.names[0], "open");
Test.is.equal(cmd.names[1], "close");
Test.is.equal(cmd.names[2], "white");
Test.is.equal(cmd.names[3], "yellow");
Test.is.equal(cmd.names[4], "green");
},
"addSubcommand()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
cmd.addSubcommand({names: "white", handler: () => "subcommand output"});
Test.is.equal(cmd.subcommands.length, 1);
Test.is.equal(cmd.subcommands[0].names[0], "white");
cmd.addSubcommand(
new Command({names: "yellow", handler: () => "yellow output"})
);
Test.is.equal(cmd.subcommands.length, 2);
Test.is.equal(cmd.subcommands[1].names[0], "yellow");
},
"addSubcommands()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
cmd.addSubcommands(
{names: "white", handler: () => "subcommand output"},
[
{names: "yellow", handler: () => "subcommand output"},
{names: "blue", handler: () => "subcommand output"},
],
[
new Command({names: "green", handler: () => "subcommand output"}),
{names: "red", handler: () => "subcommand output"},
],
new Command({names: "purple", handler: () => "subcommand output"})
)
Test.is.equal(cmd.subcommands.length, 6);
Test.is.equal(cmd.subcommands[0].names[0], "white");
Test.is.equal(cmd.subcommands[1].names[0], "yellow");
Test.is.equal(cmd.subcommands[2].names[0], "blue");
Test.is.equal(cmd.subcommands[3].names[0], "green");
Test.is.equal(cmd.subcommands[4].names[0], "red");
Test.is.equal(cmd.subcommands[5].names[0], "purple");
},
"findSubcommand()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
cmd.addSubcommands(
{names: "white", handler: () => "subcommand output"},
[
{names: "yellow", handler: () => "subcommand output"},
{names: "blue", handler: () => "subcommand output"},
],
[
new Command({names: "green", handler: () => "subcommand output"}),
{names: "red", handler: () => "subcommand output"},
],
new Command({names: "purple", handler: () => "subcommand output"})
)
Test.is.equal(cmd.findSubcommand(["blue"]).names[0], "blue");
},
"addHelp()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.equal(cmd.help.length, 0);
cmd.addHelp([{token: "cmd.open", file: "open.help", aliases: ["open"]}]);
Test.is.equal(cmd.help.length, 1);
Test.is.equal(cmd.help[0].token, "cmd.open");
},
"addHelps()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.equal(cmd.help.length, 0);
cmd.addHelps([{token: "cmd.open", file: "open.help", aliases: ["open"]}]);
Test.is.equal(cmd.help.length, 1);
Test.is.equal(cmd.help[0].token, "cmd.open");
cmd.addHelps(
{token: "cmd.swing", file: "swing.help", aliases: ["swing"]},
[
{token: "cmd.close" , file: "close.help" , aliases: ["close"]} ,
{token: "cmd.shut" , file: "shut.help" , aliases: ["shut"]} ,
],
{token: "cmd.green", file: "green.help", aliases: ["green"]}
);
Test.is.equal(cmd.help.length, 5);
Test.is.equal(cmd.help[0].token, "cmd.open");
Test.is.equal(cmd.help[1].token, "cmd.swing");
Test.is.equal(cmd.help[2].token, "cmd.close");
Test.is.equal(cmd.help[3].token, "cmd.shut");
Test.is.equal(cmd.help[4].token, "cmd.green");
},
"isHelp()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
Test.is.ok(cmd.isHelp("help potato" ) , "help ..." );
Test.is.ok(cmd.isHelp("? potato" ) , "? ..." );
Test.is.ok(cmd.isHelp("potato --help" ) , "... --help" );
Test.is.ok(cmd.isHelp("potato -h" ) , "... -h" );
Test.is.ok(cmd.isHelp("potato -?" ) , "... -?" );
Test.is.ok(!cmd.isHelp("helps potato" ) , "helps ..." );
Test.is.ok(!cmd.isHelp("?s potato" ) , "?s ..." );
Test.is.ok(!cmd.isHelp("potato --helps" ) , "... --helps" );
Test.is.ok(!cmd.isHelp("potato -hs" ) , "... -hs" );
Test.is.ok(!cmd.isHelp("potato -?s" ) , "... -?s" );
},
"getHelpToken()": () => {
var ctx = new Context();
var cmd = new Command({names: "open", handler: () => "handler output"});
cmd.addHelp([
{token: "cmd.open", file: "open.help", aliases: ["open"]},
{token: "cmd.close", file: "open.help", aliases: ["close", "shut"]}
]);
Test.is.equal(cmd.getHelpToken("open"), "cmd.open");
Test.is.equal(cmd.getHelpToken("close"), "cmd.close");
Test.is.equal(cmd.getHelpToken("shut"), "cmd.close");
Test.is.strictEqual(cmd.getHelpToken("foo"), null);
},
"getHelp()": () => {
var ctx = new Context();
var cmd = new Command({
names: "open",
handler: () => "handler output",
help: [
{token: "cmd.swing" , file: "swing.help" , aliases: ["swing"]} ,
{token: "cmd.close" , file: "close.help" , aliases: ["close"]} ,
{token: "cmd.shut" , file: "shut.help" , aliases: ["shut" ]} ,
{token: "cmd.green" , file: "green.help" , aliases: ["green"]}
]
});
Test.is.equal(cmd.help.length, 4);
Test.is.equal(cmd.help[0].token, "cmd.swing");
Test.is.equal(cmd.help[1].token, "cmd.close");
Test.is.equal(cmd.help[2].token, "cmd.shut");
Test.is.equal(cmd.help[3].token, "cmd.green");
Test.is.equal(cmd.getHelp("cmd.swing").file, "swing.help");
Test.is.equal(cmd.getHelp("cmd.green").file, "green.help");
},
"requestHelp()": () => {
var ctx = new Context();
var cmd = new Command({
names: "open",
handler: () => "handler output",
help: [
{token: "cmd.swing" , file: "swing.help" , aliases: ["swing"]} ,
{ token : "cmd.close",
file : "close.help",
section : "sec",
aliases : ["close","shut"]},
{token: "cmd.green" , file: "green.help" , aliases: ["green"]}
]
});
Test.is.equal(ctx.requestedCommands.length, 0);
cmd.requestHelp("green", ctx);
cmd.requestHelp("shut", "sec", ctx);
Test.is.equal(ctx.requestedCommands.length, 2);
Test.is.equal(ctx.requestedCommands[0].name, "help");
Test.is.equal(ctx.requestedCommands[0].params.token, "cmd.green");
Test.is.equal(ctx.requestedCommands[0].params.section, undefined);
Test.is.equal(ctx.requestedCommands[1].name, "help");
Test.is.equal(ctx.requestedCommands[1].params.token, "cmd.close");
Test.is.equal(ctx.requestedCommands[1].params.section, "sec");
},
"registering commands": () => {
var ctx = new Context();
var cmd = new Command({
names: "open",
handler: () => "handler output",
});
var str = "before";
Command.onRegister(function (cmd) {str = cmd.names[0]});
Test.is.equal(str, "before");
Command.register(cmd);
Test.is.equal(str, "open");
},
}
).run(module);