mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-09-18 18:32:42 +08:00
Add Spectre.Cli to Spectre.Console
* Renames Spectre.Cli to Spectre.Console.Cli. * Now uses Verify with Spectre.Console.Cli tests. * Removes some duplicate definitions. Closes #168
This commit is contained in:

committed by
Patrik Svensson

parent
0bbf9b81a9
commit
0ae419326d
@@ -0,0 +1,92 @@
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli.Annotations
|
||||
{
|
||||
public sealed partial class CommandArgumentAttributeTests
|
||||
{
|
||||
[UsesVerify]
|
||||
public sealed class TheArgumentCannotContainOptionsMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandArgument(0, "--foo <BAR>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var result = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheMultipleValuesAreNotSupportedMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandArgument(0, "<FOO> <BAR>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var result = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheValuesMustHaveNameMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandArgument(0, "<>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var result = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Fixture
|
||||
{
|
||||
public static string Run<TSettings>(params string[] args)
|
||||
where TSettings : CommandSettings
|
||||
{
|
||||
using (var writer = new FakeConsole())
|
||||
{
|
||||
var app = new CommandApp();
|
||||
app.Configure(c => c.ConfigureConsole(writer));
|
||||
app.Configure(c => c.AddCommand<GenericCommand<TSettings>>("foo"));
|
||||
app.Run(args);
|
||||
|
||||
return writer.Output
|
||||
.NormalizeLineEndings()
|
||||
.TrimLines()
|
||||
.Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli.Annotations
|
||||
{
|
||||
public sealed partial class CommandArgumentAttributeTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Not_Contain_Options()
|
||||
{
|
||||
// Given, When
|
||||
var result = Record.Exception(() => new CommandArgumentAttribute(0, "--foo <BAR>"));
|
||||
|
||||
// Then
|
||||
result.ShouldNotBe(null);
|
||||
result.ShouldBeOfType<CommandTemplateException>().And(exception =>
|
||||
exception.Message.ShouldBe("Arguments can not contain options."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<FOO> <BAR>")]
|
||||
[InlineData("[FOO] [BAR]")]
|
||||
[InlineData("[FOO] <BAR>")]
|
||||
[InlineData("<FOO> [BAR]")]
|
||||
public void Should_Not_Contain_Multiple_Value_Names(string template)
|
||||
{
|
||||
// Given, When
|
||||
var result = Record.Exception(() => new CommandArgumentAttribute(0, template));
|
||||
|
||||
// Then
|
||||
result.ShouldNotBe(null);
|
||||
result.ShouldBeOfType<CommandTemplateException>().And(exception =>
|
||||
exception.Message.ShouldBe("Multiple values are not supported."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<>")]
|
||||
[InlineData("[]")]
|
||||
public void Should_Not_Contain_Empty_Value_Name(string template)
|
||||
{
|
||||
// Given, When
|
||||
var result = Record.Exception(() => new CommandArgumentAttribute(0, template));
|
||||
|
||||
// Then
|
||||
result.ShouldNotBe(null);
|
||||
result.ShouldBeOfType<CommandTemplateException>().And(exception =>
|
||||
exception.Message.ShouldBe("Values without name are not allowed."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<FOO>", true)]
|
||||
[InlineData("[FOO]", false)]
|
||||
public void Should_Parse_Valid_Options(string template, bool required)
|
||||
{
|
||||
// Given, When
|
||||
var result = new CommandArgumentAttribute(0, template);
|
||||
|
||||
// Then
|
||||
result.ValueName.ShouldBe("FOO");
|
||||
result.IsRequired.ShouldBe(required);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,239 @@
|
||||
using System.Threading.Tasks;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli.Annotations
|
||||
{
|
||||
public sealed partial class CommandOptionAttributeTests
|
||||
{
|
||||
[UsesVerify]
|
||||
public sealed class TheUnexpectedCharacterMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("<FOO> $ <BAR>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Encountered unexpected character '$'.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheUnterminatedValueNameMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("--foo|-f <BAR")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Encountered unterminated value name 'BAR'.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheOptionsMustHaveNameMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("--foo|-")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Options without name are not allowed.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheOptionNamesCannotStartWithDigitMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("--1foo")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Option names cannot start with a digit.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheInvalidCharacterInOptionNameMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("--f$oo")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Encountered invalid character '$' in option name.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheLongOptionMustHaveMoreThanOneCharacterMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("--f")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Long option names must consist of more than one character.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheShortOptionMustOnlyBeOneCharacterMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("--foo|-bar")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Short option names can not be longer than one character.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheMultipleOptionValuesAreNotSupportedMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("-f|--foo <FOO> <BAR>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Multiple option values are not supported.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheInvalidCharacterInValueNameMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("-f|--foo <F$OO>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("Encountered invalid character '$' in value name.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class TheMissingLongAndShortNameMethod
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[CommandOption("<FOO>")]
|
||||
public string Foo { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given, When
|
||||
var (message, result) = Fixture.Run<Settings>();
|
||||
|
||||
// Then
|
||||
message.ShouldBe("No long or short name for option has been specified.");
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Fixture
|
||||
{
|
||||
public static (string Message, string Output) Run<TSettings>(params string[] args)
|
||||
where TSettings : CommandSettings
|
||||
{
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(c =>
|
||||
{
|
||||
c.PropagateExceptions();
|
||||
c.AddCommand<GenericCommand<TSettings>>("foo");
|
||||
});
|
||||
|
||||
return app.RunAndCatch<CommandTemplateException>(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,197 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli.Annotations
|
||||
{
|
||||
public sealed partial class CommandOptionAttributeTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Parse_Short_Name_Correctly()
|
||||
{
|
||||
// Given, When
|
||||
var option = new CommandOptionAttribute("-o|--option <VALUE>");
|
||||
|
||||
// Then
|
||||
option.ShortNames.ShouldContain("o");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Parse_Long_Name_Correctly()
|
||||
{
|
||||
// Given, When
|
||||
var option = new CommandOptionAttribute("-o|--option <VALUE>");
|
||||
|
||||
// Then
|
||||
option.LongNames.ShouldContain("option");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<VALUE>")]
|
||||
public void Should_Parse_Value_Correctly(string value)
|
||||
{
|
||||
// Given, When
|
||||
var option = new CommandOptionAttribute($"-o|--option {value}");
|
||||
|
||||
// Then
|
||||
option.ValueName.ShouldBe("VALUE");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Parse_Only_Short_Name()
|
||||
{
|
||||
// Given, When
|
||||
var option = new CommandOptionAttribute("-o");
|
||||
|
||||
// Then
|
||||
option.ShortNames.ShouldContain("o");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Parse_Only_Long_Name()
|
||||
{
|
||||
// Given, When
|
||||
var option = new CommandOptionAttribute("--option");
|
||||
|
||||
// Then
|
||||
option.LongNames.ShouldContain("option");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("<VALUE>")]
|
||||
public void Should_Throw_If_Template_Is_Empty(string value)
|
||||
{
|
||||
// Given, When
|
||||
var option = Record.Exception(() => new CommandOptionAttribute(value));
|
||||
|
||||
// Then
|
||||
option.ShouldBeOfType<CommandTemplateException>().And(e =>
|
||||
e.Message.ShouldBe("No long or short name for option has been specified."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("--bar|-foo")]
|
||||
[InlineData("--bar|-f-b")]
|
||||
public void Should_Throw_If_Short_Name_Is_Invalid(string value)
|
||||
{
|
||||
// Given, When
|
||||
var option = Record.Exception(() => new CommandOptionAttribute(value));
|
||||
|
||||
// Then
|
||||
option.ShouldBeOfType<CommandTemplateException>().And(e =>
|
||||
e.Message.ShouldBe("Short option names can not be longer than one character."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("--o")]
|
||||
public void Should_Throw_If_Long_Name_Is_Invalid(string value)
|
||||
{
|
||||
// Given, When
|
||||
var option = Record.Exception(() => new CommandOptionAttribute(value));
|
||||
|
||||
// Then
|
||||
option.ShouldBeOfType<CommandTemplateException>().And(e =>
|
||||
e.Message.ShouldBe("Long option names must consist of more than one character."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("-")]
|
||||
[InlineData("--")]
|
||||
public void Should_Throw_If_Option_Have_No_Name(string template)
|
||||
{
|
||||
// Given, When
|
||||
var option = Record.Exception(() => new CommandOptionAttribute(template));
|
||||
|
||||
// Then
|
||||
option.ShouldBeOfType<CommandTemplateException>().And(e =>
|
||||
e.Message.ShouldBe("Options without name are not allowed."));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("--foo|-foo[b", '[')]
|
||||
[InlineData("--foo|-f€b", '€')]
|
||||
[InlineData("--foo|-foo@b", '@')]
|
||||
public void Should_Throw_If_Option_Contains_Invalid_Name(string template, char invalid)
|
||||
{
|
||||
// Given, When
|
||||
var result = Record.Exception(() => new CommandOptionAttribute(template));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandTemplateException>().And(e =>
|
||||
{
|
||||
e.Message.ShouldBe($"Encountered invalid character '{invalid}' in option name.");
|
||||
e.Template.ShouldBe(template);
|
||||
});
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("--foo <HELLO-WORLD>", "HELLO-WORLD")]
|
||||
[InlineData("--foo <HELLO_WORLD>", "HELLO_WORLD")]
|
||||
public void Should_Accept_Dash_And_Underscore_In_Value_Name(string template, string name)
|
||||
{
|
||||
// Given, When
|
||||
var result = new CommandOptionAttribute(template);
|
||||
|
||||
// Then
|
||||
result.ValueName.ShouldBe(name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("--foo|-1")]
|
||||
public void Should_Throw_If_First_Letter_Of_An_Option_Name_Is_A_Digit(string template)
|
||||
{
|
||||
// Given, When
|
||||
var result = Record.Exception(() => new CommandOptionAttribute(template));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandTemplateException>().And(e =>
|
||||
{
|
||||
e.Message.ShouldBe("Option names cannot start with a digit.");
|
||||
e.Template.ShouldBe(template);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Multiple_Short_Options_Are_Supported()
|
||||
{
|
||||
// Given, When
|
||||
var result = new CommandOptionAttribute("-f|-b");
|
||||
|
||||
// Then
|
||||
result.ShortNames.Count.ShouldBe(2);
|
||||
result.ShortNames.ShouldContain("f");
|
||||
result.ShortNames.ShouldContain("b");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Multiple_Long_Options_Are_Supported()
|
||||
{
|
||||
// Given, When
|
||||
var result = new CommandOptionAttribute("--foo|--bar");
|
||||
|
||||
// Then
|
||||
result.LongNames.Count.ShouldBe(2);
|
||||
result.LongNames.ShouldContain("foo");
|
||||
result.LongNames.ShouldContain("bar");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("-f|--foo <BAR>")]
|
||||
[InlineData("--foo|-f <BAR>")]
|
||||
[InlineData("<BAR> --foo|-f")]
|
||||
[InlineData("<BAR> -f|--foo")]
|
||||
[InlineData("-f <BAR> --foo")]
|
||||
[InlineData("--foo <BAR> -f")]
|
||||
public void Template_Parts_Can_Appear_In_Any_Order(string template)
|
||||
{
|
||||
// Given, When
|
||||
var result = new CommandOptionAttribute(template);
|
||||
|
||||
// Then
|
||||
result.LongNames.ShouldContain("foo");
|
||||
result.ShortNames.ShouldContain("f");
|
||||
result.ValueName.ShouldBe("BAR");
|
||||
}
|
||||
}
|
||||
}
|
234
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.FlagValues.cs
Normal file
234
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.FlagValues.cs
Normal file
@@ -0,0 +1,234 @@
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Spectre.Console.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class FlagValues
|
||||
{
|
||||
[SuppressMessage("Performance", "CA1812", Justification = "It's OK")]
|
||||
private sealed class FlagSettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--serve [PORT]")]
|
||||
public FlagValue<int> Serve { get; set; }
|
||||
}
|
||||
|
||||
[SuppressMessage("Performance", "CA1812", Justification = "It's OK")]
|
||||
private sealed class FlagSettingsWithNullableValueType : CommandSettings
|
||||
{
|
||||
[CommandOption("--serve [PORT]")]
|
||||
public FlagValue<int?> Serve { get; set; }
|
||||
}
|
||||
|
||||
[SuppressMessage("Performance", "CA1812", Justification = "It's OK")]
|
||||
private sealed class FlagSettingsWithOptionalOptionButNoFlagValue : CommandSettings
|
||||
{
|
||||
[CommandOption("--serve [PORT]")]
|
||||
public int Serve { get; set; }
|
||||
}
|
||||
|
||||
[SuppressMessage("Performance", "CA1812", Justification = "It's OK")]
|
||||
private sealed class FlagSettingsWithDefaultValue : CommandSettings
|
||||
{
|
||||
[CommandOption("--serve [PORT]")]
|
||||
[DefaultValue(987)]
|
||||
public FlagValue<int> Serve { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Command_Option_Value_Is_Optional_But_Type_Is_Not_A_Flag_Value()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FlagSettingsWithOptionalOptionButNoFlagValue>>("foo");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "foo", "--serve", "123" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The option 'serve' has an optional value but does not implement IFlagValue.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Set_Flag_And_Value_If_Both_Were_Provided()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FlagSettings>>("foo");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"foo", "--serve", "123",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<FlagSettings>().And(flag =>
|
||||
{
|
||||
flag.Serve.IsSet.ShouldBeTrue();
|
||||
flag.Serve.Value.ShouldBe(123);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Only_Set_Flag_If_No_Value_Was_Provided()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FlagSettings>>("foo");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"foo", "--serve",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<FlagSettings>().And(flag =>
|
||||
{
|
||||
flag.Serve.IsSet.ShouldBeTrue();
|
||||
flag.Serve.Value.ShouldBe(0);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Set_Value_To_Default_Value_If_None_Was_Explicitly_Set()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FlagSettingsWithDefaultValue>>("foo");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"foo", "--serve",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<FlagSettingsWithDefaultValue>().And(flag =>
|
||||
{
|
||||
flag.Serve.IsSet.ShouldBeTrue();
|
||||
flag.Serve.Value.ShouldBe(987);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Create_Unset_Instance_If_Flag_Was_Not_Set()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FlagSettings>>("foo");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"foo",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<FlagSettings>().And(flag =>
|
||||
{
|
||||
flag.Serve.IsSet.ShouldBeFalse();
|
||||
flag.Serve.Value.ShouldBe(0);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Create_Unset_Instance_With_Null_For_Nullable_Value_Type_If_Flag_Was_Not_Set()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FlagSettingsWithNullableValueType>>("foo");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"foo",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<FlagSettingsWithNullableValueType>().And(flag =>
|
||||
{
|
||||
flag.Serve.IsSet.ShouldBeFalse();
|
||||
flag.Serve.Value.ShouldBeNull();
|
||||
});
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Foo", true, "Set=True, Value=Foo")]
|
||||
[InlineData("Bar", false, "Set=False, Value=Bar")]
|
||||
public void Should_Return_Correct_String_Representation_From_ToString(
|
||||
string value,
|
||||
bool isSet,
|
||||
string expected)
|
||||
{
|
||||
// Given
|
||||
var flag = new FlagValue<string>();
|
||||
flag.Value = value;
|
||||
flag.IsSet = isSet;
|
||||
|
||||
// When
|
||||
var result = flag.ToString();
|
||||
|
||||
// Then
|
||||
result.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, "Set=True")]
|
||||
[InlineData(false, "Set=False")]
|
||||
public void Should_Return_Correct_String_Representation_From_ToString_If_Value_Is_Not_Set(
|
||||
bool isSet,
|
||||
string expected)
|
||||
{
|
||||
// Given
|
||||
var flag = new FlagValue<string>();
|
||||
flag.IsSet = isSet;
|
||||
|
||||
// When
|
||||
var result = flag.ToString();
|
||||
|
||||
// Then
|
||||
result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
231
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Help.cs
Normal file
231
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Help.cs
Normal file
@@ -0,0 +1,231 @@
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
[UsesVerify]
|
||||
public class Help
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Output_Root_Correctly()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
configurator.AddCommand<HorseCommand>("horse");
|
||||
configurator.AddCommand<GiraffeCommand>("giraffe");
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Skip_Hidden_Commands()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
configurator.AddCommand<HorseCommand>("horse");
|
||||
configurator.AddCommand<GiraffeCommand>("giraffe").IsHidden();
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Command_Correctly()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddBranch<CatSettings>("cat", animal =>
|
||||
{
|
||||
animal.SetDescription("Contains settings for a cat.");
|
||||
animal.AddCommand<LionCommand>("lion");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("cat", "--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Leaf_Correctly()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddBranch<CatSettings>("cat", animal =>
|
||||
{
|
||||
animal.SetDescription("Contains settings for a cat.");
|
||||
animal.AddCommand<LionCommand>("lion");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("cat", "lion", "--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Default_Command_Correctly()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.WithDefaultCommand<LionCommand>();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Root_Examples_Defined_On_Root()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddExample(new[] { "dog", "--name", "Rufus", "--age", "12", "--good-boy" });
|
||||
configurator.AddExample(new[] { "horse", "--name", "Brutus" });
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
configurator.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Root_Examples_Defined_On_Direct_Children_If_Root_Have_No_Examples()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddCommand<DogCommand>("dog")
|
||||
.WithExample(new[] { "dog", "--name", "Rufus", "--age", "12", "--good-boy" });
|
||||
configurator.AddCommand<HorseCommand>("horse")
|
||||
.WithExample(new[] { "horse", "--name", "Brutus" });
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Root_Examples_Defined_On_Leaves_If_No_Other_Examples_Are_Found()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.SetDescription("The animal command.");
|
||||
animal.AddCommand<DogCommand>("dog")
|
||||
.WithExample(new[] { "animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy" });
|
||||
animal.AddCommand<HorseCommand>("horse")
|
||||
.WithExample(new[] { "animal", "horse", "--name", "Brutus" });
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Only_Output_Command_Examples_Defined_On_Command()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.SetDescription("The animal command.");
|
||||
animal.AddExample(new[] { "animal", "--help" });
|
||||
|
||||
animal.AddCommand<DogCommand>("dog")
|
||||
.WithExample(new[] { "animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy" });
|
||||
animal.AddCommand<HorseCommand>("horse")
|
||||
.WithExample(new[] { "animal", "horse", "--name", "Brutus" });
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("animal", "--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Output_Root_Examples_If_Default_Command_Is_Specified()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.WithDefaultCommand<LionCommand>();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddExample(new[] { "12", "-c", "3" });
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class Injection
|
||||
{
|
||||
public sealed class FakeDependency
|
||||
{
|
||||
}
|
||||
|
||||
public sealed class InjectSettings : CommandSettings
|
||||
{
|
||||
public FakeDependency Fake { get; set; }
|
||||
|
||||
[CommandOption("--name <NAME>")]
|
||||
public string Name { get; }
|
||||
|
||||
[CommandOption("--age <AGE>")]
|
||||
public int Age { get; set; }
|
||||
|
||||
public InjectSettings(FakeDependency fake, string name)
|
||||
{
|
||||
Fake = fake;
|
||||
Name = "Hello " + name;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Inject_Parameters()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
var dependency = new FakeDependency();
|
||||
|
||||
app.WithDefaultCommand<GenericCommand<InjectSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.Settings.Registrar.RegisterInstance(dependency);
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--name", "foo",
|
||||
"--age", "35",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<InjectSettings>().And(injected =>
|
||||
{
|
||||
injected.ShouldNotBeNull();
|
||||
injected.Fake.ShouldBeSameAs(dependency);
|
||||
injected.Name.ShouldBe("Hello foo");
|
||||
injected.Age.ShouldBe(35);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
292
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Pairs.cs
Normal file
292
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Pairs.cs
Normal file
@@ -0,0 +1,292 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class Pairs
|
||||
{
|
||||
public sealed class AmbiguousSettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--var <VALUE>")]
|
||||
[PairDeconstructor(typeof(StringIntDeconstructor))]
|
||||
[TypeConverter(typeof(CatAgilityConverter))]
|
||||
public ILookup<string, string> Values { get; set; }
|
||||
}
|
||||
|
||||
public sealed class NotDeconstructableSettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--var <VALUE>")]
|
||||
[PairDeconstructor(typeof(StringIntDeconstructor))]
|
||||
public string Values { get; set; }
|
||||
}
|
||||
|
||||
public sealed class DefaultPairDeconstructorSettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--var <VALUE>")]
|
||||
public IDictionary<string, int> Values { get; set; }
|
||||
}
|
||||
|
||||
public sealed class LookupSettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--var <VALUE>")]
|
||||
[PairDeconstructor(typeof(StringIntDeconstructor))]
|
||||
public ILookup<string, string> Values { get; set; }
|
||||
}
|
||||
|
||||
public sealed class DictionarySettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--var <VALUE>")]
|
||||
[PairDeconstructor(typeof(StringIntDeconstructor))]
|
||||
public IDictionary<string, string> Values { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ReadOnlyDictionarySettings : CommandSettings
|
||||
{
|
||||
[CommandOption("--var <VALUE>")]
|
||||
[PairDeconstructor(typeof(StringIntDeconstructor))]
|
||||
public IReadOnlyDictionary<string, string> Values { get; set; }
|
||||
}
|
||||
|
||||
public sealed class StringIntDeconstructor : PairDeconstuctor<string, string>
|
||||
{
|
||||
protected override (string Key, string Value) Deconstruct(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
var parts = value.Split(new[] { '=' });
|
||||
if (parts.Length != 2)
|
||||
{
|
||||
throw new InvalidOperationException("Could not parse pair");
|
||||
}
|
||||
|
||||
return (parts[0], parts[1]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Option_Has_Pair_Deconstructor_And_Type_Converter()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp<GenericCommand<AmbiguousSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[]
|
||||
{
|
||||
"--var", "foo=bar",
|
||||
"--var", "foo=qux",
|
||||
}));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The option 'var' is both marked as pair deconstructable and convertable.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Option_Has_Pair_Deconstructor_But_Type_Is_Not_Deconstructable()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp<GenericCommand<NotDeconstructableSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[]
|
||||
{
|
||||
"--var", "foo=bar",
|
||||
"--var", "foo=qux",
|
||||
}));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The option 'var' is marked as pair deconstructable, but the underlying type does not support that.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Map_Pairs_To_Pair_Deconstructable_Collection_Using_Default_Deconstructort()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<DefaultPairDeconstructorSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--var", "foo=1",
|
||||
"--var", "foo=3",
|
||||
"--var", "bar=4",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DefaultPairDeconstructorSettings>().And(pair =>
|
||||
{
|
||||
pair.Values.ShouldNotBeNull();
|
||||
pair.Values.Count.ShouldBe(2);
|
||||
pair.Values["foo"].ShouldBe(3);
|
||||
pair.Values["bar"].ShouldBe(4);
|
||||
});
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("foo=1=2", "Error: The value 'foo=1=2' is not in a correct format")]
|
||||
[InlineData("foo=1=2=3", "Error: The value 'foo=1=2=3' is not in a correct format")]
|
||||
public void Should_Throw_If_Value_Is_Not_In_A_Valid_Format_Using_Default_Deconstructor(
|
||||
string input, string expected)
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<DefaultPairDeconstructorSettings>>();
|
||||
|
||||
// When
|
||||
var (result, output, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--var", input,
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(-1);
|
||||
output.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Map_Lookup_Values()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<LookupSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--var", "foo=bar",
|
||||
"--var", "foo=qux",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<LookupSettings>().And(pair =>
|
||||
{
|
||||
pair.Values.ShouldNotBeNull();
|
||||
pair.Values.Count.ShouldBe(1);
|
||||
pair.Values["foo"].ToList().Count.ShouldBe(2);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Map_Dictionary_Values()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<DictionarySettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--var", "foo=bar",
|
||||
"--var", "baz=qux",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DictionarySettings>().And(pair =>
|
||||
{
|
||||
pair.Values.ShouldNotBeNull();
|
||||
pair.Values.Count.ShouldBe(2);
|
||||
pair.Values["foo"].ShouldBe("bar");
|
||||
pair.Values["baz"].ShouldBe("qux");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Map_Latest_Value_Of_Same_Key_When_Mapping_To_Dictionary()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<DictionarySettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--var", "foo=bar",
|
||||
"--var", "foo=qux",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DictionarySettings>().And(pair =>
|
||||
{
|
||||
pair.Values.ShouldNotBeNull();
|
||||
pair.Values.Count.ShouldBe(1);
|
||||
pair.Values["foo"].ShouldBe("qux");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Map_ReadOnly_Dictionary_Values()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<ReadOnlyDictionarySettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"--var", "foo=bar",
|
||||
"--var", "baz=qux",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<ReadOnlyDictionarySettings>().And(pair =>
|
||||
{
|
||||
pair.Values.ShouldNotBeNull();
|
||||
pair.Values.Count.ShouldBe(2);
|
||||
pair.Values["foo"].ShouldBe("bar");
|
||||
pair.Values["baz"].ShouldBe("qux");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
615
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Parsing.cs
Normal file
615
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Parsing.cs
Normal file
@@ -0,0 +1,615 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public static class Parsing
|
||||
{
|
||||
[UsesVerify]
|
||||
public sealed class UnknownCommand
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_When_Command_Is_Unknown()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("cat", "14");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_With_Suggestion_When_Root_Command_Followed_By_Argument_Is_Unknown_And_Distance_Is_Small()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<CatCommand>("cat");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("bat", "14");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_With_Suggestion_When_Command_Followed_By_Argument_Is_Unknown_And_Distance_Is_Small()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddBranch<CommandSettings>("dog", a =>
|
||||
{
|
||||
a.AddCommand<CatCommand>("cat");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "bat", "14");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_With_Suggestion_And_No_Arguments_When_Root_Command_Is_Unknown_And_Distance_Is_Small()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.WithDefaultCommand<GenericCommand<EmptyCommandSettings>>();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<GenericCommand<EmptyCommandSettings>>("cat");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("bat");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_With_Suggestion_And_No_Arguments_When_Command_Is_Unknown_And_Distance_Is_Small()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.WithDefaultCommand<GenericCommand<EmptyCommandSettings>>();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddBranch<CommandSettings>("dog", a =>
|
||||
{
|
||||
a.AddCommand<CatCommand>("cat");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "bat");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_With_Suggestion_When_Root_Command_After_Argument_Is_Unknown_And_Distance_Is_Small()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.WithDefaultCommand<GenericCommand<FooCommandSettings>>();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<GenericCommand<BarCommandSettings>>("bar");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("qux", "bat");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_With_Suggestion_When_Command_After_Argument_Is_Unknown_And_Distance_Is_Small()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddBranch<FooCommandSettings>("foo", a =>
|
||||
{
|
||||
a.AddCommand<GenericCommand<BarCommandSettings>>("bar");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("foo", "qux", "bat");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Unknown_Command_When_Current_Command_Has_No_Arguments()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<EmptyCommand>("empty");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("empty", "other");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class CannotAssignValueToFlag
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Long_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "--alive", "foo");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Short_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "-a", "foo");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class NoValueForOption
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Long_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "--name");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Short_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "-n");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class NoMatchingArgument
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<GiraffeCommand>("giraffe");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("giraffe", "foo", "bar", "baz");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class UnexpectedOption
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Long_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("--foo");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Short_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("-f");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class UnknownOption
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Long_Option_If_Strict_Mode_Is_Enabled()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.UseStrictParsing();
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "--unknown");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Short_Option_If_Strict_Mode_Is_Enabled()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.UseStrictParsing();
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "-u");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class UnterminatedQuote
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("--name", "\"Rufus");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class OptionWithoutName
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Short_Option()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", "-", " ");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Missing_Long_Option_Value_With_Equality_Separator()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"--foo=");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Missing_Long_Option_Value_With_Colon_Separator()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"--foo:");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Missing_Short_Option_Value_With_Equality_Separator()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"-f=");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text_For_Missing_Short_Option_Value_With_Colon_Separator()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"-f:");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class InvalidShortOptionName
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"-f0o");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class LongOptionNameIsOneCharacter
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"--f");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class LongOptionNameIsMissing
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"-- ");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class LongOptionNameStartWithDigit
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"--1foo");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
|
||||
[UsesVerify]
|
||||
public sealed class LongOptionNameContainSymbol
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Return_Correct_Text()
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", $"--f€oo");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("--f-oo")]
|
||||
[InlineData("--f-o-o")]
|
||||
[InlineData("--f_oo")]
|
||||
[InlineData("--f_o_o")]
|
||||
public void Should_Allow_Special_Symbols_In_Name(string option)
|
||||
{
|
||||
// Given
|
||||
var fixture = new Fixture();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("dog", option);
|
||||
|
||||
// Then
|
||||
result.ShouldBe("Error: Command 'dog' is missing required argument 'AGE'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Fixture
|
||||
{
|
||||
private Action<CommandApp> _appConfiguration = _ => { };
|
||||
private Action<IConfigurator> _configuration;
|
||||
|
||||
public void WithDefaultCommand<T>()
|
||||
where T : class, ICommand
|
||||
{
|
||||
_appConfiguration = (app) => app.SetDefaultCommand<T>();
|
||||
}
|
||||
|
||||
public void Configure(Action<IConfigurator> action)
|
||||
{
|
||||
_configuration = action;
|
||||
}
|
||||
|
||||
public string Run(params string[] args)
|
||||
{
|
||||
using (var console = new FakeConsole())
|
||||
{
|
||||
var app = new CommandApp();
|
||||
_appConfiguration?.Invoke(app);
|
||||
|
||||
app.Configure(_configuration);
|
||||
app.Configure(c => c.ConfigureConsole(console));
|
||||
app.Run(args);
|
||||
|
||||
return console.Output
|
||||
.NormalizeLineEndings()
|
||||
.TrimLines()
|
||||
.Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,118 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandApptests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Treat_Commands_As_Case_Sensitive_If_Specified()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.UseStrictParsing();
|
||||
config.PropagateExceptions();
|
||||
config.CaseSensitivity(CaseSensitivity.Commands);
|
||||
config.AddCommand<GenericCommand<StringOptionSettings>>("command");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[]
|
||||
{
|
||||
"Command", "--foo", "bar",
|
||||
}));
|
||||
|
||||
// Then
|
||||
result.ShouldNotBeNull();
|
||||
result.ShouldBeOfType<CommandParseException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("Unknown command 'Command'.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Treat_Long_Options_As_Case_Sensitive_If_Specified()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.UseStrictParsing();
|
||||
config.PropagateExceptions();
|
||||
config.CaseSensitivity(CaseSensitivity.LongOptions);
|
||||
config.AddCommand<GenericCommand<StringOptionSettings>>("command");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[]
|
||||
{
|
||||
"command", "--Foo", "bar",
|
||||
}));
|
||||
|
||||
// Then
|
||||
result.ShouldNotBeNull();
|
||||
result.ShouldBeOfType<CommandParseException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("Unknown option 'Foo'.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Treat_Short_Options_As_Case_Sensitive()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.UseStrictParsing();
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<StringOptionSettings>>("command");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[]
|
||||
{
|
||||
"command", "-F", "bar",
|
||||
}));
|
||||
|
||||
// Then
|
||||
result.ShouldNotBeNull();
|
||||
result.ShouldBeOfType<CommandParseException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("Unknown option 'F'.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Suppress_Case_Sensitivity_If_Specified()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.UseStrictParsing();
|
||||
config.PropagateExceptions();
|
||||
config.CaseSensitivity(CaseSensitivity.None);
|
||||
config.AddCommand<GenericCommand<StringOptionSettings>>("command");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"Command", "--Foo", "bar",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<StringOptionSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.ShouldBe("bar");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Apply_Case_Sensitivity_For_Everything_By_Default()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
|
||||
// When
|
||||
var defaultSensitivity = CaseSensitivity.None;
|
||||
app.Configure(config =>
|
||||
{
|
||||
defaultSensitivity = config.Settings.CaseSensitivity;
|
||||
});
|
||||
|
||||
// Then
|
||||
defaultSensitivity.ShouldBe(CaseSensitivity.All);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class TypeConverters
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Bind_Using_Custom_Type_Converter_If_Specified()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<CatCommand>("cat");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"cat", "--name", "Tiger",
|
||||
"--agility", "FOOBAR",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<CatSettings>().And(cat =>
|
||||
{
|
||||
cat.Agility.ShouldBe(6);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
299
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Unsafe.cs
Normal file
299
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Unsafe.cs
Normal file
@@ -0,0 +1,299 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Spectre.Console.Cli.Unsafe;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class SafetyOff
|
||||
{
|
||||
[Fact]
|
||||
public void Can_Mix_Safe_And_Unsafe_Configurators()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.SafetyOff().AddBranch("mammal", typeof(MammalSettings), mammal =>
|
||||
{
|
||||
mammal.AddCommand("dog", typeof(DogCommand));
|
||||
mammal.AddCommand("horse", typeof(HorseCommand));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "--alive", "mammal", "--name",
|
||||
"Rufus", "dog", "12", "--good-boy",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Can_Turn_Safety_On_After_Turning_It_Off_For_Branch()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
|
||||
config.SafetyOff().AddBranch("animal", typeof(AnimalSettings), animal =>
|
||||
{
|
||||
animal.SafetyOn<AnimalSettings>()
|
||||
.AddBranch<MammalSettings>("mammal", mammal =>
|
||||
{
|
||||
mammal.SafetyOff().AddCommand("dog", typeof(DogCommand));
|
||||
mammal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "--alive", "mammal", "--name",
|
||||
"Rufus", "dog", "12", "--good-boy",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Trying_To_Convert_Unsafe_Branch_Configurator_To_Safe_Version_With_Wrong_Type()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
|
||||
config.SafetyOff().AddBranch("animal", typeof(AnimalSettings), animal =>
|
||||
{
|
||||
animal.SafetyOn<MammalSettings>().AddCommand<DogCommand>("dog");
|
||||
});
|
||||
}));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>();
|
||||
result.Message.ShouldBe("Configurator cannot be converted to a safe configurator of type 'MammalSettings'.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_1()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
|
||||
config.SafetyOff().AddBranch("animal", typeof(AnimalSettings), animal =>
|
||||
{
|
||||
animal.AddBranch("mammal", typeof(MammalSettings), mammal =>
|
||||
{
|
||||
mammal.AddCommand("dog", typeof(DogCommand));
|
||||
mammal.AddCommand("horse", typeof(HorseCommand));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "--alive", "mammal", "--name",
|
||||
"Rufus", "dog", "12", "--good-boy",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_2()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.SafetyOff().AddCommand("dog", typeof(DogCommand));
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"dog", "12", "4", "--good-boy",
|
||||
"--name", "Rufus", "--alive",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Legs.ShouldBe(12);
|
||||
dog.Age.ShouldBe(4);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_3()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.SafetyOff().AddBranch("animal", typeof(AnimalSettings), animal =>
|
||||
{
|
||||
animal.AddCommand("dog", typeof(DogCommand));
|
||||
animal.AddCommand("horse", typeof(HorseCommand));
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "dog", "12", "--good-boy",
|
||||
"--name", "Rufus",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(false);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_4()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.SafetyOff().AddBranch("animal", typeof(AnimalSettings), animal =>
|
||||
{
|
||||
animal.AddCommand("dog", typeof(DogCommand));
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12",
|
||||
"--good-boy", "--name", "Rufus",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Legs.ShouldBe(4);
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.IsAlive.ShouldBe(false);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_5()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.SafetyOff().AddCommand("multi", typeof(OptionVectorCommand));
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"multi", "--foo", "a", "--foo", "b", "--bar", "1", "--foo", "c", "--bar", "2",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.Length.ShouldBe(3);
|
||||
vec.Foo.ShouldBe(new[] { "a", "b", "c" });
|
||||
vec.Bar.Length.ShouldBe(2);
|
||||
vec.Bar.ShouldBe(new[] { 1, 2 });
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_6()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<ArgumentVectorSettings>>("multi");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"multi", "a", "b", "c",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.Length.ShouldBe(3);
|
||||
vec.Foo.ShouldBe(new[] { "a", "b", "c" });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class Validation
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Throw_If_Attribute_Validation_Fails()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "animal", "3", "dog", "7", "--name", "Rufus" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandRuntimeException>().And(e =>
|
||||
{
|
||||
e.Message.ShouldBe("Animals must have an even number of legs.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Settings_Validation_Fails()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "animal", "4", "dog", "7", "--name", "Tiger" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandRuntimeException>().And(e =>
|
||||
{
|
||||
e.Message.ShouldBe("Tiger is not a dog name!");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Command_Validation_Fails()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "animal", "4", "dog", "101", "--name", "Rufus" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandRuntimeException>().And(e =>
|
||||
{
|
||||
e.Message.ShouldBe("Dog is too old...");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
111
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Vectors.cs
Normal file
111
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Vectors.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class Vectors
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Throw_If_A_Single_Command_Has_Multiple_Argument_Vectors()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<MultipleArgumentVectorSettings>>("multi");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "multi", "a", "b", "c" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The command 'multi' specifies more than one vector argument.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_An_Argument_Vector_Is_Not_Specified_Last()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<MultipleArgumentVectorSpecifiedFirstSettings>>("multi");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "multi", "a", "b", "c" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The command 'multi' specifies an argument vector that is not the last argument.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Assign_Values_To_Argument_Vector()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<ArgumentVectorSettings>>("multi");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"multi", "a", "b", "c",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.Length.ShouldBe(3);
|
||||
vec.Foo[0].ShouldBe("a");
|
||||
vec.Foo[1].ShouldBe("b");
|
||||
vec.Foo[2].ShouldBe("c");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Assign_Values_To_Option_Vector()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<OptionVectorCommand>("cmd");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"cmd", "--foo", "red",
|
||||
"--bar", "4", "--foo", "blue",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.ShouldBe(new string[] { "red", "blue" });
|
||||
vec.Bar.ShouldBe(new int[] { 4 });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using Shouldly;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
public sealed class Version
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Output_The_Version_To_The_Console()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddBranch<MammalSettings>("mammal", mammal =>
|
||||
{
|
||||
mammal.AddCommand<DogCommand>("dog");
|
||||
mammal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.VersionCommand);
|
||||
|
||||
// Then
|
||||
output.ShouldStartWith("Spectre.Cli version ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
148
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Xml.cs
Normal file
148
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Xml.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Testing;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
[UsesVerify]
|
||||
public sealed class Xml
|
||||
{
|
||||
/// <summary>
|
||||
/// https://github.com/spectresystems/spectre.cli/wiki/Test-cases#test-case-1
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public Task Should_Dump_Correct_Model_For_Case_1()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddBranch<MammalSettings>("mammal", mammal =>
|
||||
{
|
||||
mammal.AddCommand<DogCommand>("dog");
|
||||
mammal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://github.com/spectresystems/spectre.cli/wiki/Test-cases#test-case-2
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public Task Should_Dump_Correct_Model_For_Case_2()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://github.com/spectresystems/spectre.cli/wiki/Test-cases#test-case-3
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public Task Should_Dump_Correct_Model_For_Case_3()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://github.com/spectresystems/spectre.cli/wiki/Test-cases#test-case-4
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public Task Should_Dump_Correct_Model_For_Case_4()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://github.com/spectresystems/spectre.cli/wiki/Test-cases#test-case-5
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public Task Should_Dump_Correct_Model_For_Case_5()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<OptionVectorCommand>("cmd");
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Dump_Correct_Model_For_Model_With_Default_Command()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppFixture().WithDefaultCommand<DogCommand>();
|
||||
fixture.Configure(config =>
|
||||
{
|
||||
config.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
|
||||
// When
|
||||
var (_, output, _, _) = fixture.Run(Constants.XmlDocCommand);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
769
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.cs
Normal file
769
src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.cs
Normal file
@@ -0,0 +1,769 @@
|
||||
using System;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Cli;
|
||||
using Spectre.Console.Tests.Data;
|
||||
using Spectre.Console.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli
|
||||
{
|
||||
public sealed partial class CommandAppTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Pass_Case_1()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddBranch<MammalSettings>("mammal", mammal =>
|
||||
{
|
||||
mammal.AddCommand<DogCommand>("dog");
|
||||
mammal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "--alive", "mammal", "--name",
|
||||
"Rufus", "dog", "12", "--good-boy",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_2()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"dog", "12", "4", "--good-boy",
|
||||
"--name", "Rufus", "--alive",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Legs.ShouldBe(12);
|
||||
dog.Age.ShouldBe(4);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_3()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "dog", "12", "--good-boy",
|
||||
"--name", "Rufus",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
dog.IsAlive.ShouldBe(false);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_4()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12", "--good-boy",
|
||||
"--name", "Rufus",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Legs.ShouldBe(4);
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.IsAlive.ShouldBe(false);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_5()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<OptionVectorCommand>("multi");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"multi", "--foo", "a", "--foo", "b",
|
||||
"--bar", "1", "--foo", "c", "--bar", "2",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.Length.ShouldBe(3);
|
||||
vec.Foo.ShouldBe(new[] { "a", "b", "c" });
|
||||
vec.Bar.Length.ShouldBe(2);
|
||||
vec.Bar.ShouldBe(new[] { 1, 2 });
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Case_6()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<ArgumentVectorSettings>>("multi");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"multi", "a", "b", "c",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<ArgumentVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.Length.ShouldBe(3);
|
||||
vec.Foo.ShouldBe(new[] { "a", "b", "c" });
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Be_Able_To_Use_Command_Alias()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<OptionVectorCommand>("multi").WithAlias("multiple");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"multiple", "--foo", "a",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<OptionVectorSettings>().And(vec =>
|
||||
{
|
||||
vec.Foo.Length.ShouldBe(1);
|
||||
vec.Foo.ShouldBe(new[] { "a" });
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Assign_Default_Value_To_Optional_Argument()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<OptionalArgumentWithDefaultValueSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(Array.Empty<string>());
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<OptionalArgumentWithDefaultValueSettings>().And(settings =>
|
||||
{
|
||||
settings.Greeting.ShouldBe("Hello World");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Assign_Default_Value_To_Optional_Argument_Using_Converter_If_Necessary()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<OptionalArgumentWithDefaultValueAndTypeConverterSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(Array.Empty<string>());
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<OptionalArgumentWithDefaultValueAndTypeConverterSettings>().And(settings =>
|
||||
{
|
||||
settings.Greeting.ShouldBe(5);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Required_Argument_Have_Default_Value()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<GenericCommand<RequiredArgumentWithDefaultValueSettings>>();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(Array.Empty<string>()));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The required argument 'GREETING' cannot have a default value.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Alias_Conflicts_With_Another_Command()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<DogCommand>("dog").WithAlias("cat");
|
||||
config.AddCommand<CatCommand>("cat");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "dog", "4", "12" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("The alias 'cat' for 'dog' conflicts with another command.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Register_Commands_When_Configuring_Application()
|
||||
{
|
||||
// Given
|
||||
var registrar = new FakeTypeRegistrar();
|
||||
var app = new CommandApp(registrar);
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<GenericCommand<FooCommandSettings>>("foo");
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12",
|
||||
});
|
||||
|
||||
// Then
|
||||
registrar.Registrations.ContainsKey(typeof(ICommand)).ShouldBeTrue();
|
||||
registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(GenericCommand<FooCommandSettings>));
|
||||
registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(DogCommand));
|
||||
registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(HorseCommand));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Register_Default_Command_When_Configuring_Application()
|
||||
{
|
||||
// Given
|
||||
var registrar = new FakeTypeRegistrar();
|
||||
var app = new CommandApp<DogCommand>(registrar);
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
app.Run(new[]
|
||||
{
|
||||
"12", "4",
|
||||
});
|
||||
|
||||
// Then
|
||||
registrar.Registrations.ContainsKey(typeof(ICommand)).ShouldBeTrue();
|
||||
registrar.Registrations.ContainsKey(typeof(DogSettings));
|
||||
registrar.Registrations[typeof(ICommand)].ShouldContain(typeof(DogCommand));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Register_Default_Command_Settings_When_Configuring_Application()
|
||||
{
|
||||
// Given
|
||||
var registrar = new FakeTypeRegistrar();
|
||||
var app = new CommandApp<DogCommand>(registrar);
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
});
|
||||
|
||||
// When
|
||||
app.Run(new[]
|
||||
{
|
||||
"12", "4",
|
||||
});
|
||||
|
||||
// Then
|
||||
registrar.Registrations.ContainsKey(typeof(DogSettings));
|
||||
registrar.Registrations[typeof(DogSettings)].Count.ShouldBe(1);
|
||||
registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("true", true)]
|
||||
[InlineData("True", true)]
|
||||
[InlineData("false", false)]
|
||||
[InlineData("False", false)]
|
||||
public void Should_Accept_Explicit_Boolan_Flag(string value, bool expected)
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"dog", "12", "4", "--alive", value,
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.IsAlive.ShouldBe(expected);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Register_Command_Settings_When_Configuring_Application()
|
||||
{
|
||||
// Given
|
||||
var registrar = new FakeTypeRegistrar();
|
||||
var app = new CommandApp(registrar);
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12",
|
||||
});
|
||||
|
||||
// Then
|
||||
registrar.Registrations.ContainsKey(typeof(DogSettings)).ShouldBeTrue();
|
||||
registrar.Registrations[typeof(DogSettings)].Count.ShouldBe(1);
|
||||
registrar.Registrations[typeof(DogSettings)].ShouldContain(typeof(DogSettings));
|
||||
registrar.Registrations.ContainsKey(typeof(MammalSettings)).ShouldBeTrue();
|
||||
registrar.Registrations[typeof(MammalSettings)].Count.ShouldBe(1);
|
||||
registrar.Registrations[typeof(MammalSettings)].ShouldContain(typeof(MammalSettings));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_When_Encountering_Unknown_Option_In_Strict_Mode()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.UseStrictParsing();
|
||||
config.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "dog", "--foo" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandParseException>().And(ex =>
|
||||
{
|
||||
ex.Message.ShouldBe("Unknown option 'foo'.");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Add_Unknown_Option_To_Remaining_Arguments_In_Relaxed_Mode()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, ctx, _) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12",
|
||||
"--foo", "bar",
|
||||
});
|
||||
|
||||
// Then
|
||||
ctx.ShouldNotBeNull();
|
||||
ctx.Remaining.Parsed.Count.ShouldBe(1);
|
||||
ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar" });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, ctx, _) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12", "--foo",
|
||||
});
|
||||
|
||||
// Then
|
||||
ctx.ShouldNotBeNull();
|
||||
ctx.Remaining.Parsed.Count.ShouldBe(1);
|
||||
ctx.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Be_Able_To_Set_The_Default_Command()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.WithDefaultCommand<DogCommand>();
|
||||
|
||||
// When
|
||||
var (result, _, _, settings) = app.Run(new[]
|
||||
{
|
||||
"4", "12", "--good-boy", "--name", "Rufus",
|
||||
});
|
||||
|
||||
// Then
|
||||
result.ShouldBe(0);
|
||||
settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||
{
|
||||
dog.Legs.ShouldBe(4);
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.GoodBoy.ShouldBe(true);
|
||||
dog.Name.ShouldBe("Rufus");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Set_Command_Name_In_Context()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, ctx, _) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12",
|
||||
});
|
||||
|
||||
// Then
|
||||
ctx.ShouldNotBeNull();
|
||||
ctx.Name.ShouldBe("dog");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Pass_Command_Data_In_Context()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog").WithData(123);
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, ctx, _) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12",
|
||||
});
|
||||
|
||||
// Then
|
||||
ctx.ShouldNotBeNull();
|
||||
ctx.Data.ShouldBe(123);
|
||||
}
|
||||
|
||||
public sealed class Delegate_Commands
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Execute_Delegate_Command_At_Root_Level()
|
||||
{
|
||||
// Given
|
||||
var dog = default(DogSettings);
|
||||
var data = 0;
|
||||
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddDelegate<DogSettings>(
|
||||
"foo", (context, settings) =>
|
||||
{
|
||||
dog = settings;
|
||||
data = (int)context.Data;
|
||||
return 1;
|
||||
}).WithData(2);
|
||||
});
|
||||
|
||||
// When
|
||||
var result = app.Run(new[] { "foo", "4", "12" });
|
||||
|
||||
// Then
|
||||
result.ShouldBe(1);
|
||||
dog.ShouldNotBeNull();
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.Legs.ShouldBe(4);
|
||||
data.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Execute_Nested_Delegate_Command()
|
||||
{
|
||||
// Given
|
||||
var dog = default(DogSettings);
|
||||
var data = 0;
|
||||
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("foo", foo =>
|
||||
{
|
||||
foo.AddDelegate<DogSettings>(
|
||||
"bar", (context, settings) =>
|
||||
{
|
||||
dog = settings;
|
||||
data = (int)context.Data;
|
||||
return 1;
|
||||
}).WithData(2);
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = app.Run(new[] { "foo", "4", "bar", "12" });
|
||||
|
||||
// Then
|
||||
result.ShouldBe(1);
|
||||
dog.ShouldNotBeNull();
|
||||
dog.Age.ShouldBe(12);
|
||||
dog.Legs.ShouldBe(4);
|
||||
data.ShouldBe(2);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Remaining_Arguments
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Register_Remaining_Parsed_Arguments_With_Context()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, ctx, _) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12", "--",
|
||||
"--foo", "bar", "--foo", "baz",
|
||||
"-bar", "\"baz\"", "qux",
|
||||
});
|
||||
|
||||
// Then
|
||||
ctx.Remaining.Parsed.Count.ShouldBe(4);
|
||||
ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
|
||||
ctx.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
|
||||
ctx.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
|
||||
ctx.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Register_Remaining_Raw_Arguments_With_Context()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, ctx, _) = app.Run(new[]
|
||||
{
|
||||
"animal", "4", "dog", "12", "--",
|
||||
"--foo", "bar", "-bar", "\"baz\"", "qux",
|
||||
});
|
||||
|
||||
// Then
|
||||
ctx.Remaining.Raw.Count.ShouldBe(5);
|
||||
ctx.Remaining.Raw[0].ShouldBe("--foo");
|
||||
ctx.Remaining.Raw[1].ShouldBe("bar");
|
||||
ctx.Remaining.Raw[2].ShouldBe("-bar");
|
||||
ctx.Remaining.Raw[3].ShouldBe("\"baz\"");
|
||||
ctx.Remaining.Raw[4].ShouldBe("qux");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Exception_Handling
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Not_Propagate_Runtime_Exceptions_If_Not_Explicitly_Told_To_Do_So()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<DogCommand>("dog");
|
||||
animal.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, _) = app.Run(new[] { "animal", "4", "dog", "101", "--name", "Rufus" });
|
||||
|
||||
// Then
|
||||
result.ShouldBe(-1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Not_Propagate_Exceptions_If_Not_Explicitly_Told_To_Do_So()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppFixture();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.AddCommand<ThrowingCommand>("throw");
|
||||
});
|
||||
|
||||
// When
|
||||
var (result, _, _, _) = app.Run(new[] { "throw" });
|
||||
|
||||
// Then
|
||||
result.ShouldBe(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user