diff --git a/src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Constructor.cs b/src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Constructor.cs new file mode 100644 index 0000000..6a689ba --- /dev/null +++ b/src/Spectre.Console.Tests/Unit/Cli/CommandAppTests.Constructor.cs @@ -0,0 +1,106 @@ +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 class NullableSettings : CommandSettings + { + public NullableSettings(bool? detailed, string[] extra) + { + Detailed = detailed; + Extra = extra; + } + + [CommandOption("-d")] + public bool? Detailed { get; } + + [CommandArgument(0, "[extra]")] + public string[] Extra { get; } + } + + public class NullableWithInitSettings : CommandSettings + { + [CommandOption("-d")] + public bool? Detailed { get; init; } + + [CommandArgument(0, "[extra]")] + public string[] Extra { get; init; } + } + + public class NullableCommand : Command + { + public override int Execute(CommandContext context, NullableSettings settings) => 0; + } + + public class NullableWithInitCommand : Command + { + public override int Execute(CommandContext context, NullableWithInitSettings settings) => 0; + } + + [Fact] + public void Nullable_objects_in_settings_are_populated_properly() + { + // Given + var fixture = new CommandAppFixture(); + fixture.Configure(configurator => + { + configurator.SetApplicationName("myapp"); + configurator.AddCommand("null"); + }); + + // When + var (_, _, _, settings) = fixture.Run("null"); + var nullableSettings = (NullableSettings)settings; + + // Then + nullableSettings.Detailed.ShouldBeNull(); + nullableSettings.Extra.ShouldBeNull(); + } + + [Fact] + public void Nullable_objects_with_init_in_settings_are_populated_properly() + { + // Given + var fixture = new CommandAppFixture(); + fixture.Configure(configurator => + { + configurator.SetApplicationName("myapp"); + configurator.AddCommand("null"); + }); + + // When + var (_, _, _, settings) = fixture.Run("null"); + var nullableSettings = (NullableWithInitSettings)settings; + + // Then + nullableSettings.Detailed.ShouldBeNull(); + nullableSettings.Extra.ShouldBeNull(); + } + + [Fact] + public void Regular_settings_are_populated_properly() + { + // Given + var fixture = new CommandAppFixture(); + fixture.Configure(configurator => + { + configurator.SetApplicationName("myapp"); + configurator.AddCommand("null"); + }); + + // When + var (_, _, _, settings) = fixture.Run("null", "-d", "true", "first-item"); + var nullableSettings = (NullableSettings)settings; + + // Then + nullableSettings.Detailed.ShouldBe(true); + nullableSettings.Extra.ShouldBe(new[] { "first-item" }); + } + } +} \ No newline at end of file