Improve tests for CommandSchemaResolver

This commit is contained in:
Alexey Golub
2019-08-19 23:19:47 +03:00
parent c906833ac7
commit 43e3042bac
3 changed files with 116 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -6,8 +7,8 @@ namespace CliFx.Tests
{
public partial class CommandSchemaResolverTests
{
[Command("Command name", Description = "Command description")]
private class TestCommand : ICommand
[Command("cmd", Description = "NormalCommand1 description.")]
private class NormalCommand1 : ICommand
{
[CommandOption("option-a", 'a')]
public int OptionA { get; set; }
@@ -15,9 +16,57 @@ namespace CliFx.Tests
[CommandOption("option-b", IsRequired = true)]
public string OptionB { get; set; }
[CommandOption("option-c", Description = "Option C description")]
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}
[Command(Description = "NormalCommand2 description.")]
private class NormalCommand2 : ICommand
{
[CommandOption("option-c", Description = "OptionC description.")]
public bool OptionC { get; set; }
[CommandOption("option-d", 'd')]
public DateTimeOffset OptionD { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}
}
public partial class CommandSchemaResolverTests
{
[Command("conflict")]
private class ConflictingCommand1 : ICommand
{
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}
[Command("conflict")]
private class ConflictingCommand2 : ICommand
{
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}
[Command]
private class InvalidCommand1 : ICommand
{
[CommandOption("conflict")]
public string ConflictingOption1 { get; set; }
[CommandOption("conflict")]
public string ConflictingOption2 { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}
[Command]
private class InvalidCommand2 : ICommand
{
[CommandOption('c')]
public string ConflictingOption1 { get; set; }
[CommandOption('c')]
public string ConflictingOption2 { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}
}

View File

@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using CliFx.Exceptions;
using CliFx.Models;
using CliFx.Services;
using CliFx.Tests.Internal;
using FluentAssertions;
using NUnit.Framework;
@@ -11,35 +11,78 @@ namespace CliFx.Tests
[TestFixture]
public partial class CommandSchemaResolverTests
{
private static IEnumerable<TestCaseData> GetTestCases_ResolveAllSchemas()
private static IEnumerable<TestCaseData> GetTestCases_GetCommandSchemas()
{
yield return new TestCaseData(
typeof(TestCommand),
new CommandSchema(typeof(TestCommand), "Command name", "Command description",
new[]
{
new CommandOptionSchema(typeof(TestCommand).GetProperty(nameof(TestCommand.OptionA)),
"option-a", 'a', false, null),
new CommandOptionSchema(typeof(TestCommand).GetProperty(nameof(TestCommand.OptionB)),
"option-b", null, true, null),
new CommandOptionSchema(typeof(TestCommand).GetProperty(nameof(TestCommand.OptionC)),
"option-c", null, false, "Option C description")
})
new[] {typeof(NormalCommand1), typeof(NormalCommand2)},
new[]
{
new CommandSchema(typeof(NormalCommand1), "cmd", "NormalCommand1 description.",
new[]
{
new CommandOptionSchema(typeof(NormalCommand1).GetProperty(nameof(NormalCommand1.OptionA)),
"option-a", 'a', false, null),
new CommandOptionSchema(typeof(NormalCommand1).GetProperty(nameof(NormalCommand1.OptionB)),
"option-b", null, true, null)
}),
new CommandSchema(typeof(NormalCommand2), null, "NormalCommand2 description.",
new[]
{
new CommandOptionSchema(typeof(NormalCommand2).GetProperty(nameof(NormalCommand2.OptionC)),
"option-c", null, false, "OptionC description."),
new CommandOptionSchema(typeof(NormalCommand2).GetProperty(nameof(NormalCommand2.OptionD)),
"option-d", 'd', false, null)
})
}
);
}
private static IEnumerable<TestCaseData> GetTestCases_GetCommandSchemas_Negative()
{
yield return new TestCaseData(new object[]
{
new Type[0]
});
yield return new TestCaseData(new object[]
{
new[] {typeof(ConflictingCommand1), typeof(ConflictingCommand2)}
});
yield return new TestCaseData(new object[]
{
new[] {typeof(InvalidCommand1)}
});
yield return new TestCaseData(new object[]
{
new[] {typeof(InvalidCommand2)}
});
}
[Test]
[TestCaseSource(nameof(GetTestCases_ResolveAllSchemas))]
public void GetCommandSchema_Test(Type commandType, CommandSchema expectedSchema)
[TestCaseSource(nameof(GetTestCases_GetCommandSchemas))]
public void GetCommandSchemas_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<CommandSchema> expectedCommandSchemas)
{
// Arrange
var resolver = new CommandSchemaResolver();
var commandSchemaResolver = new CommandSchemaResolver();
// Act
var schema = resolver.GetCommandSchema(commandType);
var commandSchemas = commandSchemaResolver.GetCommandSchemas(commandTypes);
// Assert
schema.Should().BeEquivalentTo(expectedSchema);
commandSchemas.Should().BeEquivalentTo(expectedCommandSchemas);
}
[Test]
[TestCaseSource(nameof(GetTestCases_GetCommandSchemas_Negative))]
public void GetCommandSchemas_Negative_Test(IReadOnlyList<Type> commandTypes)
{
// Arrange
var commandSchemaResolver = new CommandSchemaResolver();
// Act & Assert
commandSchemaResolver.Invoking(r => r.GetCommandSchemas(commandTypes)).Should().ThrowExactly<InvalidCommandSchemaException>();
}
}
}

View File

@@ -86,7 +86,7 @@ namespace CliFx.Tests
[Test]
[TestCaseSource(nameof(GetTestCases_RenderHelpText))]
public void RenderHelpText_Test(HelpTextSource source, IReadOnlyList<string> substrings)
public void RenderHelpText_Test(HelpTextSource source, IReadOnlyList<string> expectedSubstrings)
{
// Arrange
using (var stdout = new StringWriter())
@@ -98,7 +98,7 @@ namespace CliFx.Tests
renderer.RenderHelpText(console, source);
// Assert
stdout.ToString().Should().ContainAll(substrings);
stdout.ToString().Should().ContainAll(expectedSubstrings);
}
}
}