Skip to content

Commit

Permalink
Make the naming of the IEquivalencyStep signature
Browse files Browse the repository at this point in the history
The name of EquivalencyResult.AssertionCompleted wasn't clear what it tried to accomplish. This has been improved. Also improved the interface that equivalency steps can use for recursive assertions.
  • Loading branch information
dennisdoomen committed Aug 26, 2024
1 parent c91cc96 commit 47c6269
Show file tree
Hide file tree
Showing 31 changed files with 96 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -377,7 +377,7 @@ public AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(IEnumerable<TExpe
CompileTimeType = typeof(IEnumerable<TExpectation>),
};

new EquivalencyValidator().AssertEquality(comparands, context);
new ValidateChildNodeEquivalency().AssertEquality(comparands, context);

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down Expand Up @@ -913,7 +913,7 @@ public AndWhichConstraint<TAssertions, T> ContainEquivalentOf<TExpectation>(TExp
CompileTimeType = typeof(TExpectation),
};

new EquivalencyValidator().AssertEquality(comparands, context);
new ValidateChildNodeEquivalency().AssertEquality(comparands, context);

string[] failures = scope.Discard();

Expand Down Expand Up @@ -2416,7 +2416,7 @@ public AndConstraint<TAssertions> NotContainEquivalentOf<TExpectation>(TExpectat
CompileTimeType = typeof(TExpectation),
};

new EquivalencyValidator().AssertEquality(comparands, context);
new ValidateChildNodeEquivalency().AssertEquality(comparands, context);

string[] failures = scope.Discard();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -233,7 +233,7 @@ public AndConstraint<TAssertions> BeEquivalentTo<TExpectation>(TExpectation expe
CompileTimeType = typeof(TExpectation),
};

new EquivalencyValidator().AssertEquality(comparands, context);
new ValidateChildNodeEquivalency().AssertEquality(comparands, context);

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -144,7 +144,7 @@ public AndConstraint<TAssertions> BeEquivalentTo(IEnumerable<string> expectation
CompileTimeType = typeof(IEnumerable<string>),
};

new EquivalencyValidator().AssertEquality(comparands, context);
new ValidateChildNodeEquivalency().AssertEquality(comparands, context);

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Equivalency/EquivalencyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ namespace FluentAssertions.Equivalency;
public enum EquivalencyResult
{
ContinueWithNext,
AssertionCompleted
EquivalencyProven
}
8 changes: 4 additions & 4 deletions Src/FluentAssertions/Equivalency/EquivalencyStep.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
namespace FluentAssertions.Equivalency;
namespace FluentAssertions.Equivalency;

/// <summary>
/// Convenient implementation of <see cref="IEquivalencyStep"/> that will only invoke
/// </summary>
public abstract class EquivalencyStep<T> : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
if (!typeof(T).IsAssignableFrom(comparands.GetExpectedType(context.Options)))
{
return EquivalencyResult.ContinueWithNext;
}

return OnHandle(comparands, context, nestedValidator);
return OnHandle(comparands, context, valueChildNodes);
}

/// <summary>
/// Implements <see cref="IEquivalencyStep.Handle"/>, but only gets called when the expected type matches <typeparamref name="T"/>.
/// </summary>
protected abstract EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator);
IValidateChildNodeEquivalency nested);
}
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Equivalency/IEquivalencyStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public interface IEquivalencyStep
/// Executes an operation such as an equivalency assertion on the provided <paramref name="comparands"/>.
/// </summary>
/// <value>
/// Should return <see cref="EquivalencyResult.AssertionCompleted"/> if the subject matches the expectation or if no additional assertions
/// Should return <see cref="EquivalencyResult.EquivalencyProven"/> if the subject matches the expectation or if no additional assertions
/// have to be executed. Should return <see cref="EquivalencyResult.ContinueWithNext"/> otherwise.
/// </value>
/// <remarks>
/// May throw when preconditions are not met or if it detects mismatching data.
/// </remarks>
EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator);
EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IValidateChildNodeEquivalency valueChildNodes);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace FluentAssertions.Equivalency;

