Ejemplo n.º 1
0
        public AliasCommand(XCommandSystem root, string command)
        {
            var ast = CommandParser.ParseCommandRequest(command);

            aliasCommand = root.AstToCommandResult(ast);
            AliasString  = command;
        }
Ejemplo n.º 2
0
        public void TailStringTest()
        {
            var execInfo      = Utils.GetExecInfo("ic3");
            var commandSystem = new XCommandSystem();
            var group         = commandSystem.RootCommand;

            group.AddCommand("cmd", new FunctionCommand(s => s));
            string CallCommand(string command)
            {
                return(commandSystem.ExecuteCommand(execInfo, command));
            }

            Assert.AreEqual("a", CallCommand("!cmd a"));
            Assert.AreEqual("a b", CallCommand("!cmd a b"));
            Assert.AreEqual("a", CallCommand("!cmd a \" b"));
            Assert.AreEqual("a b 1", CallCommand("!cmd a b 1"));
        }
Ejemplo n.º 3
0
        public void XCommandSystemFilterTest()
        {
            var filterList = new Dictionary <string, object>
            {
                { "help", null },
                { "quit", null },
                { "play", null },
                { "ply", null }
            };

            // Exact match
            var result = XCommandSystem.FilterList(filterList, "help");

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("help", result.First().Key);

            // The first occurence of y
            result = XCommandSystem.FilterList(filterList, "y");
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("ply", result.First().Key);

            // The smallest word
            result = XCommandSystem.FilterList(filterList, "zorn");
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("ply", result.First().Key);

            // First letter match
            result = XCommandSystem.FilterList(filterList, "q");
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("quit", result.First().Key);

            // Ignore other letters
            result = XCommandSystem.FilterList(filterList, "palyndrom");
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("play", result.First().Key);

            filterList.Add("pla", null);

            // Ambiguous command
            result = XCommandSystem.FilterList(filterList, "p");
            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(r => r.Key == "ply"));
            Assert.IsTrue(result.Any(r => r.Key == "pla"));
        }
Ejemplo n.º 4
0
        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"));
        }
Ejemplo n.º 5
0
        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"));
        }
Ejemplo n.º 6
0
        public void XCommandSystemTest2()
        {
            var execInfo      = Utils.GetExecInfo("exact");
            var commandSystem = new XCommandSystem();
            var group         = commandSystem.RootCommand;

            var o1 = new OverloadedFunctionCommand();

            o1.AddCommand(new FunctionCommand(new Action <int>((_) => { })));
            o1.AddCommand(new FunctionCommand(new Action <long>((_) => { })));
            group.AddCommand("one", o1);

            group.AddCommand("two", new FunctionCommand(new Action <StringSplitOptions>((_) => { })));

            var o2 = new CommandGroup();

            o2.AddCommand("a", new FunctionCommand(new Action(() => { })));
            o2.AddCommand("b", new FunctionCommand(new Action(() => { })));
            group.AddCommand("three", o2);

            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!one"));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!one \"\""));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!one (!print \"\")"));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!one string"));
            Assert.DoesNotThrow(() => commandSystem.ExecuteCommand(execInfo, "!one 42"));
            Assert.DoesNotThrow(() => commandSystem.ExecuteCommand(execInfo, "!one 4200000000000"));

            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!two"));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!two \"\""));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!two (!print \"\")"));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!two 42"));
            Assert.DoesNotThrow(() => commandSystem.ExecuteCommand(execInfo, "!two None"));

            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!three"));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!three \"\""));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!three (!print \"\")"));
            Assert.Throws <CommandException>(() => commandSystem.ExecuteCommand(execInfo, "!three c"));
            Assert.DoesNotThrow(() => commandSystem.ExecuteCommand(execInfo, "!three a"));
            Assert.DoesNotThrow(() => commandSystem.ExecuteCommand(execInfo, "!three b"));
        }
Ejemplo n.º 7
0
        public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes)
        {
            string result;

            if (arguments.Count == 0)
            {
                if (returnTypes.Contains(CommandResultType.Command))
                {
                    return(new CommandCommandResult(this));
                }
                result = string.Empty;
            }
            else
            {
                var comResult = arguments[0].Execute(info, Array.Empty <ICommand>(), XCommandSystem.ReturnString);
                result = ((StringCommandResult)comResult).Content;
            }

            var commandResults = XCommandSystem.FilterList(commands, result).ToArray();

            if (commandResults.Length > 1)
            {
                throw new CommandException("Ambiguous call, possible subcommands: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall);
            }
            if (commandResults.Length == 0)
            {
                throw new CommandException("No matching command", CommandExceptionReason.AmbiguousCall);
            }
            if (commandResults.Length == 1 && result == string.Empty && commandResults[0].Key != string.Empty)
            {
                throw new CommandException("Ambiguous call, possible subcommands: " + string.Join(", ", commands.Keys.Take(4)) + ", ...", CommandExceptionReason.AmbiguousCall);
            }


            var argSubList = arguments.TrySegment(1);

            return(commandResults[0].Value.Execute(info, argSubList, returnTypes));
        }
Ejemplo n.º 8
0
 public CommandManager()
 {
     CommandSystem = new XCommandSystem();
     PluginCommands = new Dictionary<Plugin, IList<BotCommand>>();
     Util.Init(ref CommandPaths);
 }