Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CommandLineParser.Tests/BasicDITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void CommandLineParserUsesInjectedServiceCorrectly()

parser.RegisterCommand<MyCommandThatUsesService>();

var result = parser.Parse(new[] { "app.exe", "cmd" });
var result = parser.Parse(new[] { "cmd" });

result.AssertNoErrors();

Expand Down
4 changes: 2 additions & 2 deletions CommandLineParser.Tests/Command/CommandDiscoveryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task CommandDiscoveryWithInjectedServices()

parser.DiscoverCommands(Assembly.GetExecutingAssembly());

var result = await parser.ParseAsync(new[] { "app.exe", "cmd" });
var result = await parser.ParseAsync(new[] { "cmd" });

myServiceMock.Verify(_ => _.Call(), Times.Once());
}
Expand All @@ -87,7 +87,7 @@ public async Task NonGenericCommandCanBeDiscovered()

parser.DiscoverCommands(typeof(NonGenericDiscoverableCommand).Assembly);

var result = await parser.ParseAsync(new[] { "app.exe", "cmd" });
var result = await parser.ParseAsync(new[] { "cmd" });

Assert.True(parser.Commands.Count == 1);

Expand Down
40 changes: 20 additions & 20 deletions CommandLineParser.Tests/CommandLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void OrderAttributeWorks()

var parser = new CommandLineParser<OrderModel>(Services);

var result = parser.Parse(new string[] { "app.exe", from, to });
var result = parser.Parse(new string[] { from, to });

result.AssertNoErrors();

Expand All @@ -54,7 +54,7 @@ public void OrderAttributeWorks2()

var parser = new CommandLineParser<OrderModel>(Services);

var result = parser.Parse(new string[] { "app.exe", "-r", "5", from, to });
var result = parser.Parse(new string[] { "-r", "5", from, to });

result.AssertNoErrors();

Expand All @@ -76,7 +76,7 @@ public void OrderAttributeInCommandWorks()
Assert.Equal(to, model.To);
});

var result = parser.Parse(new string[] { "app.exe", "cmd", from, to });
var result = parser.Parse(new string[] { "cmd", from, to });

result.AssertNoErrors();
}
Expand All @@ -89,7 +89,7 @@ public void OrderedOptions_With_Named_Option_Between_Does_Not_work()

var parser = new CommandLineParser<OrderModel>(Services);

var result = parser.Parse(new string[] { "app.exe", from, "-r", "5", to });
var result = parser.Parse(new string[] { from, "-r", "5", to });

Assert.True(result.HasErrors);

Expand All @@ -105,7 +105,7 @@ public void OrderedOptions_With_Named_Option_Between_Does_Not_work2()

var parser = new CommandLineParser<OrderModelInt>(Services);

var result = parser.Parse(new string[] { "app.exe", from.ToString(), "oops", to.ToString() });
var result = parser.Parse(new string[] { from.ToString(), "oops", to.ToString() });

Assert.True(result.HasErrors);

Expand All @@ -123,7 +123,7 @@ public void StopProcessingWorks()

var parser = new CommandLineParser<OrderModelInt>(options, Services);

var result = parser.Parse(new string[] { "app.exe", "10", "20", "--", "some random stuff", "nothing to see here", "yadi yadi yadi", "-r", "10" });
var result = parser.Parse(new string[] { "10", "20", "--", "some random stuff", "nothing to see here", "yadi yadi yadi", "-r", "10" });

result.AssertNoErrors();

Expand Down Expand Up @@ -203,7 +203,7 @@ public void CommandLineParserUsesContainerCorrectly(bool generic)
parser.RegisterCommand(typeof(MyCommand), typeof(object));
}

var result = parser.Parse(new[] { "app.exe", "my" });
var result = parser.Parse(new[] { "my" });

result.AssertNoErrors();

Expand Down Expand Up @@ -238,7 +238,7 @@ public async Task CommandLineParserUsesContainerCorrectlyAsync(bool generic)
parser.RegisterCommand(typeof(MyCommand), typeof(object));
}

var result = await parser.ParseAsync(new[] { "app.exe", "my" });
var result = await parser.ParseAsync(new[] { "my" });

result.AssertNoErrors();

Expand Down Expand Up @@ -299,7 +299,7 @@ public void ParseTests()
.Default("Default message")
.Required();

var parsed = parser.Parse(new string[] { "app.exe", "-o", "test" });
var parsed = parser.Parse(new string[] { "-o", "test" });

Assert.NotNull(parsed);

Expand All @@ -309,11 +309,11 @@ public void ParseTests()
}