public interface IEquivalencyValidator
public interface IValidateChildNodeEquivalency
{
/// <summary>
/// Runs a deep recursive equivalency assertion on the provided <paramref name="comparands"/>.
/// </summary>
void RecursivelyAssertEquality(Comparands comparands, IEquivalencyValidationContext context);
void AssertEquivalencyOf(Comparands comparands, IEquivalencyValidationContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace FluentAssertions.Equivalency;
internal class MultiDimensionalArrayEquivalencyStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
if (comparands.Expectation is not Array expectationAsArray || expectationAsArray.Rank == 1)
{
Expand All @@ -22,7 +22,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
{
if (expectationAsArray.Length == 0)
{
return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

Digit digit = BuildDigitsRepresentingAllIndices(expectationAsArray);
Expand All @@ -36,12 +36,12 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon

IEquivalencyValidationContext itemContext = context.AsCollectionItem<object>(listOfIndices);

nestedValidator.RecursivelyAssertEquality(new Comparands(subject, expectation, typeof(object)), itemContext);
valueChildNodes.AssertEquivalencyOf(new Comparands(subject, expectation, typeof(object)), itemContext);
}
while (digit.Increment());
}

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

private static Digit BuildDigitsRepresentingAllIndices(Array subjectAsArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public AssertionRuleEquivalencyStep(
}

public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
bool success = false;

Expand All @@ -41,7 +41,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
{
// Convert into a child context
context = context.Clone();
converter.Handle(comparands, context, nestedValidator);
converter.Handle(comparands, context, valueChildNodes);
converted = true;
}

Expand All @@ -59,7 +59,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
}
}

return success ? EquivalencyResult.AssertionCompleted : EquivalencyResult.ContinueWithNext;
return success ? EquivalencyResult.EquivalencyProven : EquivalencyResult.ContinueWithNext;
}

private bool AppliesTo(Comparands comparands, INode currentNode) => predicate(new ObjectInfo(comparands, currentNode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace FluentAssertions.Equivalency.Steps;
public class AutoConversionStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
if (!context.Options.ConversionSelector.RequiresConversion(comparands, context.CurrentNode))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DictionaryEquivalencyStep : EquivalencyStep<IDictionary>
{
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency nested)
{
var subject = comparands.Subject as IDictionary;
var expectation = comparands.Expectation as IDictionary;
Expand All @@ -23,7 +23,7 @@ protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalenc
context.Tracer.WriteLine(member =>
Invariant($"Recursing into dictionary item {key} at {member.Description}"));

nestedValidator.RecursivelyAssertEquality(new Comparands(subject[key], expectation[key], typeof(object)),
nested.AssertEquivalencyOf(new Comparands(subject[key], expectation[key], typeof(object)),
context.AsDictionaryItem<object, IDictionary>(key));
}
else
Expand All @@ -37,7 +37,7 @@ protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalenc
}
}

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

private static bool PreconditionsAreMet(IDictionary expectation, IDictionary subject)
Expand Down
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Equivalency/Steps/EnumEqualityStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace FluentAssertions.Equivalency.Steps;
public class EnumEqualityStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
if (!comparands.GetExpectedType(context.Options).IsEnum)
{
Expand Down Expand Up @@ -48,7 +48,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
}
}

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

private static void HandleByValue(Comparands comparands, Reason reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace FluentAssertions.Equivalency.Steps;
public class EnumerableEquivalencyStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
if (!IsCollection(comparands.GetExpectedType(context.Options)))
{
Expand All @@ -17,7 +17,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon

if (AssertSubjectIsCollection(comparands.Subject))
{
var validator = new EnumerableEquivalencyValidator(nestedValidator, context)
var validator = new EnumerableEquivalencyValidator(valueChildNodes, context)
{
Recursive = context.CurrentNode.IsRoot || context.Options.IsRecursive,
OrderingRules = context.Options.OrderingRules
Expand All @@ -26,7 +26,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
validator.Execute(ToArray(comparands.Subject), ToArray(comparands.Expectation));
}

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

private static bool AssertSubjectIsCollection(object subject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ internal class EnumerableEquivalencyValidator

#region Private Definitions

private readonly IEquivalencyValidator parent;
private readonly IValidateChildNodeEquivalency parent;
private readonly IEquivalencyValidationContext context;

#endregion

public EnumerableEquivalencyValidator(IEquivalencyValidator parent, IEquivalencyValidationContext context)
public EnumerableEquivalencyValidator(IValidateChildNodeEquivalency parent, IEquivalencyValidationContext context)
{
this.parent = parent;
this.context = context;
Expand Down Expand Up @@ -195,7 +195,7 @@ private string[] TryToMatch<T>(object subject, T expectation, int expectationInd
{
using var scope = new AssertionScope();

parent.RecursivelyAssertEquality(new Comparands(subject, expectation, typeof(T)),
parent.AssertEquivalencyOf(new Comparands(subject, expectation, typeof(T)),
context.AsCollectionItem<T>(expectationIndex));

return scope.Discard();
Expand All @@ -207,7 +207,7 @@ private bool StrictlyMatchAgainst<T>(object[] subjects, T expectation, int expec
object subject = subjects[expectationIndex];
IEquivalencyValidationContext equivalencyValidationContext = context.AsCollectionItem<T>(expectationIndex);

parent.RecursivelyAssertEquality(new Comparands(subject, expectation, typeof(T)), equivalencyValidationContext);
parent.AssertEquivalencyOf(new Comparands(subject, expectation, typeof(T)), equivalencyValidationContext);

bool failed = scope.HasFailures();
return !failed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public EqualityComparerEquivalencyStep(IEqualityComparer<T> comparer)
}

public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
var expectedType = context.Options.UseRuntimeTyping ? comparands.RuntimeType : comparands.CompileTimeType;

Expand All @@ -39,7 +39,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
.FailWith("Expected {context:object} to be equal to {1} according to {0}{because}, but {2} was not.",
comparer.ToString(), comparands.Expectation, comparands.Subject);

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class GenericDictionaryEquivalencyStep : IEquivalencyStep
{
#pragma warning disable SA1110 // Allow opening parenthesis on new line to reduce line length
private static readonly MethodInfo AssertDictionaryEquivalenceMethod =
new Action<EquivalencyValidationContext, IEquivalencyValidator, IEquivalencyOptions,
new Action<EquivalencyValidationContext, IValidateChildNodeEquivalency, IEquivalencyOptions,
IDictionary<object, object>, IDictionary<object, object>>
(AssertDictionaryEquivalence).GetMethodInfo().GetGenericMethodDefinition();
#pragma warning restore SA1110

public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
if (comparands.Expectation is null)
{
Expand All @@ -40,10 +40,10 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
if (IsNotNull(comparands.Subject)
&& EnsureSubjectIsOfTheExpectedDictionaryType(comparands, expectedDictionary) is { } actualDictionary)
{
AssertDictionaryEquivalence(comparands, context, nestedValidator, actualDictionary, expectedDictionary);
AssertDictionaryEquivalence(comparands, context, valueChildNodes, actualDictionary, expectedDictionary);
}

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

private static bool IsNonGenericDictionary(object subject)
Expand Down Expand Up @@ -148,7 +148,7 @@ private static void FailWithLengthDifference<TSubjectKey, TSubjectValue, TExpect
}

private static void AssertDictionaryEquivalence(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator parent, DictionaryInterfaceInfo actualDictionary, DictionaryInterfaceInfo expectedDictionary)
IValidateChildNodeEquivalency parent, DictionaryInterfaceInfo actualDictionary, DictionaryInterfaceInfo expectedDictionary)
{
AssertDictionaryEquivalenceMethod
.MakeGenericMethod(actualDictionary.Key, actualDictionary.Value, expectedDictionary.Key, expectedDictionary.Value)
Expand All @@ -157,7 +157,7 @@ private static void AssertDictionaryEquivalence(Comparands comparands, IEquivale

private static void AssertDictionaryEquivalence<TSubjectKey, TSubjectValue, TExpectedKey, TExpectedValue>(
EquivalencyValidationContext context,
IEquivalencyValidator parent,
IValidateChildNodeEquivalency parent,
IEquivalencyOptions options,
IDictionary<TSubjectKey, TSubjectValue> subject,
IDictionary<TExpectedKey, TExpectedValue> expectation)
Expand All @@ -180,7 +180,7 @@ private static void AssertDictionaryEquivalence<TSubjectKey, TSubjectValue, TExp
{
var nestedComparands = new Comparands(subject[key], expectation[key], typeof(TExpectedValue));

parent.RecursivelyAssertEquality(nestedComparands,
parent.AssertEquivalencyOf(nestedComparands,
context.AsDictionaryItem<TExpectedKey, TExpectedValue>(key));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GenericEnumerableEquivalencyStep : IEquivalencyStep
#pragma warning restore SA1110

public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context,
IEquivalencyValidator nestedValidator)
IValidateChildNodeEquivalency valueChildNodes)
{
Type expectedType = comparands.GetExpectedType(context.Options);

Expand All @@ -35,7 +35,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon

if (AssertSubjectIsCollection(comparands.Subject))
{
var validator = new EnumerableEquivalencyValidator(nestedValidator, context)
var validator = new EnumerableEquivalencyValidator(valueChildNodes, context)
{
Recursive = context.CurrentNode.IsRoot || context.Options.IsRecursive,
OrderingRules = context.Options.OrderingRules
Expand All @@ -56,7 +56,7 @@ public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationCon
}
}

return EquivalencyResult.AssertionCompleted;
return EquivalencyResult.EquivalencyProven;
}

private static void HandleImpl<T>(EnumerableEquivalencyValidator validator, object[] subject, IEnumerable<T> expectation) =>
Expand Down
Loading

0 comments on commit 47c6269

Please sign in to comment.