mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Use FluentAssertions
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="5.7.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||
<PackageReference Include="NUnit" Version="3.12.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">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<MissingCommandOptionException>(() => initializer.InitializeCommand(command, schema, commandInput));
|
||||
|
||||
// Act & Assert
|
||||
initializer.Invoking(i => i.InitializeCommand(command, schema, commandInput))
|
||||
.Should().ThrowExactly<MissingCommandOptionException>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user