Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include because+becauseArgs when comparing collections of enums for equivalency #2214

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class EnumEqualityStep : IEquivalencyStep

bool succeeded = Execute.Assertion
.ForCondition(comparands.Subject?.GetType().IsEnum == true)
.BecauseOf(context.Reason)
.FailWith(() =>
{
decimal? expectationsUnderlyingValue = ExtractDecimal(comparands.Expectation);
Expand All @@ -35,11 +36,11 @@ public class EnumEqualityStep : IEquivalencyStep
switch (context.Options.EnumEquivalencyHandling)
{
case EnumEquivalencyHandling.ByValue:
HandleByValue(comparands);
HandleByValue(comparands, context.Reason);
break;

case EnumEquivalencyHandling.ByName:
HandleByName(comparands);
HandleByName(comparands, context.Reason);
break;

default:
Expand All @@ -50,13 +51,14 @@ public class EnumEqualityStep : IEquivalencyStep
return EquivalencyResult.AssertionCompleted;
}

private static void HandleByValue(Comparands comparands)
private static void HandleByValue(Comparands comparands, Reason reason)
{
decimal? subjectsUnderlyingValue = ExtractDecimal(comparands.Subject);
decimal? expectationsUnderlyingValue = ExtractDecimal(comparands.Expectation);

Execute.Assertion
.ForCondition(subjectsUnderlyingValue == expectationsUnderlyingValue)
.BecauseOf(reason)
.FailWith(() =>
{
string subjectsName = GetDisplayNameForEnumComparison(comparands.Subject, subjectsUnderlyingValue);
Expand All @@ -67,13 +69,14 @@ private static void HandleByValue(Comparands comparands)
});
}

private static void HandleByName(Comparands comparands)
private static void HandleByName(Comparands comparands, Reason reason)
{
string subject = comparands.Subject.ToString();
string expected = comparands.Expectation.ToString();

Execute.Assertion
.ForCondition(subject == expected)
.BecauseOf(reason)
.FailWith(() =>
{
decimal? subjectsUnderlyingValue = ExtractDecimal(comparands.Subject);
Expand Down
48 changes: 48 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/EnumSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,54 @@ public void When_asserting_different_enum_members_are_equivalent_it_should_fail(
.WithMessage("Expected *EnumOne.Two {value: 3}*but*EnumOne.One {value: 0}*");
}

[Fact]
public void Comparing_collections_of_enums_by_value_includes_custom_message()
{
// Arrange
var subject = new[] { EnumOne.One };
var expectation = new[] { EnumOne.Two };

// Act
Action act = () => subject.Should().BeEquivalentTo(expectation, "some {0}", "reason");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected *EnumOne.Two {value: 3}*some reason*but*EnumOne.One {value: 0}*");
}

[Fact]
public void Comparing_collections_of_enums_by_name_includes_custom_message()
{
// Arrange
var subject = new[] { EnumOne.Two };
var expectation = new[] { EnumFour.Three };

// Act
Action act = () => subject.Should().BeEquivalentTo(expectation, config => config.ComparingEnumsByName(),
"some {0}", "reason");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*to equal EnumFour.Three {value: 3} by name*some reason*but found EnumOne.Two {value: 3}*");
}

[Fact]
public void Comparing_collections_of_numerics_with_collections_of_enums_includes_custom_message()
{
// Arrange
var actual = new[] { 1 };

var expected = new[] { TestEnum.First };

// Act
Action act = () => actual.Should().BeEquivalentTo(expected, options => options.ComparingEnumsByValue(),
"some {0}", "reason");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("*some reason*");
}

[Fact]
public void When_asserting_members_from_different_enum_types_are_equivalent_it_should_compare_by_value_by_default()
{
Expand Down
3 changes: 3 additions & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ sidebar:

## Unreleased

### Fixes
* `because` and `becauseArgs` were not included in the error message when collections of enums were not equivalent - [#2214](https://github.com/fluentassertions/fluentassertions/pull/2214)

## 6.11.0

### What's new
Expand Down