From 05fcf12744a84735d69f5733d246c6d8d85d1333 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Thu, 1 Jun 2023 16:03:30 +0200 Subject: [PATCH] Include because+becauseArgs when comparing collections of enums for equivalency --- .../Equivalency/Steps/EnumEqualityStep.cs | 11 +++-- .../EnumSpecs.cs | 48 +++++++++++++++++++ docs/_pages/releases.md | 3 ++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs b/Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs index 48a9110dba..80a7909103 100644 --- a/Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs +++ b/Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs @@ -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); @@ -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: @@ -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); @@ -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); diff --git a/Tests/FluentAssertions.Equivalency.Specs/EnumSpecs.cs b/Tests/FluentAssertions.Equivalency.Specs/EnumSpecs.cs index 3438462db7..93321bdc57 100644 --- a/Tests/FluentAssertions.Equivalency.Specs/EnumSpecs.cs +++ b/Tests/FluentAssertions.Equivalency.Specs/EnumSpecs.cs @@ -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() + .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() + .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() + .WithMessage("*some reason*"); + } + [Fact] public void When_asserting_members_from_different_enum_types_are_equivalent_it_should_compare_by_value_by_default() { diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index 2c8096f1d6..53e87d993c 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -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