mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
(#916) fixed missing call to Validate when using CommandConstructorBinder
This commit is contained in:
parent
78d841e3dc
commit
0d19ccd8a6
@ -45,6 +45,13 @@ internal static class CommandConstructorBinder
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the settings.
|
||||
var validationResult = settings.Validate();
|
||||
if (!validationResult.Successful)
|
||||
{
|
||||
throw CommandRuntimeException.ValidationFailed(validationResult);
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace Spectre.Console.Tests.Data;
|
||||
|
||||
[Description("The turtle command.")]
|
||||
public class TurtleCommand : AnimalCommand<TurtleSettings>
|
||||
{
|
||||
public override int Execute(CommandContext context, TurtleSettings settings)
|
||||
{
|
||||
DumpSettings(context, settings);
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace Spectre.Console.Tests.Data;
|
||||
|
||||
public class ReptileSettings : AnimalSettings
|
||||
{
|
||||
[CommandOption("-n|-p|--name|--pet-name <VALUE>")]
|
||||
public string Name { get; set; }
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
namespace Spectre.Console.Tests.Data;
|
||||
|
||||
public sealed class TurtleSettings : ReptileSettings
|
||||
{
|
||||
public TurtleSettings(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public override ValidationResult Validate()
|
||||
{
|
||||
return Name != "Lonely George"
|
||||
? ValidationResult.Error("Only 'Lonely George' is valid name for a turtle!")
|
||||
: ValidationResult.Success();
|
||||
}
|
||||
}
|
@ -54,6 +54,30 @@ public sealed partial class CommandAppTests
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Settings_Validation_Fails_On_Settings_With_ctor()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandApp();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.PropagateExceptions();
|
||||
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||
{
|
||||
animal.AddCommand<TurtleCommand>("turtle");
|
||||
});
|
||||
});
|
||||
|
||||
// When
|
||||
var result = Record.Exception(() => app.Run(new[] { "animal", "4", "turtle", "--name", "Klaus" }));
|
||||
|
||||
// Then
|
||||
result.ShouldBeOfType<CommandRuntimeException>().And(e =>
|
||||
{
|
||||
e.Message.ShouldBe("Only 'Lonely George' is valid name for a turtle!");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Throw_If_Command_Validation_Fails()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user