Use FluentAssertions

This commit is contained in:
Alexey Golub
2019-07-26 17:39:28 +03:00
parent d72391df1f
commit 5bb175fd4b
13 changed files with 36 additions and 159 deletions

View File

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Exceptions; using CliFx.Exceptions;
using CliFx.Models; using CliFx.Models;
using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -209,7 +210,7 @@ namespace CliFx.Tests
var exitCodeValue = await application.RunAsync(commandLineArguments); var exitCodeValue = await application.RunAsync(commandLineArguments);
// Assert // Assert
Assert.That(exitCodeValue, Is.Zero, "Exit code"); exitCodeValue.Should().Be(0);
} }
[Test] [Test]
@@ -223,7 +224,7 @@ namespace CliFx.Tests
var exitCodeValue = await application.RunAsync(commandLineArguments); var exitCodeValue = await application.RunAsync(commandLineArguments);
// Assert // Assert
Assert.That(exitCodeValue, Is.Not.Zero, "Exit code"); exitCodeValue.Should().NotBe(0);
} }
} }
} }

View File

@@ -11,10 +11,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="NUnit" Version="3.12.0" /> <PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" /> <PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="CliWrap" Version="2.3.0" /> <PackageReference Include="CliWrap" Version="2.3.1" />
<PackageReference Include="coverlet.msbuild" Version="2.6.3"> <PackageReference Include="coverlet.msbuild" Version="2.6.3">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Models; using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -31,13 +32,13 @@ namespace CliFx.Tests
{ {
// Arrange // Arrange
var factory = new CommandFactory(); var factory = new CommandFactory();
var schema = new CommandSchemaResolver().GetCommandSchema(commandType);
// Act // Act
var schema = new CommandSchemaResolver().GetCommandSchema(commandType);
var command = factory.CreateCommand(schema); var command = factory.CreateCommand(schema);
// Assert // Assert
Assert.That(command, Is.TypeOf(commandType)); command.Should().BeOfType(commandType);
} }
} }
} }

View File

@@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Exceptions; using CliFx.Exceptions;
using CliFx.Models; using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -12,7 +14,9 @@ namespace CliFx.Tests
public partial class CommandInitializerTests public partial class CommandInitializerTests
{ {
[Command] [Command]
public class TestCommand : ICommand [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Local")]
private class TestCommand : ICommand
{ {
[CommandOption("int", 'i', IsRequired = true)] [CommandOption("int", 'i', IsRequired = true)]
public int IntOption { get; set; } = 24; public int IntOption { get; set; } = 24;
@@ -107,23 +111,18 @@ namespace CliFx.Tests
[Test] [Test]
[TestCaseSource(nameof(GetTestCases_InitializeCommand))] [TestCaseSource(nameof(GetTestCases_InitializeCommand))]
public void InitializeCommand_Test(CommandInput commandInput, TestCommand expectedCommand) public void InitializeCommand_Test(CommandInput commandInput, ICommand expectedCommand)
{ {
// Arrange // Arrange
var initializer = new CommandInitializer(); var initializer = new CommandInitializer();
// Act
var schema = new CommandSchemaResolver().GetCommandSchema(typeof(TestCommand)); var schema = new CommandSchemaResolver().GetCommandSchema(typeof(TestCommand));
var command = new TestCommand(); var command = new TestCommand();
// Act
initializer.InitializeCommand(command, schema, commandInput); initializer.InitializeCommand(command, schema, commandInput);
// Assert // Assert
Assert.Multiple(() => command.Should().BeEquivalentTo(expectedCommand, o => o.RespectingRuntimeTypes());
{
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));
});
} }
[Test] [Test]
@@ -132,11 +131,12 @@ namespace CliFx.Tests
{ {
// Arrange // Arrange
var initializer = new CommandInitializer(); var initializer = new CommandInitializer();
// Act & Assert
var schema = new CommandSchemaResolver().GetCommandSchema(typeof(TestCommand)); var schema = new CommandSchemaResolver().GetCommandSchema(typeof(TestCommand));
var command = new TestCommand(); var command = new TestCommand();
Assert.Throws<MissingCommandOptionException>(() => initializer.InitializeCommand(command, schema, commandInput));
// Act & Assert
initializer.Invoking(i => i.InitializeCommand(command, schema, commandInput))
.Should().ThrowExactly<MissingCommandOptionException>();
} }
} }
} }

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using CliFx.Models; using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using CliFx.Tests.Internal; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -170,7 +170,7 @@ namespace CliFx.Tests
var commandInput = parser.ParseInput(commandLineArguments); var commandInput = parser.ParseInput(commandLineArguments);
// Assert // Assert
Assert.That(commandInput, Is.EqualTo(expectedCommandInput).Using(CommandInputEqualityComparer.Instance)); commandInput.Should().BeEquivalentTo(expectedCommandInput);
} }
} }
} }

