public void XCommandSystemTest() { var execInfo = Utils.GetExecInfo("ic3"); var commandSystem = new XCommandSystem(); var group = commandSystem.RootCommand; group.AddCommand("one", new FunctionCommand(() => "ONE")); group.AddCommand("two", new FunctionCommand(() => "TWO")); group.AddCommand("echo", new FunctionCommand(s => s)); group.AddCommand("optional", new FunctionCommand(GetType().GetMethod(nameof(OptionalFunc), BindingFlags.NonPublic | BindingFlags.Static))); // Basic tests Assert.AreEqual("ONE", ((StringCommandResult)commandSystem.Execute(execInfo, new ICommand[] { new StringCommand("one") })).Content); Assert.AreEqual("ONE", commandSystem.ExecuteCommand(execInfo, "!one")); Assert.AreEqual("TWO", commandSystem.ExecuteCommand(execInfo, "!t")); Assert.AreEqual("TEST", commandSystem.ExecuteCommand(execInfo, "!e TEST")); Assert.AreEqual("ONE", commandSystem.ExecuteCommand(execInfo, "!o")); // Optional parameters Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!e")); Assert.AreEqual("NULL", commandSystem.ExecuteCommand(execInfo, "!op")); Assert.AreEqual("NOT NULL", commandSystem.ExecuteCommand(execInfo, "!op 1")); // Command chaining Assert.AreEqual("TEST", commandSystem.ExecuteCommand(execInfo, "!e (!e TEST)")); Assert.AreEqual("TWO", commandSystem.ExecuteCommand(execInfo, "!e (!t)")); Assert.AreEqual("NOT NULL", commandSystem.ExecuteCommand(execInfo, "!op (!e TEST)")); Assert.AreEqual("ONE", commandSystem.ExecuteCommand(execInfo, "!(!e on)")); // Command overloading var intCom = new Func <int, string>(_ => "INT"); var strCom = new Func <string, string>(_ => "STRING"); group.AddCommand("overlord", new OverloadedFunctionCommand(new[] { new FunctionCommand(intCom.Method, intCom.Target), new FunctionCommand(strCom.Method, strCom.Target) })); Assert.AreEqual("INT", commandSystem.ExecuteCommand(execInfo, "!overlord 1")); Assert.AreEqual("STRING", commandSystem.ExecuteCommand(execInfo, "!overlord a")); Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!overlord")); }
public void XCommandSystemTest() { var commandSystem = new XCommandSystem(); var group = commandSystem.RootCommand; group.AddCommand("one", new FunctionCommand(() => "ONE")); group.AddCommand("two", new FunctionCommand(() => "TWO")); group.AddCommand("echo", new FunctionCommand(s => s)); group.AddCommand("optional", new FunctionCommand(new Func <string, string>(s => s == null ? "NULL" : "NOT NULL")).SetRequiredParameters(0)); // Basic tests Assert.AreEqual("ONE", ((StringCommandResult)commandSystem.Execute(ExecutionInformation.Debug, new ICommand[] { new StringCommand("one") })).Content); Assert.AreEqual("ONE", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!one")); Assert.AreEqual("TWO", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!t")); Assert.AreEqual("TEST", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!e TEST")); Assert.AreEqual("ONE", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!o")); // Optional parameters Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!e")); Assert.AreEqual("NULL", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!op")); Assert.AreEqual("NOT NULL", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!op 1")); // Command chaining Assert.AreEqual("TEST", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!e (!e TEST)")); Assert.AreEqual("TWO", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!e (!t)")); Assert.AreEqual("NOT NULL", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!op (!e TEST)")); Assert.AreEqual("ONE", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!(!e on)")); // Command overloading var intCom = new Func <int, string>((int i) => "INT"); var strCom = new Func <string, string>((string s) => "STRING"); group.AddCommand("overlord", new OverloadedFunctionCommand(new[] { new FunctionCommand(intCom.Method, intCom.Target), new FunctionCommand(strCom.Method, strCom.Target) })); Assert.AreEqual("INT", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!overlord 1")); Assert.AreEqual("STRING", commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!overlord a")); Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(ExecutionInformation.Debug, "!overlord")); }