[Theory]
[InlineData(new[] { "app.exe", "-e", "Opt1" }, false, EnumOption.Opt1)]
[InlineData(new[] { "app.exe", "-e=opt1" }, false, EnumOption.Opt1)]
[InlineData(new[] { "app.exe", "-e", "Opt2" }, false, EnumOption.Opt2)]
[InlineData(new[] { "app.exe", "-e", "bla" }, true, default(EnumOption))]
[InlineData(new[] { "app.exe", "-e" }, true, default(EnumOption))]
[InlineData(new[] { "-e", "Opt1" }, false, EnumOption.Opt1)]
[InlineData(new[] { "-e=opt1" }, false, EnumOption.Opt1)]
[InlineData(new[] { "-e", "Opt2" }, false, EnumOption.Opt2)]
[InlineData(new[] { "-e", "bla" }, true, default(EnumOption))]
[InlineData(new[] { "-e" }, true, default(EnumOption))]
public void ParseEnumInArguments(string[] args, bool hasErrors, EnumOption enumOption)
{
var parser = new CommandLineParser<EnumOptions>(Services);
Expand Down Expand Up @@ -439,7 +439,7 @@ public async Task ParseWithCommandTestsAsync()
.Name("m", "message")
.Required();

var parsed = await parser.ParseAsync(new string[] { "app.exe", "-o", "test", "add", "-m=my message" });
var parsed = await parser.ParseAsync(new string[] { "-o", "test", "add", "-m=my message" });

parsed.AssertNoErrors();

Expand All @@ -451,8 +451,8 @@ public async Task ParseWithCommandTestsAsync()
}

