Add negative tests for CommandOptionInputConverter

This commit is contained in:
Alexey Golub
2019-08-20 12:27:11 +03:00
parent 89cc3c8785
commit 256b693466
4 changed files with 47 additions and 5 deletions

View File

@@ -46,4 +46,18 @@ namespace CliFx.Tests
new TestStringParseableWithFormatProvider(value + " " + formatProvider); new TestStringParseableWithFormatProvider(value + " " + formatProvider);
} }
} }
// Negative
public partial class CommandOptionInputConverterTests
{
private class NonStringParseable
{
public int Value { get; }
public NonStringParseable(int value)
{
Value = value;
}
}
}
} }

View File

@@ -2,6 +2,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using CliFx.Exceptions;
using CliFx.Models; using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using FluentAssertions; using FluentAssertions;
@@ -261,19 +262,44 @@ namespace CliFx.Tests
); );
} }
private static IEnumerable<TestCaseData> 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] [Test]
[TestCaseSource(nameof(GetTestCases_ConvertOptionInput))] [TestCaseSource(nameof(GetTestCases_ConvertOptionInput))]
public void ConvertOptionInput_Test(CommandOptionInput optionInput, Type targetType, object expectedConvertedValue) public void ConvertOptionInput_Test(CommandOptionInput optionInput, Type targetType, object expectedConvertedValue)
{ {
// Arrange // Arrange
var converter = new CommandOptionInputConverter(); var commandOptionInputConverter = new CommandOptionInputConverter();
// Act // Act
var convertedValue = converter.ConvertOptionInput(optionInput, targetType); var convertedValue = commandOptionInputConverter.ConvertOptionInput(optionInput, targetType);
// Assert // Assert
convertedValue.Should().BeEquivalentTo(expectedConvertedValue); convertedValue.Should().BeEquivalentTo(expectedConvertedValue);
convertedValue?.Should().BeAssignableTo(targetType); 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<InvalidCommandOptionInputException>();
}
} }
} }

View File

@@ -10,8 +10,6 @@ namespace CliFx.Tests
[Command("cmd", Description = "NormalCommand1 description.")] [Command("cmd", Description = "NormalCommand1 description.")]
private class NormalCommand1 : ICommand private class NormalCommand1 : ICommand
{ {
public string NotAnOption { get; set; }
[CommandOption("option-a", 'a')] [CommandOption("option-a", 'a')]
public int OptionA { get; set; } public int OptionA { get; set; }
@@ -30,10 +28,13 @@ namespace CliFx.Tests
[CommandOption("option-d", 'd')] [CommandOption("option-d", 'd')]
public DateTimeOffset OptionD { get; set; } public DateTimeOffset OptionD { get; set; }
public string NotAnOption { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask; public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
} }
} }
// Negative
public partial class CommandSchemaResolverTests public partial class CommandSchemaResolverTests
{ {
[Command("conflict")] [Command("conflict")]

View File

@@ -87,7 +87,8 @@ namespace CliFx.Tests
var commandSchemaResolver = new CommandSchemaResolver(); var commandSchemaResolver = new CommandSchemaResolver();
// Act & Assert // Act & Assert
commandSchemaResolver.Invoking(r => r.GetCommandSchemas(commandTypes)).Should().ThrowExactly<InvalidCommandSchemaException>(); commandSchemaResolver.Invoking(r => r.GetCommandSchemas(commandTypes)).Should()
.ThrowExactly<InvalidCommandSchemaException>();
} }
} }
} }