diff --git a/Src/FluentAssertions/Equivalency/Execution/CollectionMemberAssertionOptionsDecorator.cs b/Src/FluentAssertions/Equivalency/Execution/CollectionMemberAssertionOptionsDecorator.cs index 426d50959e..157f0394b1 100644 --- a/Src/FluentAssertions/Equivalency/Execution/CollectionMemberAssertionOptionsDecorator.cs +++ b/Src/FluentAssertions/Equivalency/Execution/CollectionMemberAssertionOptionsDecorator.cs @@ -65,7 +65,7 @@ public IEnumerable UserEquivalencySteps public bool ExcludeNonBrowsableOnExpectation => inner.ExcludeNonBrowsableOnExpectation; - public bool CompareRecordsByValue => inner.CompareRecordsByValue; + public bool? CompareRecordsByValue => inner.CompareRecordsByValue; public EqualityStrategy GetEqualityStrategy(Type type) { diff --git a/Src/FluentAssertions/Equivalency/IEquivalencyAssertionOptions.cs b/Src/FluentAssertions/Equivalency/IEquivalencyAssertionOptions.cs index 2df309e111..01f3406002 100644 --- a/Src/FluentAssertions/Equivalency/IEquivalencyAssertionOptions.cs +++ b/Src/FluentAssertions/Equivalency/IEquivalencyAssertionOptions.cs @@ -88,7 +88,7 @@ public interface IEquivalencyAssertionOptions /// /// Gets a value indicating whether records should be compared by value instead of their members /// - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } /// /// Gets the currently configured tracer, or null if no tracing was configured. diff --git a/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs b/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs index d1105e5c1e..ba3b13701d 100644 --- a/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs +++ b/Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs @@ -60,7 +60,7 @@ public abstract class SelfReferenceEquivalencyAssertionOptions : IEquival private bool ignoreNonBrowsableOnSubject; private bool excludeNonBrowsableOnExpectation; - private bool compareRecordsByValue; + private bool? compareRecordsByValue; #endregion @@ -176,7 +176,7 @@ IEnumerable IEquivalencyAssertionOptions.SelectionRules bool IEquivalencyAssertionOptions.ExcludeNonBrowsableOnExpectation => excludeNonBrowsableOnExpectation; - public bool CompareRecordsByValue => compareRecordsByValue; + public bool? CompareRecordsByValue => compareRecordsByValue; EqualityStrategy IEquivalencyAssertionOptions.GetEqualityStrategy(Type requestedType) { @@ -202,9 +202,9 @@ EqualityStrategy IEquivalencyAssertionOptions.GetEqualityStrategy(Type requested { strategy = EqualityStrategy.ForceEquals; } - else if (type.IsRecord()) + else if ((compareRecordsByValue.HasValue || getDefaultEqualityStrategy is null) && type.IsRecord()) { - strategy = compareRecordsByValue ? EqualityStrategy.ForceEquals : EqualityStrategy.ForceMembers; + strategy = compareRecordsByValue is true ? EqualityStrategy.ForceEquals : EqualityStrategy.ForceMembers; } else { @@ -760,7 +760,7 @@ public override string ToString() builder.AppendLine($"- Compare tuples by their properties"); builder.AppendLine($"- Compare anonymous types by their properties"); - if (compareRecordsByValue) + if (compareRecordsByValue is true) { builder.AppendLine("- Compare records by value"); } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt index b5bb8c5428..6b925ee4d2 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt @@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency public interface IEquivalencyAssertionOptions { bool AllowInfiniteRecursion { get; } - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; } FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; } @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions { protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { } - public bool CompareRecordsByValue { get; } + public bool? CompareRecordsByValue { get; } public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net6.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net6.0.verified.txt index 8dd2790649..5f70443868 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net6.0.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net6.0.verified.txt @@ -877,7 +877,7 @@ namespace FluentAssertions.Equivalency public interface IEquivalencyAssertionOptions { bool AllowInfiniteRecursion { get; } - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; } FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; } @@ -1040,7 +1040,7 @@ namespace FluentAssertions.Equivalency where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions { protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { } - public bool CompareRecordsByValue { get; } + public bool? CompareRecordsByValue { get; } public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt index 44e2750782..6bfee8d9a2 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt @@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency public interface IEquivalencyAssertionOptions { bool AllowInfiniteRecursion { get; } - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; } FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; } @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions { protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { } - public bool CompareRecordsByValue { get; } + public bool? CompareRecordsByValue { get; } public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt index 1f8debcc1e..147106a2f5 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt @@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency public interface IEquivalencyAssertionOptions { bool AllowInfiniteRecursion { get; } - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; } FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; } @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions { protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { } - public bool CompareRecordsByValue { get; } + public bool? CompareRecordsByValue { get; } public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt index d86a70b6c6..d62701262e 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt @@ -857,7 +857,7 @@ namespace FluentAssertions.Equivalency public interface IEquivalencyAssertionOptions { bool AllowInfiniteRecursion { get; } - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; } FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; } @@ -1020,7 +1020,7 @@ namespace FluentAssertions.Equivalency where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions { protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { } - public bool CompareRecordsByValue { get; } + public bool? CompareRecordsByValue { get; } public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt index e025395e17..e7d1464292 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt @@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency public interface IEquivalencyAssertionOptions { bool AllowInfiniteRecursion { get; } - bool CompareRecordsByValue { get; } + bool? CompareRecordsByValue { get; } FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; } FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; } @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions { protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { } - public bool CompareRecordsByValue { get; } + public bool? CompareRecordsByValue { get; } public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; } [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; } diff --git a/Tests/Benchmarks/UsersOfGetClosedGenericInterfaces.cs b/Tests/Benchmarks/UsersOfGetClosedGenericInterfaces.cs index 9ae7beffdc..455e11c08e 100644 --- a/Tests/Benchmarks/UsersOfGetClosedGenericInterfaces.cs +++ b/Tests/Benchmarks/UsersOfGetClosedGenericInterfaces.cs @@ -72,7 +72,7 @@ private class Config : IEquivalencyAssertionOptions public bool ExcludeNonBrowsableOnExpectation => throw new NotImplementedException(); - public bool CompareRecordsByValue => throw new NotImplementedException(); + public bool? CompareRecordsByValue => throw new NotImplementedException(); public ITraceWriter TraceWriter => throw new NotImplementedException(); diff --git a/Tests/FluentAssertions.Specs/AssertionOptionsSpecs.cs b/Tests/FluentAssertions.Specs/AssertionOptionsSpecs.cs index 0750a73867..5e7aaf4429 100644 --- a/Tests/FluentAssertions.Specs/AssertionOptionsSpecs.cs +++ b/Tests/FluentAssertions.Specs/AssertionOptionsSpecs.cs @@ -127,6 +127,34 @@ internal class MyClass } } + public class When_modifying_record_settings_globally : Given_temporary_global_assertion_options + { + public When_modifying_record_settings_globally() + { + When(() => + { + AssertionOptions.AssertEquivalencyUsing( + options => options.ComparingByValue(typeof(Position))); + }); + } + + [Fact] + public void It_should_use_the_global_settings_for_comparing_records() + { + new Position(123).Should().BeEquivalentTo(new Position(123)); + } + + private record Position + { + private readonly int value; + + public Position(int value) + { + this.value = value; + } + } + } + [Collection("AssertionOptionsSpecs")] public class When_assertion_doubles_should_always_allow_small_deviations : Given_temporary_global_assertion_options diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index eb74a03506..f9e118b800 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -18,6 +18,7 @@ sidebar: ### Fixes * Fixed `For`/`Exclude` not excluding properties in objects in a collection - [#1953](https://github.com/fluentassertions/fluentassertions/pull/1953) * Changed `MatchEquivalentOf` to use `CultureInfo.InvariantCulture` instead of `CultureInfo.CurrentCulture` - [#1985](https://github.com/fluentassertions/fluentassertions/pull/1985). +* Fixes `BeEquivalentTo` not taking into account any `record` equivalency settings coming from the `AssertionOptions` - [#1984](https://github.com/fluentassertions/fluentassertions/pull/1984) ## 6.7.0