[Theory]
[InlineData(new[] { "app.exe", "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
[InlineData(new[] { "app.exe", "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
[InlineData(new[] { "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
[InlineData(new[] { "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
[InlineData(new[] { "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
[InlineData(new[] { "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
public void ParseCommandTests(string[] args, string result1, string result2)
Expand Down Expand Up @@ -487,8 +487,8 @@ public void ParseCommandTests(string[] args, string result1, string result2)
}

[Theory]
[InlineData(new[] { "app.exe", "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
[InlineData(new[] { "app.exe", "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
[InlineData(new[] { "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
[InlineData(new[] { "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
[InlineData(new[] { "add", "-m", "message2", "-m", "message1" }, "message1", "message2")]
[InlineData(new[] { "-m", "message1", "add", "-m", "message2" }, "message1", "message2")]
public async Task ParseCommandTestsAsync(string[] args, string result1, string result2)
Expand Down Expand Up @@ -603,7 +603,7 @@ public void ConfigureTests()
}

[Theory]
[InlineData(new string[] { "" }, "defaulttransformed", false)]
[InlineData(new string[] { }, "defaulttransformed", false)]
[InlineData(new string[] { "-m", "test" }, "testtransformed", false)]
[InlineData(new string[] { "--message", "test" }, "testtransformed", false)]
public void TransformationWorksAsExpected(string[] args, string expected, bool errors)
Expand Down
12 changes: 6 additions & 6 deletions CommandLineParser.Tests/Parsing/OptionClusteringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ClusterdOptionsAreParsedCorrectly()
{
var parser = new CommandLineParser<ClusteredOptions<bool>>(Services);

var result = parser.Parse(new[] { "app.exe", "-abc" });
var result = parser.Parse(new[] { "-abc" });

result.AssertNoErrors();

Expand All @@ -31,7 +31,7 @@ public void ClusterdOptionsAllSetTheSameValue()
{
var parser = new CommandLineParser<ClusteredOptions<bool>>(Services);

var result = parser.Parse(new[] { "app.exe", "-abc", "false" });
var result = parser.Parse(new[] { "-abc", "false" });

result.AssertNoErrors();

Expand All @@ -47,7 +47,7 @@ public void ClusterdOptionsAreIgnoredWhenRepeated()
{
var parser = new CommandLineParser<ClusteredOptions<bool>>(Services);

var result = parser.Parse(new[] { "app.exe", "-abc", "false", "-abc", "true" });
var result = parser.Parse(new[] { "-abc", "false", "-abc", "true" });

result.AssertNoErrors();

Expand All @@ -70,7 +70,7 @@ public void ClusterdOptionsInCommandWork()
Assert.False(model.C);
});

var result = parser.Parse(new[] { "app.exe", "cmd", "-abc", "false" });
var result = parser.Parse(new[] { "cmd", "-abc", "false" });

result.AssertNoErrors();
}
Expand All @@ -87,7 +87,7 @@ public void ClusterdOptionsInCommandAndReusedInParentWork()
Assert.False(model.C);
});

var result = parser.Parse(new[] { "app.exe", "-abc", "cmd", "-abc", "false" });
var result = parser.Parse(new[] { "-abc", "cmd", "-abc", "false" });

result.AssertNoErrors();

Expand All @@ -108,7 +108,7 @@ public void ClusterdOptionsInCommandAndReusedInParentWork_String_Version()
Assert.Equal("false", model.C);
});

var result = parser.Parse(new[] { "app.exe", "-abc", "works", "cmd", "-abc", "false" });
var result = parser.Parse(new[] { "-abc", "works", "cmd", "-abc", "false" });

result.AssertNoErrors();

Expand Down
121 changes: 121 additions & 0 deletions CommandLineParser.Tests/Usage/DamerauLevenshteinTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using MatthiWare.CommandLine.Abstractions;
using MatthiWare.CommandLine.Abstractions.Command;
using MatthiWare.CommandLine.Core.Usage;
using Moq;
using System.Collections.Generic;
using Xunit;
using Xunit.Abstractions;

namespace MatthiWare.CommandLine.Tests.Usage
{
public class DamerauLevenshteinTests : TestBase
{
public DamerauLevenshteinTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

[Theory]
[InlineData(3, "CA", "ABC")]
[InlineData(1, "Test", "Tst")]
[InlineData(0, "Test", "Test")]
[InlineData(4, "", "Test")]
[InlineData(4, "Test", "")]
public void DistanceOfStrings(int expected, string a, string b)
{
var dl = new DamerauLevenshteinSuggestionProvider();

Assert.Equal(expected, dl.FindDistance(a, b));
}

[Fact]
public void SuggestionsAreGatheredFromAllAvailablePlaces()
{
var containerMock = new Mock<ICommandLineCommandContainer>();
var cmdMock = new Mock<ICommandLineCommand>();
var optionMock = new Mock<ICommandLineOption>();

cmdMock.SetupGet(_ => _.Name).Returns("test1");
optionMock.SetupGet(_ => _.HasLongName).Returns(true);
optionMock.SetupGet(_ => _.HasShortName).Returns(true);
optionMock.SetupGet(_ => _.LongName).Returns("test2");
optionMock.SetupGet(_ => _.ShortName).Returns("test3");

containerMock.SetupGet(_ => _.Commands).Returns(AsList(cmdMock.Object));
containerMock.SetupGet(_ => _.Options).Returns(AsList(optionMock.Object));

var dl = new DamerauLevenshteinSuggestionProvider();

var result = dl.GetSuggestions("test", containerMock.Object);

Assert.Contains("test1", result);
Assert.Contains("test2", result);
Assert.Contains("test3", result);

List<T> AsList<T>(T obj)
{
var list = new List<T>(1) { obj };
return list;
}
}

[Fact]
public void NoContainerReturnsEmptyResult()
{
var dl = new DamerauLevenshteinSuggestionProvider();

Assert.Empty(dl.GetSuggestions(string.Empty, null));
}

[Fact]
public void InvalidSuggestionIsNotReturned()
{
var containerMock = new Mock<ICommandLineCommandContainer>();
var cmdMock = new Mock<ICommandLineCommand>();
var optionMock = new Mock<ICommandLineOption>();

cmdMock.SetupGet(_ => _.Name).Returns("test");

containerMock.SetupGet(_ => _.Commands).Returns(AsList(cmdMock.Object));
containerMock.SetupGet(_ => _.Options).Returns(new List<ICommandLineOption>());

var dl = new DamerauLevenshteinSuggestionProvider();

var result = dl.GetSuggestions("abc", containerMock.Object);

Assert.Empty(result);

List<T> AsList<T>(T obj)
{
var list = new List<T>(1) { obj };
return list;
}
}

[Fact]
public void NoSuggestionsReturnsEmpty()
{
var containerMock = new Mock<ICommandLineCommandContainer>();

containerMock.SetupGet(_ => _.Commands).Returns(new List<ICommandLineCommand>());
containerMock.SetupGet(_ => _.Options).Returns(new List<ICommandLineOption>());

var dl = new DamerauLevenshteinSuggestionProvider();

var result = dl.GetSuggestions("abc", containerMock.Object);

Assert.Empty(result);
}

[Fact]
public void Test()
{
var parser = new CommandLineParser(Services);

parser.AddCommand().Name("cmd");

var result = parser.Parse(new[] { "cmdd" });

result.AssertNoErrors();
}
}
}
5 changes: 4 additions & 1 deletion CommandLineParser.Tests/Usage/NoColorOutputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ public NoColorOutputTests(ITestOutputHelper output) : base(output)
var envMock = new Mock<IEnvironmentVariablesService>();
envMock.SetupGet(env => env.NoColorRequested).Returns(() => variableServiceResult);

var consoleMock = new Mock<IConsole>();

variablesService = envMock.Object;

var usageBuilderMock = new Mock<IUsageBuilder>();
usageBuilderMock.Setup(m => m.AddErrors(It.IsAny<IReadOnlyCollection<Exception>>())).Callback(() =>
{
consoleColorGetter(((UsagePrinter)parser.Printer).m_currentConsoleColor);
consoleColorGetter(consoleMock.Object.ForegroundColor);
});

Services.AddSingleton(envMock.Object);
Services.AddSingleton(consoleMock.Object);
Services.AddSingleton(usageBuilderMock.Object);

parser = new CommandLineParser<Options>(Services);
Expand Down
Loading