public BasicTestModule(CmdR cmdR) { _cmdR = cmdR; _cmdR.RegisterRoute("basic-module1", MethodOne, "Basic Echo Command"); _cmdR.RegisterRoute("basic-module2", MethodTwo, "Basic Echo Command"); }
public void Run_SplitsInitialCommandsOnTheCommandSeparator() { var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.RegisterRoute("first", (p, con, s) => con.Write("First Command")); cmdR.RegisterRoute("second", (p, con, s) => con.Write("Second Command")); cmdR.Run(new string[] { "first", CmdRExtensions.COMMAND_SEPARATOR, "second" }); Assert.AreEqual("First Command", console.ConsoleWindow[0]); Assert.AreEqual("Second Command", console.ConsoleWindow[1]); }
public void ExecuteCommand_AllParamsArePassedCorreclty_MissingOptionalParam() { var cmdR = new CmdR("", new string[0]); var validParams = 0; cmdR.RegisterRoute("ls path filter? param3?", (p, c, s) => { if (p["path"] == "c:\\program files\\msbob\\") { validParams += 1; } if (p["filter"] == "*.exe") { validParams += 1; } if (!p.ContainsKey("param3")) { validParams += 1; } }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\" *.exe"); Assert.AreEqual(3, validParams); }
public void Run_Help() { //todo: use moq to fake the console & state var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.AutoRegisterCommands(); cmdR.RegisterRoute("cd", (p, con, state) => { state.CmdPrompt = "new prompt> "; }); cmdR.Run(new[] { "?" }); Assert.AreEqual("basic-module1 ", console.ConsoleWindow[0]); Assert.AreEqual("basic-module2 ", console.ConsoleWindow[1]); Assert.AreEqual("attribute-module1 ", console.ConsoleWindow[2]); Assert.AreEqual("attribute-module2 ", console.ConsoleWindow[3]); Assert.AreEqual("? ", console.ConsoleWindow[4]); Assert.AreEqual("cd ", console.ConsoleWindow[5]); Assert.AreEqual("", console.ConsoleWindow[6]); Assert.AreEqual("> ", console.ConsoleWindow[7]); Assert.AreEqual("> ", console.ConsoleWindow[8]); }
public void Run_DoubleEscapedCommandSeparatorNotUsedInCommand() { var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.RegisterRoute("first &", (p, con, s) => con.Write("First Command")); cmdR.RegisterRoute("second", (p, con, s) => con.Write("Second Command")); var doubleEscapedCommandSeparator = CmdRExtensions.ESCAPE_CHAR + CmdRExtensions.ESCAPE_CHAR + CmdRExtensions.COMMAND_SEPARATOR; cmdR.Run(new[] { "first", doubleEscapedCommandSeparator, CmdRExtensions.COMMAND_SEPARATOR, "second" }); Assert.AreEqual("First Command", console.ConsoleWindow[0]); Assert.AreEqual("Second Command", console.ConsoleWindow[1]); }
public void ExecuteCommand_CanExecuteCommandWithOptionalParam_Specified() { var cmdR = new CmdR("", new string[0]); var closureI = 1; cmdR.RegisterRoute("ls path filter?", (p, c, s) => { closureI += 1; }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\" *.exe"); Assert.AreEqual(2, closureI); }
public void ExecuteCommand_AllParamsArePassedCorreclty_Count() { var cmdR = new CmdR("", new string[0]); var paramCount = 0; cmdR.RegisterRoute("ls path filter?", (p, c, s) => { paramCount = p.Count; }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\" *.exe"); Assert.AreEqual(2, paramCount); }
public void ExecuteCommand_CanExecuteCommandWithEscappedParam() { var cmdR = new CmdR("", new string[0]); var closureI = 1; cmdR.RegisterRoute("ls path", (p, c, s) => { closureI += 1; }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\""); Assert.AreEqual(2, closureI); }
public void ExecuteCommand_CanExecuteSimpleCommand() { var cmdR = new CmdR("", new string[0]); var closureI = 1; cmdR.RegisterRoute("ls", (p, c, s) => { closureI += 1; }); cmdR.ExecuteCommand("ls"); Assert.AreEqual(2, closureI); }
static void Main(string[] args) { // the class which contains all our logic var example = new DOSPromptReplication(); // creating the CmdR class passing, specifying the command prompt (> ) to use and a list of exit codes (exit) the user can type to exit the cmdR loop // these are the system defaults, so they dont actually need to be passed in var cmdR = new CmdR("> ", new string[] { "exit" }); // setting up the command routes cmdR.RegisterRoute("cd path", example.ChangeDirectory, "change current directory"); cmdR.RegisterRoute("ls filter?", example.ListDirectory, "list the contents of the current directory, optionaly specify a filter to search for contents within the directory"); cmdR.RegisterRoute("del file", example.DeleteFile, "delete the sepcified file"); cmdR.RegisterRoute("cls", example.Clear, "Clears the screen."); // registering a route with a lambda cmdR.RegisterRoute("echo text", (parameters, console, state) => console.WriteLine(parameters["text"]), "print the specified text to the console"); // start the cmdR loop passing in the args as the first command to execute cmdR.Run(args); }
public void Run_PrintsOutTheCommandPromptAfterRunningACommand() { //todo: use moq to fake the console & state var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.RegisterRoute("cd", (p, con, s) => con.Write("hello!")); cmdR.Run(new string[] { "cd" }); Assert.AreEqual("> ", console.ConsoleWindow[1]); }
public void Run_ExecutesACommandPassedInOnTheCommandLine() { //todo: use moq to fake the console & state var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.RegisterRoute("cd", (p, con, s) => con.Write("hello!")); cmdR.Run(new string[] { "cd" }); Assert.AreEqual("hello!", console.ConsoleWindow[0]); }
public void Run_WeCanModifyTheCommandPromptAndItsWrittenToTheConsole() { //todo: use moq to fake the console & state var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.RegisterRoute("cd", (p, con, state) => { state.CmdPrompt = "new prompt> "; }); cmdR.Run(new string[] { "cd" }); Assert.AreEqual("new prompt> ", console.ConsoleWindow[0]); }
public void ExecuteCommand_AllParamsArePassedCorreclty_Values() { var cmdR = new CmdR("", new string[0]); var validParams = 0; cmdR.RegisterRoute("ls path filter?", (p, c, s) => { if (p["path"] == "c:\\program files\\msbob\\") validParams += 1; if (p["filter"] == "*.exe") validParams += 1; }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\" *.exe"); Assert.AreEqual(2, validParams); }
public void Run_HelpWithDetails() { //todo: use moq to fake the console & state var console = new FakeCmdRConsole(""); var cmdR = new CmdR(new OrderedCommandParser(), new Routing(), new RouteParser(), console, new CmdRState()); cmdR.AutoRegisterCommands(); cmdR.RegisterRoute("cd", (p, con, state) => { state.CmdPrompt = "new prompt> "; }, "changes directory"); cmdR.Run(new[] { "? cd" }); Assert.AreEqual(" cd", console.ConsoleWindow[0]); Assert.AreEqual(" changes directory", console.ConsoleWindow[1]); }
public void ExecuteCommand_AllParamsArePassedCorreclty_MissingOptionalParam() { var cmdR = new CmdR("", new string[0]); var validParams = 0; cmdR.RegisterRoute("ls path filter? param3?", (p, c, s) => { if (p["path"] == "c:\\program files\\msbob\\") validParams += 1; if (p["filter"] == "*.exe") validParams += 1; if (!p.ContainsKey("param3")) validParams += 1; }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\" *.exe"); Assert.AreEqual(3, validParams); }
public void ExecuteCommand_AllParamsArePassedCorreclty_Values() { var cmdR = new CmdR("", new string[0]); var validParams = 0; cmdR.RegisterRoute("ls path filter?", (p, c, s) => { if (p["path"] == "c:\\program files\\msbob\\") { validParams += 1; } if (p["filter"] == "*.exe") { validParams += 1; } }); cmdR.ExecuteCommand("ls \"c:\\program files\\msbob\\\" *.exe"); Assert.AreEqual(2, validParams); }