Skip to content

Commit f100196

Browse files
committed
Global record equivalency settings are now taken into account
1 parent 58b05b8 commit f100196

11 files changed

+48
-20
lines changed

Src/FluentAssertions/Equivalency/Execution/CollectionMemberAssertionOptionsDecorator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public IEnumerable<IEquivalencyStep> UserEquivalencySteps
6565

6666
public bool ExcludeNonBrowsableOnExpectation => inner.ExcludeNonBrowsableOnExpectation;
6767

68-
public bool CompareRecordsByValue => inner.CompareRecordsByValue;
68+
public bool? CompareRecordsByValue => inner.CompareRecordsByValue;
6969

7070
public EqualityStrategy GetEqualityStrategy(Type type)
7171
{

Src/FluentAssertions/Equivalency/IEquivalencyAssertionOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public interface IEquivalencyAssertionOptions
8888
/// <summary>
8989
/// Gets a value indicating whether records should be compared by value instead of their members
9090
/// </summary>
91-
bool CompareRecordsByValue { get; }
91+
bool? CompareRecordsByValue { get; }
9292

9393
/// <summary>
9494
/// Gets the currently configured tracer, or <c>null</c> if no tracing was configured.

Src/FluentAssertions/Equivalency/SelfReferenceEquivalencyAssertionOptions.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public abstract class SelfReferenceEquivalencyAssertionOptions<TSelf> : IEquival
6060
private bool ignoreNonBrowsableOnSubject;
6161
private bool excludeNonBrowsableOnExpectation;
6262

63-
private bool compareRecordsByValue;
63+
private bool? compareRecordsByValue;
6464

6565
#endregion
6666

@@ -176,7 +176,7 @@ IEnumerable<IMemberSelectionRule> IEquivalencyAssertionOptions.SelectionRules
176176

177177
bool IEquivalencyAssertionOptions.ExcludeNonBrowsableOnExpectation => excludeNonBrowsableOnExpectation;
178178

179-
public bool CompareRecordsByValue => compareRecordsByValue;
179+
public bool? CompareRecordsByValue => compareRecordsByValue;
180180

181181
EqualityStrategy IEquivalencyAssertionOptions.GetEqualityStrategy(Type requestedType)
182182
{
@@ -202,9 +202,9 @@ EqualityStrategy IEquivalencyAssertionOptions.GetEqualityStrategy(Type requested
202202
{
203203
strategy = EqualityStrategy.ForceEquals;
204204
}
205-
else if (type.IsRecord())
205+
else if (type.IsRecord() && (compareRecordsByValue.HasValue || getDefaultEqualityStrategy is null))
206206
{
207-
strategy = compareRecordsByValue ? EqualityStrategy.ForceEquals : EqualityStrategy.ForceMembers;
207+
strategy = compareRecordsByValue is true ? EqualityStrategy.ForceEquals : EqualityStrategy.ForceMembers;
208208
}
209209
else
210210
{
@@ -760,7 +760,7 @@ public override string ToString()
760760
builder.AppendLine($"- Compare tuples by their properties");
761761
builder.AppendLine($"- Compare anonymous types by their properties");
762762

763-
if (compareRecordsByValue)
763+
if (compareRecordsByValue is true)
764764
{
765765
builder.AppendLine("- Compare records by value");
766766
}

Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency
864864
public interface IEquivalencyAssertionOptions
865865
{
866866
bool AllowInfiniteRecursion { get; }
867-
bool CompareRecordsByValue { get; }
867+
bool? CompareRecordsByValue { get; }
868868
FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
869869
FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; }
870870
FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; }
@@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
10271027
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
10281028
{
10291029
protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { }
1030-
public bool CompareRecordsByValue { get; }
1030+
public bool? CompareRecordsByValue { get; }
10311031
public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
10321032
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
10331033
protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; }

Tests/Approval.Tests/ApprovedApi/FluentAssertions/net6.0.verified.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ namespace FluentAssertions.Equivalency
877877
public interface IEquivalencyAssertionOptions
878878
{
879879
bool AllowInfiniteRecursion { get; }
880-
bool CompareRecordsByValue { get; }
880+
bool? CompareRecordsByValue { get; }
881881
FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
882882
FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; }
883883
FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; }
@@ -1040,7 +1040,7 @@ namespace FluentAssertions.Equivalency
10401040
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
10411041
{
10421042
protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { }
1043-
public bool CompareRecordsByValue { get; }
1043+
public bool? CompareRecordsByValue { get; }
10441044
public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
10451045
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
10461046
protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; }

Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency
864864
public interface IEquivalencyAssertionOptions
865865
{
866866
bool AllowInfiniteRecursion { get; }
867-
bool CompareRecordsByValue { get; }
867+
bool? CompareRecordsByValue { get; }
868868
FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
869869
FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; }
870870
FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; }
@@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
10271027
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
10281028
{
10291029
protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { }
1030-
public bool CompareRecordsByValue { get; }
1030+
public bool? CompareRecordsByValue { get; }
10311031
public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
10321032
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
10331033
protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; }

Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency
864864
public interface IEquivalencyAssertionOptions
865865
{
866866
bool AllowInfiniteRecursion { get; }
867-
bool CompareRecordsByValue { get; }
867+
bool? CompareRecordsByValue { get; }
868868
FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
869869
FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; }
870870
FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; }
@@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
10271027
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
10281028
{
10291029
protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { }
1030-
public bool CompareRecordsByValue { get; }
1030+
public bool? CompareRecordsByValue { get; }
10311031
public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
10321032
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
10331033
protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; }

Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ namespace FluentAssertions.Equivalency
857857
public interface IEquivalencyAssertionOptions
858858
{
859859
bool AllowInfiniteRecursion { get; }
860-
bool CompareRecordsByValue { get; }
860+
bool? CompareRecordsByValue { get; }
861861
FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
862862
FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; }
863863
FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; }
@@ -1020,7 +1020,7 @@ namespace FluentAssertions.Equivalency
10201020
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
10211021
{
10221022
protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { }
1023-
public bool CompareRecordsByValue { get; }
1023+
public bool? CompareRecordsByValue { get; }
10241024
public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
10251025
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
10261026
protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; }

Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ namespace FluentAssertions.Equivalency
864864
public interface IEquivalencyAssertionOptions
865865
{
866866
bool AllowInfiniteRecursion { get; }
867-
bool CompareRecordsByValue { get; }
867+
bool? CompareRecordsByValue { get; }
868868
FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
869869
FluentAssertions.Equivalency.CyclicReferenceHandling CyclicReferenceHandling { get; }
870870
FluentAssertions.Equivalency.EnumEquivalencyHandling EnumEquivalencyHandling { get; }
@@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
10271027
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
10281028
{
10291029
protected SelfReferenceEquivalencyAssertionOptions(FluentAssertions.Equivalency.IEquivalencyAssertionOptions defaults) { }
1030-
public bool CompareRecordsByValue { get; }
1030+
public bool? CompareRecordsByValue { get; }
10311031
public FluentAssertions.Equivalency.ConversionSelector ConversionSelector { get; }
10321032
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
10331033
protected FluentAssertions.Equivalency.OrderingRuleCollection OrderingRules { get; }

Tests/Benchmarks/UsersOfGetClosedGenericInterfaces.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private class Config : IEquivalencyAssertionOptions
7272

7373
public bool ExcludeNonBrowsableOnExpectation => throw new NotImplementedException();
7474

75-
public bool CompareRecordsByValue => throw new NotImplementedException();
75+
public bool? CompareRecordsByValue => throw new NotImplementedException();
7676

7777
public ITraceWriter TraceWriter => throw new NotImplementedException();
7878

Tests/FluentAssertions.Specs/AssertionOptionsSpecs.cs

+28
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ internal class MyClass
123123
}
124124
}
125125

126+
public class When_modifying_record_settings_globally : Given_temporary_global_assertion_options
127+
{
128+
public When_modifying_record_settings_globally()
129+
{
130+
When(() =>
131+
{
132+
AssertionOptions.AssertEquivalencyUsing(
133+
options => options.ComparingByValue(typeof(Position)));
134+
});
135+
}
136+
137+
[Fact]
138+
public void It_should_use_the_global_settings_for_comparing_records()
139+
{
140+
new Position(123).Should().BeEquivalentTo(new Position(123));
141+
}
142+
143+
private record Position
144+
{
145+
private readonly int value;
146+
147+
public Position(int value)
148+
{
149+
this.value = value;
150+
}
151+
}
152+
}
153+
126154
[Collection("AssertionOptionsSpecs")]
127155
public class When_assertion_doubles_should_always_allow_small_deviations :
128156
Given_temporary_global_assertion_options

0 commit comments

Comments
 (0)