Skip to content

Commit

Permalink
Fixing #1507 AddDelegate uses an abstract type when used in a branch (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazeFace authored Apr 13, 2024
1 parent fc0b553 commit eb38f76
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Spectre.Console.Cli/ConfiguratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,16 @@ public static ICommandConfigurator AddAsyncDelegate(
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddDelegate<TSettings>(
this IConfigurator<TSettings> configurator,
this IConfigurator<TSettings>? configurator,
string name,
Func<CommandContext, int> func)
where TSettings : CommandSettings
where TSettings : CommandSettings
{
if (typeof(TSettings).IsAbstract)
{
AddDelegate(configurator as IConfigurator<EmptyCommandSettings>, name, func);
}

if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
Expand Down
49 changes: 49 additions & 0 deletions test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,55 @@ public void Should_Execute_Delegate_Command_At_Root_Level()
data.ShouldBe(2);
}

[Fact]
public void Should_Execute_Nested_Delegate_Empty_Command()
{
// Given
var app = new CommandAppTester();
app.Configure(cfg =>
{
cfg.AddBranch("a", d =>
{
d.AddDelegate("b", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});
});

// When
var result = app.Run([
"a", "b"
]);

// Then
result.ExitCode.ShouldBe(0);
}

[Fact]
public void Should_Execute_Delegate_Empty_Command_At_Root_Level()
{
// Given
var app = new CommandAppTester();
app.Configure(cfg =>
{
cfg.AddDelegate("a", _ =>
{
AnsiConsole.MarkupLine("[red]Complete[/]");
return 0;
});
});

// When
var result = app.Run([
"a"
]);

// Then
result.ExitCode.ShouldBe(0);
}

[Fact]
public async void Should_Execute_Async_Delegate_Command_At_Root_Level()
{
Expand Down

0 comments on commit eb38f76

Please sign in to comment.