diff --git a/CliFx.Tests/CommandOptionInputConverterTests.Types.cs b/CliFx.Tests/CommandOptionInputConverterTests.Types.cs index 66baf53..f79abbf 100644 --- a/CliFx.Tests/CommandOptionInputConverterTests.Types.cs +++ b/CliFx.Tests/CommandOptionInputConverterTests.Types.cs @@ -46,4 +46,18 @@ namespace CliFx.Tests new TestStringParseableWithFormatProvider(value + " " + formatProvider); } } + + // Negative + public partial class CommandOptionInputConverterTests + { + private class NonStringParseable + { + public int Value { get; } + + public NonStringParseable(int value) + { + Value = value; + } + } + } } \ No newline at end of file diff --git a/CliFx.Tests/CommandOptionInputConverterTests.cs b/CliFx.Tests/CommandOptionInputConverterTests.cs index 9067806..efb3711 100644 --- a/CliFx.Tests/CommandOptionInputConverterTests.cs +++ b/CliFx.Tests/CommandOptionInputConverterTests.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; +using CliFx.Exceptions; using CliFx.Models; using CliFx.Services; using FluentAssertions; @@ -261,19 +262,44 @@ namespace CliFx.Tests ); } + private static IEnumerable GetTestCases_ConvertOptionInput_Negative() + { + yield return new TestCaseData( + new CommandOptionInput("option", "1234.5"), + typeof(int) + ); + + yield return new TestCaseData( + new CommandOptionInput("option", "123"), + typeof(NonStringParseable) + ); + } + [Test] [TestCaseSource(nameof(GetTestCases_ConvertOptionInput))] public void ConvertOptionInput_Test(CommandOptionInput optionInput, Type targetType, object expectedConvertedValue) { // Arrange - var converter = new CommandOptionInputConverter(); + var commandOptionInputConverter = new CommandOptionInputConverter(); // Act - var convertedValue = converter.ConvertOptionInput(optionInput, targetType); + var convertedValue = commandOptionInputConverter.ConvertOptionInput(optionInput, targetType); // Assert convertedValue.Should().BeEquivalentTo(expectedConvertedValue); convertedValue?.Should().BeAssignableTo(targetType); } + + [Test] + [TestCaseSource(nameof(GetTestCases_ConvertOptionInput_Negative))] + public void ConvertOptionInput_Negative_Test(CommandOptionInput optionInput, Type targetType) + { + // Arrange + var commandOptionInputConverter = new CommandOptionInputConverter(); + + // Act & Assert + commandOptionInputConverter.Invoking(c => c.ConvertOptionInput(optionInput, targetType)).Should() + .ThrowExactly(); + } } } \ No newline at end of file diff --git a/CliFx.Tests/CommandSchemaResolverTests.Commands.cs b/CliFx.Tests/CommandSchemaResolverTests.Commands.cs index c5e9801..f3acea7 100644 --- a/CliFx.Tests/CommandSchemaResolverTests.Commands.cs +++ b/CliFx.Tests/CommandSchemaResolverTests.Commands.cs @@ -10,8 +10,6 @@ namespace CliFx.Tests [Command("cmd", Description = "NormalCommand1 description.")] private class NormalCommand1 : ICommand { - public string NotAnOption { get; set; } - [CommandOption("option-a", 'a')] public int OptionA { get; set; } @@ -30,10 +28,13 @@ namespace CliFx.Tests [CommandOption("option-d", 'd')] public DateTimeOffset OptionD { get; set; } + public string NotAnOption { get; set; } + public Task ExecuteAsync(IConsole console) => Task.CompletedTask; } } + // Negative public partial class CommandSchemaResolverTests { [Command("conflict")] diff --git a/CliFx.Tests/CommandSchemaResolverTests.cs b/CliFx.Tests/CommandSchemaResolverTests.cs index da02277..84b1373 100644 --- a/CliFx.Tests/CommandSchemaResolverTests.cs +++ b/CliFx.Tests/CommandSchemaResolverTests.cs @@ -87,7 +87,8 @@ namespace CliFx.Tests var commandSchemaResolver = new CommandSchemaResolver(); // Act & Assert - commandSchemaResolver.Invoking(r => r.GetCommandSchemas(commandTypes)).Should().ThrowExactly(); + commandSchemaResolver.Invoking(r => r.GetCommandSchemas(commandTypes)).Should() + .ThrowExactly(); } } } \ No newline at end of file