Skip to content

Commit

Permalink
Include because+becauseArgs when comparing collections of enums for e…
Browse files Browse the repository at this point in the history
…quivalency
  • Loading branch information
jnyrup committed Jun 1, 2023
1 parent cd88618 commit 05fcf12
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs
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);
break;

case EnumEquivalencyHandling.ByName:
HandleByName(comparands);
HandleByName(comparands, context);
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, IEquivalencyValidationContext context)
{
decimal? subjectsUnderlyingValue = ExtractDecimal(comparands.Subject);
decimal? expectationsUnderlyingValue = ExtractDecimal(comparands.Expectation);

Execute.Assertion
.ForCondition(subjectsUnderlyingValue == expectationsUnderlyingValue)
.BecauseOf(context.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, IEquivalencyValidationContext context)
{
string subject = comparands.Subject.ToString();
string expected = comparands.Expectation.ToString();

Execute.Assertion
.ForCondition(subject == expected)
.BecauseOf(context.Reason)
.FailWith(() =>
{
decimal? subjectsUnderlyingValue = ExtractDecimal(comparands.Subject);
Expand Down
48 changes: 48 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/EnumSpecs.cs
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
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

0 comments on commit 05fcf12

Please sign in to comment.