From 5bb175fd4b2652e22d02488c43698b71ac9c616d Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Fri, 26 Jul 2019 17:39:28 +0300 Subject: [PATCH] Use FluentAssertions --- CliFx.Tests/CliApplicationTests.cs | 5 +-- CliFx.Tests/CliFx.Tests.csproj | 3 +- CliFx.Tests/CommandFactoryTests.cs | 5 +-- CliFx.Tests/CommandInitializerTests.cs | 26 +++++++-------- CliFx.Tests/CommandInputParserTests.cs | 4 +-- .../CommandOptionInputConverterTests.cs | 10 +++--- CliFx.Tests/CommandSchemaResolverTests.cs | 4 +-- CliFx.Tests/DummyTests.cs | 7 ++-- .../Internal/CommandInputEqualityComparer.cs | 31 ----------------- .../CommandOptionInputEqualityComparer.cs | 31 ----------------- .../CommandOptionSchemaEqualityComparer.cs | 33 ------------------- .../Internal/CommandSchemaEqualityComparer.cs | 33 ------------------- Readme.md | 3 ++ 13 files changed, 36 insertions(+), 159 deletions(-) delete mode 100644 CliFx.Tests/Internal/CommandInputEqualityComparer.cs delete mode 100644 CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs delete mode 100644 CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs delete mode 100644 CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs diff --git a/CliFx.Tests/CliApplicationTests.cs b/CliFx.Tests/CliApplicationTests.cs index b50525b..78ca246 100644 --- a/CliFx.Tests/CliApplicationTests.cs +++ b/CliFx.Tests/CliApplicationTests.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Exceptions; using CliFx.Models; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -209,7 +210,7 @@ namespace CliFx.Tests var exitCodeValue = await application.RunAsync(commandLineArguments); // Assert - Assert.That(exitCodeValue, Is.Zero, "Exit code"); + exitCodeValue.Should().Be(0); } [Test] @@ -223,7 +224,7 @@ namespace CliFx.Tests var exitCodeValue = await application.RunAsync(commandLineArguments); // Assert - Assert.That(exitCodeValue, Is.Not.Zero, "Exit code"); + exitCodeValue.Should().NotBe(0); } } } \ No newline at end of file diff --git a/CliFx.Tests/CliFx.Tests.csproj b/CliFx.Tests/CliFx.Tests.csproj index 811a4d4..f87a873 100644 --- a/CliFx.Tests/CliFx.Tests.csproj +++ b/CliFx.Tests/CliFx.Tests.csproj @@ -11,10 +11,11 @@ + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CliFx.Tests/CommandFactoryTests.cs b/CliFx.Tests/CommandFactoryTests.cs index 92db42e..5ac3a2a 100644 --- a/CliFx.Tests/CommandFactoryTests.cs +++ b/CliFx.Tests/CommandFactoryTests.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Models; using CliFx.Services; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -31,13 +32,13 @@ namespace CliFx.Tests { // Arrange var factory = new CommandFactory(); + var schema = new CommandSchemaResolver().GetCommandSchema(commandType); // Act - var schema = new CommandSchemaResolver().GetCommandSchema(commandType); var command = factory.CreateCommand(schema); // Assert - Assert.That(command, Is.TypeOf(commandType)); + command.Should().BeOfType(commandType); } } } \ No newline at end of file diff --git a/CliFx.Tests/CommandInitializerTests.cs b/CliFx.Tests/CommandInitializerTests.cs index 32c0782..e1e3cb0 100644 --- a/CliFx.Tests/CommandInitializerTests.cs +++ b/CliFx.Tests/CommandInitializerTests.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Exceptions; using CliFx.Models; using CliFx.Services; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -12,7 +14,9 @@ namespace CliFx.Tests public partial class CommandInitializerTests { [Command] - public class TestCommand : ICommand + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")] + [SuppressMessage("ReSharper", "MemberCanBePrivate.Local")] + private class TestCommand : ICommand { [CommandOption("int", 'i', IsRequired = true)] public int IntOption { get; set; } = 24; @@ -107,23 +111,18 @@ namespace CliFx.Tests [Test] [TestCaseSource(nameof(GetTestCases_InitializeCommand))] - public void InitializeCommand_Test(CommandInput commandInput, TestCommand expectedCommand) + public void InitializeCommand_Test(CommandInput commandInput, ICommand expectedCommand) { // Arrange var initializer = new CommandInitializer(); - - // Act var schema = new CommandSchemaResolver().GetCommandSchema(typeof(TestCommand)); var command = new TestCommand(); + + // Act initializer.InitializeCommand(command, schema, commandInput); // Assert - Assert.Multiple(() => - { - Assert.That(command.StringOption, Is.EqualTo(expectedCommand.StringOption), nameof(command.StringOption)); - Assert.That(command.IntOption, Is.EqualTo(expectedCommand.IntOption), nameof(command.IntOption)); - Assert.That(command.BoolOption, Is.EqualTo(expectedCommand.BoolOption), nameof(command.BoolOption)); - }); + command.Should().BeEquivalentTo(expectedCommand, o => o.RespectingRuntimeTypes()); } [Test] @@ -132,11 +131,12 @@ namespace CliFx.Tests { // Arrange var initializer = new CommandInitializer(); - - // Act & Assert var schema = new CommandSchemaResolver().GetCommandSchema(typeof(TestCommand)); var command = new TestCommand(); - Assert.Throws(() => initializer.InitializeCommand(command, schema, commandInput)); + + // Act & Assert + initializer.Invoking(i => i.InitializeCommand(command, schema, commandInput)) + .Should().ThrowExactly(); } } } \ No newline at end of file diff --git a/CliFx.Tests/CommandInputParserTests.cs b/CliFx.Tests/CommandInputParserTests.cs index 17a3e24..4a5194e 100644 --- a/CliFx.Tests/CommandInputParserTests.cs +++ b/CliFx.Tests/CommandInputParserTests.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using CliFx.Models; using CliFx.Services; -using CliFx.Tests.Internal; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -170,7 +170,7 @@ namespace CliFx.Tests var commandInput = parser.ParseInput(commandLineArguments); // Assert - Assert.That(commandInput, Is.EqualTo(expectedCommandInput).Using(CommandInputEqualityComparer.Instance)); + commandInput.Should().BeEquivalentTo(expectedCommandInput); } } } \ No newline at end of file diff --git a/CliFx.Tests/CommandOptionInputConverterTests.cs b/CliFx.Tests/CommandOptionInputConverterTests.cs index 035ee45..878565c 100644 --- a/CliFx.Tests/CommandOptionInputConverterTests.cs +++ b/CliFx.Tests/CommandOptionInputConverterTests.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using CliFx.Models; using CliFx.Services; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -212,13 +213,10 @@ namespace CliFx.Tests var convertedValue = converter.ConvertOption(optionInput, targetType); // Assert - Assert.Multiple(() => - { - Assert.That(convertedValue, Is.EqualTo(expectedConvertedValue)); + convertedValue.Should().BeEquivalentTo(expectedConvertedValue); - if (convertedValue != null) - Assert.That(convertedValue, Is.AssignableTo(targetType)); - }); + if (convertedValue != null) + convertedValue.Should().BeAssignableTo(targetType); } } } \ No newline at end of file diff --git a/CliFx.Tests/CommandSchemaResolverTests.cs b/CliFx.Tests/CommandSchemaResolverTests.cs index 91904a1..6c24386 100644 --- a/CliFx.Tests/CommandSchemaResolverTests.cs +++ b/CliFx.Tests/CommandSchemaResolverTests.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Models; using CliFx.Services; -using CliFx.Tests.Internal; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -61,7 +61,7 @@ namespace CliFx.Tests var schema = resolver.GetCommandSchema(commandType); // Assert - Assert.That(schema, Is.EqualTo(expectedSchema).Using(CommandSchemaEqualityComparer.Instance)); + schema.Should().BeEquivalentTo(expectedSchema); } } } \ No newline at end of file diff --git a/CliFx.Tests/DummyTests.cs b/CliFx.Tests/DummyTests.cs index fc18830..e725b41 100644 --- a/CliFx.Tests/DummyTests.cs +++ b/CliFx.Tests/DummyTests.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using CliWrap; +using FluentAssertions; using NUnit.Framework; namespace CliFx.Tests @@ -31,7 +32,7 @@ namespace CliFx.Tests .ExecuteAsync(); // Assert - Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput), "Stdout"); + result.StandardOutput.Trim().Should().Be(expectedOutput); } [Test] @@ -46,7 +47,7 @@ namespace CliFx.Tests .ExecuteAsync(); // Assert - Assert.That(result.StandardOutput.Trim(), Is.EqualTo(DummyVersionText), "Stdout"); + result.StandardOutput.Trim().Should().Be(DummyVersionText); } [Test] @@ -66,7 +67,7 @@ namespace CliFx.Tests .ExecuteAsync(); // Assert - Assert.That(result.StandardOutput.Trim(), Is.Not.Empty, "Stdout"); + result.StandardOutput.Trim().Should().NotBeNullOrWhiteSpace(); } } } \ No newline at end of file diff --git a/CliFx.Tests/Internal/CommandInputEqualityComparer.cs b/CliFx.Tests/Internal/CommandInputEqualityComparer.cs deleted file mode 100644 index 88eca83..0000000 --- a/CliFx.Tests/Internal/CommandInputEqualityComparer.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using CliFx.Models; - -namespace CliFx.Tests.Internal -{ - public partial class CommandInputEqualityComparer : IEqualityComparer - { - /// - public bool Equals(CommandInput x, CommandInput y) - { - if (ReferenceEquals(x, y)) - return true; - - if (x is null || y is null) - return false; - - return StringComparer.OrdinalIgnoreCase.Equals(x.CommandName, y.CommandName) && - x.Options.SequenceEqual(y.Options, CommandOptionInputEqualityComparer.Instance); - } - - /// - public int GetHashCode(CommandInput obj) => throw new NotSupportedException(); - } - - public partial class CommandInputEqualityComparer - { - public static CommandInputEqualityComparer Instance { get; } = new CommandInputEqualityComparer(); - } -} \ No newline at end of file diff --git a/CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs b/CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs deleted file mode 100644 index 41a6f92..0000000 --- a/CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using CliFx.Models; - -namespace CliFx.Tests.Internal -{ - public partial class CommandOptionInputEqualityComparer : IEqualityComparer - { - /// - public bool Equals(CommandOptionInput x, CommandOptionInput y) - { - if (ReferenceEquals(x, y)) - return true; - - if (x is null || y is null) - return false; - - return StringComparer.OrdinalIgnoreCase.Equals(x.Alias, y.Alias) && - x.Values.SequenceEqual(y.Values, StringComparer.Ordinal); - } - - /// - public int GetHashCode(CommandOptionInput obj) => throw new NotSupportedException(); - } - - public partial class CommandOptionInputEqualityComparer - { - public static CommandOptionInputEqualityComparer Instance { get; } = new CommandOptionInputEqualityComparer(); - } -} \ No newline at end of file diff --git a/CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs b/CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs deleted file mode 100644 index bb3a70f..0000000 --- a/CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using CliFx.Models; - -namespace CliFx.Tests.Internal -{ - public partial class CommandOptionSchemaEqualityComparer : IEqualityComparer - { - /// - public bool Equals(CommandOptionSchema x, CommandOptionSchema y) - { - if (ReferenceEquals(x, y)) - return true; - - if (x is null || y is null) - return false; - - return x.Property == y.Property && - StringComparer.OrdinalIgnoreCase.Equals(x.Name, y.Name) && - x.ShortName == y.ShortName && - StringComparer.OrdinalIgnoreCase.Equals(x.GroupName, y.GroupName) && - StringComparer.Ordinal.Equals(x.Description, y.Description); - } - - /// - public int GetHashCode(CommandOptionSchema obj) => throw new NotSupportedException(); - } - - public partial class CommandOptionSchemaEqualityComparer - { - public static CommandOptionSchemaEqualityComparer Instance { get; } = new CommandOptionSchemaEqualityComparer(); - } -} \ No newline at end of file diff --git a/CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs b/CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs deleted file mode 100644 index fbc3a60..0000000 --- a/CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using CliFx.Models; - -namespace CliFx.Tests.Internal -{ - public partial class CommandSchemaEqualityComparer : IEqualityComparer - { - /// - public bool Equals(CommandSchema x, CommandSchema y) - { - if (ReferenceEquals(x, y)) - return true; - - if (x is null || y is null) - return false; - - return x.Type == y.Type && - StringComparer.OrdinalIgnoreCase.Equals(x.Name, y.Name) && - StringComparer.Ordinal.Equals(x.Description, y.Description) && - x.Options.SequenceEqual(y.Options, CommandOptionSchemaEqualityComparer.Instance); - } - - /// - public int GetHashCode(CommandSchema obj) => throw new NotSupportedException(); - } - - public partial class CommandSchemaEqualityComparer - { - public static CommandSchemaEqualityComparer Instance { get; } = new CommandSchemaEqualityComparer(); - } -} \ No newline at end of file diff --git a/Readme.md b/Readme.md index 1ce1e26..5f0ee48 100644 --- a/Readme.md +++ b/Readme.md @@ -28,6 +28,9 @@ To be added with a stable release... ## Libraries used - [NUnit](https://github.com/nunit/nunit) +- [FluentAssertions](https://github.com/fluentassertions/fluentassertions) +- [CliWrap](https://github.com/Tyrrrz/CliWrap) +- [Coverlet](https://github.com/tonerdo/coverlet) ## Donate