[InlineData("verb0 verb4 -o1 -o2 -o3", 0, 4)] //All tokens present, wrong order public void TestMatchWithOptionalTokens(string input, params int[] expectedMatchingIndexes) { var tokens = new ICommandToken[] { new VerbToken(new Name("verb0")), new StandAloneOptionToken(new Name("-o1", "--option1")), new StandAloneOptionToken(new Name("-o2", "--option2")), 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); } }
[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); } }