Exemple #1
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"));
        }
Exemple #2
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));
        }