Ejemplo n.º 1
0
        [InlineData("-o2 1", false)]      //partial match
        public void TestTokenIsFullMatch(string input, bool expIsFullMatch)
        {
            ArgumentToken arg1 = new ArgumentToken <int> .Builder().Name("arg1").IsOptional(false).Parser(int.TryParse).Build();

            ArgumentToken arg2 = new ArgumentToken <double> .Builder().Name("arg2").IsOptional(false).Parser(double.TryParse).Build();

            var tokens = new ICommandToken[]
            {
                new OptionWithArgumentToken.Builder().Name("-o2", "--option2").WithArgument(arg1).WithArgument(arg2).Build()
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, input);

            Assert.Same(usage.Tokens, matchCollection.MatchableTokens);
            Assert.NotEmpty(matchCollection.Matches);

            Assert.Equal(1, matchCollection.Matches.Count());
            ParserTokenMatch match = matchCollection.Matches.First();

            Assert.Equal(expIsFullMatch, match.IsFullMatch);
        }
Ejemplo n.º 2
0
        public void TestMatchArgValuesWithOptionalTokens()
        {
            ArgumentToken arg1 = new ArgumentToken <int> .Builder().Name("arg1").IsOptional(false).Parser(int.TryParse).Build();

            ArgumentToken arg2 = new ArgumentToken <double> .Builder().Name("arg2").IsOptional(false).Parser(double.TryParse).Build();

            var tokens = new ICommandToken[]
            {
                new VerbToken(new Name("verb0")),
                new OptionWithArgumentToken.Builder().Name("-o2", "--option2").WithArgument(arg1).WithArgument(arg2).Build(),
            };

            var builder = new CommandUsage.Builder().Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, "verb0 -o2 1 2.34");

            int  arg1Value;
            bool arg1Exists = matchCollection.TryGetArgValue(arg1, out arg1Value);

            double arg2Value;
            bool   arg2Exists = matchCollection.TryGetArgValue(arg2, out arg2Value);

            Assert.True(arg1Exists);
            Assert.True(arg2Exists);
            Assert.Equal(1, arg1Value);
            Assert.Equal(2.34, arg2Value);
        }
Ejemplo n.º 3
0
        public void TestAdjacentTokens(string input, int?prevIdx, int?currentIdx, int?nextIdx)
        {
            var tokens = new ICommandToken[]
            {
                new ArgumentToken <int> .Builder()
                .Name("name")
                .Parser(int.TryParse)
                .IsOptional(false)
                .Build(),
                new VerbToken(new Name("verb1", "alt1")),
                new VerbToken(new Name("verb2", "alt2")),
                new VerbToken(new Name("verb3", "alt3"))
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            ICommandToken currentToken = CommandParser.GetCurrentToken(usage, input);
            ICommandToken prevToken    = CommandParser.GetPreviousToken(usage, input);
            ICommandToken nextToken    = CommandParser.GetNextToken(usage, input);

            if (prevIdx.HasValue)
            {
                Assert.Same(tokens[prevIdx.Value], prevToken);
            }
            else
            {
                Assert.Null(prevToken);
            }

            if (currentIdx.HasValue)
            {
                Assert.Same(tokens[currentIdx.Value], currentToken);
            }
            else
            {
                Assert.Null(currentToken);
            }

            if (nextIdx.HasValue)
            {
                Assert.Same(tokens[nextIdx.Value], nextToken);
            }
            else
            {
                Assert.Null(nextToken);
            }
        }
Ejemplo n.º 4
0
        [InlineData("verb0 verb4 -o1 -o2 1 2.34 -o3", 0, 4)]                    //All tokens present, wrong order
        public void TestMatchWithOptionalTokensWithArgs(string input, params int[] expectedMatchingIndexes)
        {
            ArgumentToken arg1 = new ArgumentToken <int> .Builder().Name("arg1").IsOptional(false).Parser(int.TryParse).Build();

            ArgumentToken arg2 = new ArgumentToken <double> .Builder().Name("arg2").IsOptional(false).Parser(double.TryParse).Build();

            var tokens = new ICommandToken[]
            {
                new VerbToken(new Name("verb0")),
                new StandAloneOptionToken(new Name("-o1", "--option1")),
                new OptionWithArgumentToken.Builder().Name("-o2", "--option2").WithArgument(arg1).WithArgument(arg2).Build(),
                new StandAloneOptionToken(new Name("-o3", "--option3")),
                new VerbToken(new Name("verb4")),
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, input);

            Assert.Same(usage.Tokens, matchCollection.MatchableTokens);
            if (expectedMatchingIndexes.Length > 0)
            {
                Assert.NotEmpty(matchCollection.Matches);

                Assert.Equal(expectedMatchingIndexes.Length, matchCollection.Matches.Count());

                //Ensure all that are expected are there
                foreach (var expectedMatchingIndex in expectedMatchingIndexes)
                {
                    Assert.True(matchCollection.Matches.Any(x => x.TokenIdx == expectedMatchingIndex));
                }
            }
            else
            {
                Assert.Empty(matchCollection.Matches);
            }
        }
Ejemplo n.º 5
0
        [InlineData("1234 verb1 verb2 verb3 verb4", 3)] //Too many tokens
        public void TestMatch(string input, int?expectedMatchIdx)
        {
            var tokens = new ICommandToken[]
            {
                new ArgumentToken <int> .Builder()
                .Name("name")
                .Parser(int.TryParse)
                .IsOptional(false)
                .Build(),
                new VerbToken(new Name("verb1", "alt1")),
                new VerbToken(new Name("verb2", "alt2")),
                new VerbToken(new Name("verb3", "alt3"))
            };
            var builder = new CommandUsage.Builder()
                          .Description("test usage");

            foreach (var token in tokens)
            {
                builder.WithToken(token);
            }

            ICommandUsage usage = builder.Build();

            TokenMatchCollection matchCollection = CommandParser.Match(usage.Tokens, input);

            Assert.Same(usage.Tokens, matchCollection.MatchableTokens);
            if (expectedMatchIdx.HasValue)
            {
                Assert.NotEmpty(matchCollection.Matches);
                Assert.Same(tokens[expectedMatchIdx.Value], matchCollection.Matches.Last(x => x.MatchOutcome == Enums.MatchOutcome.Full).Token);
                Assert.Equal(expectedMatchIdx, matchCollection.Matches.Where(x => x.MatchOutcome == Enums.MatchOutcome.Full).Max(x => x.TokenIdx));
            }
            else
            {
                Assert.Empty(matchCollection.Matches);
            }
        }