Skip to content

Commit

Permalink
Global record equivalency settings are now taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Sep 4, 2022
1 parent 58b05b8 commit f100196
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IEnumerable<IEquivalencyStep> UserEquivalencySteps

public bool ExcludeNonBrowsableOnExpectation => inner.ExcludeNonBrowsableOnExpectation;

public bool CompareRecordsByValue => inner.CompareRecordsByValue;
public bool? CompareRecordsByValue => inner.CompareRecordsByValue;

public EqualityStrategy GetEqualityStrategy(Type type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public interface IEquivalencyAssertionOptions
/// <summary>
/// Gets a value indicating whether records should be compared by value instead of their members
/// </summary>
bool CompareRecordsByValue { get; }
bool? CompareRecordsByValue { get; }

/// <summary>
/// Gets the currently configured tracer, or <c>null</c> if no tracing was configured.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract class SelfReferenceEquivalencyAssertionOptions<TSelf> : IEquival
private bool ignoreNonBrowsableOnSubject;
private bool excludeNonBrowsableOnExpectation;

private bool compareRecordsByValue;
private bool? compareRecordsByValue;

#endregion

Expand Down Expand Up @@ -176,7 +176,7 @@ IEnumerable<IMemberSelectionRule> IEquivalencyAssertionOptions.SelectionRules

bool IEquivalencyAssertionOptions.ExcludeNonBrowsableOnExpectation => excludeNonBrowsableOnExpectation;

public bool CompareRecordsByValue => compareRecordsByValue;
public bool? CompareRecordsByValue => compareRecordsByValue;

EqualityStrategy IEquivalencyAssertionOptions.GetEqualityStrategy(Type requestedType)
{
Expand All @@ -202,9 +202,9 @@ EqualityStrategy IEquivalencyAssertionOptions.GetEqualityStrategy(Type requested
{
strategy = EqualityStrategy.ForceEquals;
}
else if (type.IsRecord())
else if (type.IsRecord() && (compareRecordsByValue.HasValue || getDefaultEqualityStrategy is null))
{
strategy = compareRecordsByValue ? EqualityStrategy.ForceEquals : EqualityStrategy.ForceMembers;
strategy = compareRecordsByValue is true ? EqualityStrategy.ForceEquals : EqualityStrategy.ForceMembers;
}
else
{
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
{
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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -1040,7 +1040,7 @@ namespace FluentAssertions.Equivalency
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
{
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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
{
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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
{
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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -1020,7 +1020,7 @@ namespace FluentAssertions.Equivalency
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
{
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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -1027,7 +1027,7 @@ namespace FluentAssertions.Equivalency
where TSelf : FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions<TSelf>
{
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; }
Expand Down
2 changes: 1 addition & 1 deletion Tests/Benchmarks/UsersOfGetClosedGenericInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
28 changes: 28 additions & 0 deletions Tests/FluentAssertions.Specs/AssertionOptionsSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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
Expand Down

0 comments on commit f100196

Please sign in to comment.