public BasicTestModule(CmdR cmdR) { _cmdR = cmdR; _cmdR.RegisterRoute("basic-module1", MethodOne, "Basic Echo Command"); _cmdR.RegisterRoute("basic-module2", MethodTwo, "Basic Echo Command"); }
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 ListAccounts(IDictionary <string, string> param, CmdR cmdR) { cmdR.Console.WriteLine("# Type Holder Balance"); cmdR.Console.WriteLine("------- -------------------- -------------------- -----------"); foreach (var account in _bank.Accounts) { cmdR.Console.WriteLine("{0} {1} {2} {3}", account.AccountNumber.ToString().PadLeft(7, '0'), account.GetType().Name.PadRight(20), account.AccountHolderName.PadRight(20), account.CurrentBalance().ToString("£0,0.00").PadLeft(11)); } cmdR.Console.WriteLine(""); foreach (var card in _bank.CreditCards) { cmdR.Console.WriteLine("{0} {1} {2} {3}", card.AccountNumber.ToString().PadLeft(7, '0'), card.GetType().Name.PadRight(20), card.AccountHolderName.PadRight(20), card.CurrentBalance().ToString("£0,0.00").PadLeft(11)); } cmdR.Console.WriteLine(""); }
public void AutoRegisterCommands_AttributeCommandAreRegistered() { var cmdR = new CmdR("> "); cmdR.AutoRegisterCommands(); Assert.AreEqual(true, cmdR.State.Routes.Any(x => x.Name == "attribute-module1")); Assert.AreEqual(true, cmdR.State.Routes.Any(x => x.Name == "attribute-module2")); }
public void AutoRegisterCommands_LoadsUpBothModulesAndAllCommandsAreRegistered() { var cmdR = new CmdR("> "); cmdR.AutoRegisterCommands(); // 1 built in route, 2 basic module commands, 2 attribute commands Assert.AreEqual(5, cmdR.State.Routes.Count); }
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_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); }
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_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 AutoRegisterCommands_CanInvokeBasicModuleRouteTwo() { var fakeConsole = new FakeCmdRConsole(""); var cmdR = new CmdR(console: fakeConsole); cmdR.AutoRegisterCommands(); cmdR.Run(new string[] { "basic-module2" }); Assert.AreEqual("module-test2", fakeConsole.ConsoleWindow[0]); }
public void AutoRegisterCommands_CanInvokeAttributeModuleRouteOne() { var fakeConsole = new FakeCmdRConsole(""); var cmdR = new CmdR(console: fakeConsole); cmdR.AutoRegisterCommands(); cmdR.Run(new string[] { "attribute-module1" }); Assert.AreEqual("attribute-test1", fakeConsole.ConsoleWindow[0]); }
public void OpenSavingsAccount(IDictionary <string, string> param, CmdR cmdR) { try { _bank.OpenSavingsAccount(param["name"], param["balance"]); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to add your new savings account, the following error was raised: " + ex.Message); } }
public void ApplyForCreditCard(IDictionary <string, string> param, CmdR cmdR) { try { _bank.ApplyCreditCard(param["name"], param["limit"]); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to add your new savings account, the following error was raised: " + ex.Message); } }
public void MoneyTransfer(IDictionary <string, string> param, CmdR cmdR) { try { _bank.TransferMoney(param["fromAccount"], param["toAccount"], param["amount"]); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to complete the Money Transfer, the following error was raised: " + ex.Message); } }
public void Deposit(IDictionary <string, string> param, CmdR cmdR) { try { _bank.Deposit(param["account"], param["amount"], param["description"]); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to deposit any money into your account, the following error was raised: " + ex.Message); } }
public void Withdraw(IDictionary <string, string> param, CmdR cmdR) { try { _bank.Withdraw(param["account"], param["amount"], param["description"]); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to withdraw money from your account, the following error was raised: " + ex.Message); } }
public void Save(IDictionary <string, string> param, CmdR cmdR) { try { _repository.Set(_bank); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to save your bank, we've experiencing a credit crunch, ask the government for a bailout: " + ex.Message); } }
static void Main(string[] args) { var cmdR = new CmdR("> ", new [] { "exit" }); cmdR.Console.WriteLine("Welcome to Midlands Bank Plc"); cmdR.Console.WriteLine("----------------------------"); cmdR.Console.WriteLine("type ? for help on available commands\n"); cmdR.AutoRegisterCommands(); cmdR.Run(args); }
public void MonthlyRun(IDictionary <string, string> param, CmdR cmdR) { try { _bank.MonthlyRun(); } catch (Exception ex) { cmdR.Console.WriteLine("Unable to add your new bank account, the following error was raised: " + ex.Message); } }
public void AutoRegisterCommands_CanInvokeAttributeModuleRouteTwo() { var fakeConsole = new FakeCmdRConsole(""); var cmdR = new CmdR(console: fakeConsole); cmdR.AutoRegisterCommands(); cmdR.Run(new string[] { "attribute-module2" }); Assert.AreEqual("attribute-test2", fakeConsole.ConsoleWindow[0]); }
public void AutoRegisterCommands_CanInvokeBasicModuleRouteOne() { var fakeConsole = new FakeCmdRConsole(""); var cmdR = new CmdR(console: fakeConsole); cmdR.AutoRegisterCommands(); cmdR.Run(new string[] { "basic-module1" }); Assert.AreEqual("module-test1", fakeConsole.ConsoleWindow[0]); }
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_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_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 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 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]); }
private void DrawTransactions(IEnumerable <Transaction> transactions, CmdR cmdR) { cmdR.Console.WriteLine("Date Description Amount Balance"); cmdR.Console.WriteLine("---------- -------------------------- ----------- -----------"); foreach (var transaction in transactions) { cmdR.Console.WriteLine("{0} {1} {2} {3}", transaction.Date.ToString("d"), transaction.Description.PadRight(26), transaction.Amount.ToString("c").PadLeft(11), transaction.Balance.ToString("c").PadLeft(11)); } cmdR.Console.WriteLine(""); }
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_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 CardTransactions(IDictionary <string, string> param, CmdR cmdR) { int accountNo; if (!int.TryParse(param["account"], out accountNo)) { throw new Exception("Unable to convert the account number into an Int"); } var card = _bank.CreditCards.FirstOrDefault(x => x.AccountNumber == accountNo); if (card == null) { throw new Exception("Invalid card account number"); } DrawTransactions(card.Transactions, cmdR); }
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); }
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 CmdBase GetCmd(string instruction, CmdInfo info, string label) { //specials switch (info.name) { case "sll": case "srl": case "sra": return(new FormatShift().GetCmd(instruction, info, label)); } //generic //add $t7,$t3,$s0 var split = instruction.Split(' '); var regSplit = split[1].Split(','); int rd = Register.Translate(regSplit[0]); int rs = Register.Translate(regSplit[1]); int rt = Register.Translate(regSplit[2]); CmdR cmd; switch (info.name) { case "add": case "sub": cmd = new CmdAddR(info, (byte)rs, (byte)rt, (byte)rd, 0, info.funct, label); break; case "slt": cmd = new CmdSlt(info, (byte)rs, (byte)rt, (byte)rd, 0, info.funct, label); break; default: cmd = new CmdR(info, (byte)rs, (byte)rt, (byte)rd, 0, info.funct, label); break; } return(cmd); }
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 StandardModule(CmdR cmdR) { _cmdR = cmdR; }
public AttributeTestModule(CmdR cmdR) { _cmdR = cmdR; }
public void MethodTwo(IDictionary<string, string> arg1, CmdR cmdR) { cmdR.Console.WriteLine("attribute-test2"); }
private void MethodTwo(IDictionary<string, string> arg1, CmdR cmdR) { cmdR.Console.WriteLine("module-test2"); }