View File

@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using CliFx.Models; using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -212,13 +213,10 @@ namespace CliFx.Tests
var convertedValue = converter.ConvertOption(optionInput, targetType); var convertedValue = converter.ConvertOption(optionInput, targetType);
// Assert // Assert
Assert.Multiple(() => convertedValue.Should().BeEquivalentTo(expectedConvertedValue);
{
Assert.That(convertedValue, Is.EqualTo(expectedConvertedValue));
if (convertedValue != null) if (convertedValue != null)
Assert.That(convertedValue, Is.AssignableTo(targetType)); convertedValue.Should().BeAssignableTo(targetType);
});
} }
} }
} }

View File

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Models; using CliFx.Models;
using CliFx.Services; using CliFx.Services;
using CliFx.Tests.Internal; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -61,7 +61,7 @@ namespace CliFx.Tests
var schema = resolver.GetCommandSchema(commandType); var schema = resolver.GetCommandSchema(commandType);
// Assert // Assert
Assert.That(schema, Is.EqualTo(expectedSchema).Using(CommandSchemaEqualityComparer.Instance)); schema.Should().BeEquivalentTo(expectedSchema);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using CliWrap; using CliWrap;
using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
namespace CliFx.Tests namespace CliFx.Tests
@@ -31,7 +32,7 @@ namespace CliFx.Tests
.ExecuteAsync(); .ExecuteAsync();
// Assert // Assert
Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput), "Stdout"); result.StandardOutput.Trim().Should().Be(expectedOutput);
} }
[Test] [Test]
@@ -46,7 +47,7 @@ namespace CliFx.Tests
.ExecuteAsync(); .ExecuteAsync();
// Assert // Assert
Assert.That(result.StandardOutput.Trim(), Is.EqualTo(DummyVersionText), "Stdout"); result.StandardOutput.Trim().Should().Be(DummyVersionText);
} }
[Test] [Test]
@@ -66,7 +67,7 @@ namespace CliFx.Tests
.ExecuteAsync(); .ExecuteAsync();
// Assert // Assert
Assert.That(result.StandardOutput.Trim(), Is.Not.Empty, "Stdout"); result.StandardOutput.Trim().Should().NotBeNullOrWhiteSpace();
} }
} }
} }

View File

@@ -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<CommandInput>
{
/// <inheritdoc />
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);
}
/// <inheritdoc />
public int GetHashCode(CommandInput obj) => throw new NotSupportedException();
}
public partial class CommandInputEqualityComparer
{
public static CommandInputEqualityComparer Instance { get; } = new CommandInputEqualityComparer();
}
}

View File

@@ -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<CommandOptionInput>
{
/// <inheritdoc />
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);
}
/// <inheritdoc />
public int GetHashCode(CommandOptionInput obj) => throw new NotSupportedException();
}
public partial class CommandOptionInputEqualityComparer
{
public static CommandOptionInputEqualityComparer Instance { get; } = new CommandOptionInputEqualityComparer();
}
}

View File

@@ -1,33 +0,0 @@
using System;
using System.Collections.Generic;
using CliFx.Models;
namespace CliFx.Tests.Internal
{
public partial class CommandOptionSchemaEqualityComparer : IEqualityComparer<CommandOptionSchema>
{
/// <inheritdoc />
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);
}
/// <inheritdoc />
public int GetHashCode(CommandOptionSchema obj) => throw new NotSupportedException();
}
public partial class CommandOptionSchemaEqualityComparer
{
public static CommandOptionSchemaEqualityComparer Instance { get; } = new CommandOptionSchemaEqualityComparer();
}
}

View File

@@ -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<CommandSchema>
{
/// <inheritdoc />
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);
}
/// <inheritdoc />
public int GetHashCode(CommandSchema obj) => throw new NotSupportedException();
}
public partial class CommandSchemaEqualityComparer
{
public static CommandSchemaEqualityComparer Instance { get; } = new CommandSchemaEqualityComparer();
}
}

View File

@@ -28,6 +28,9 @@ To be added with a stable release...
## Libraries used ## Libraries used
- [NUnit](https://github.com/nunit/nunit) - [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 ## Donate