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 40838be commit de7ab9a
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 20 deletions.
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
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
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 ((compareRecordsByValue.HasValue || getDefaultEqualityStrategy is null) && type.IsRecord())
{
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
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
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
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
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
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
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
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
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Expand Up @@ -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

Expand Down

0 comments on commit de7ab9a

Please sign in to comment.