Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
pentp committed Nov 20, 2018
1 parent b8c7c7a commit bf43168
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
Expand Up @@ -53,7 +53,7 @@ public void Execute<T>(object[] subject, T[] expectation)

private bool AssertIsNotNull(object expectation, object[] subject)
{
return expectation != null || AssertionScope.Current
return AssertionScope.Current
.ForCondition(!(expectation is null))
.FailWith("Expected {context:subject} to be <null>, but found {0}.", new object[] { subject });
}
Expand Down
Expand Up @@ -13,6 +13,12 @@ namespace FluentAssertions.Equivalency
/// </remarks>
public class GenericDictionaryEquivalencyStep : IEquivalencyStep
{
private static readonly MethodInfo AssertSameLengthMethod = new Func<IDictionary<object, object>, IDictionary<object, object>, bool>
(AssertSameLength).GetMethodInfo().GetGenericMethodDefinition();

private static readonly MethodInfo AssertDictionaryEquivalenceMethod = new Action<EquivalencyValidationContext, IEquivalencyValidator, IEquivalencyAssertionOptions, IDictionary<object, object>, IDictionary<object, object>>
(AssertDictionaryEquivalence).GetMethodInfo().GetGenericMethodDefinition();

public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
{
Type expectationType = config.GetExpectationType(context);
Expand Down Expand Up @@ -51,14 +57,14 @@ private static bool PreconditionsAreMet(IEquivalencyValidationContext context, I

private static bool AssertSubjectIsNotNull(object subject)
{
return subject != null || AssertionScope.Current
return AssertionScope.Current
.ForCondition(!(subject is null))
.FailWith("Expected {context:Subject} not to be {0}.", new object[] { null });
}

private static bool AssertExpectationIsNotNull(object subject, object expectation)
{
return expectation != null || AssertionScope.Current
return AssertionScope.Current
.ForCondition(!(expectation is null))
.FailWith("Expected {context:Subject} to be {0}, but found {1}.", null, subject);
}
Expand Down Expand Up @@ -159,9 +165,6 @@ private static Type GetIDictionaryInterface(Type expectedType)
return GetIDictionaryInterfaces(expectedType).Single();
}

private static readonly MethodInfo AssertSameLengthMethod = new Func<IDictionary<object, object>, IDictionary<object, object>, bool>
(AssertSameLength).GetMethodInfo().GetGenericMethodDefinition();

private static bool AssertSameLength<TSubjectKey, TSubjectValue, TExpectedKey, TExpectedValue>(
IDictionary<TSubjectKey, TSubjectValue> subject, IDictionary<TExpectedKey, TExpectedValue> expectation)
where TExpectedKey : TSubjectKey
Expand Down Expand Up @@ -238,9 +241,6 @@ private static Type GetIDictionaryInterface(Type expectedType)
AssertDictionaryEquivalenceMethod.MakeGenericMethod(typeArguments).Invoke(null, new[] { context, parent, config, context.Subject, context.Expectation });
}

private static readonly MethodInfo AssertDictionaryEquivalenceMethod = new Action<EquivalencyValidationContext, IEquivalencyValidator, IEquivalencyAssertionOptions, IDictionary<object, object>, IDictionary<object, object>>
(AssertDictionaryEquivalence).GetMethodInfo().GetGenericMethodDefinition();

private static void AssertDictionaryEquivalence<TSubjectKey, TSubjectValue, TExpectedKey, TExpectedValue>(
EquivalencyValidationContext context,
IEquivalencyValidator parent,
Expand Down
Expand Up @@ -10,6 +10,9 @@ namespace FluentAssertions.Equivalency
{
public class GenericEnumerableEquivalencyStep : IEquivalencyStep
{
private static readonly MethodInfo HandleMethod = new Action<EnumerableEquivalencyValidator, object[], IEnumerable<object>>
(HandleImpl).GetMethodInfo().GetGenericMethodDefinition();

/// <summary>
/// Gets a value indicating whether this step can handle the verificationScope subject and/or expectation.
/// </summary>
Expand Down Expand Up @@ -37,14 +40,11 @@ public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAsserti

var interfaceTypes = GetIEnumerableInterfaces(expectedType);

if (interfaceTypes.Length != 1)
{
AssertionScope.Current
.ForCondition(interfaceTypes.Length == 1)
.FailWith("{context:Expectation} implements {0}, so cannot determine which one " +
"to use for asserting the equivalency of the collection. ",
interfaceTypes.Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">").ToList());
}
AssertionScope.Current
.ForCondition(interfaceTypes.Length == 1)
.FailWith(() => new FailReason("{context:Expectation} implements {0}, so cannot determine which one " +
"to use for asserting the equivalency of the collection. ",
interfaceTypes.Select(type => "IEnumerable<" + type.GetGenericArguments().Single() + ">")));

if (AssertSubjectIsCollection(context.Expectation, context.Subject))
{
Expand All @@ -71,15 +71,12 @@ public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAsserti
return true;
}

private static readonly MethodInfo HandleMethod = new Action<EnumerableEquivalencyValidator, object[], IEnumerable<object>>
(HandleImpl).GetMethodInfo().GetGenericMethodDefinition();

private static void HandleImpl<T>(EnumerableEquivalencyValidator validator, object[] subject, IEnumerable<T> expectation)
=> validator.Execute(subject, expectation?.ToArray());

private static bool AssertSubjectIsCollection(object expectation, object subject)
{
bool conditionMet = subject != null || AssertionScope.Current
bool conditionMet = AssertionScope.Current
.ForCondition(!(subject is null))
.FailWith("Expected {context:subject} not to be {0}.", new object[] {null});

Expand Down
18 changes: 6 additions & 12 deletions Src/FluentAssertions/Equivalency/ObjectReference.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using FluentAssertions.Common;

namespace FluentAssertions.Equivalency
Expand All @@ -11,6 +12,7 @@ internal class ObjectReference
private readonly object @object;
private readonly string path;
private readonly bool? isComplexType;
private string[] pathElements;

public ObjectReference(object @object, string path, bool? isComplexType = null)
{
Expand All @@ -36,22 +38,14 @@ public override bool Equals(object obj)
return ReferenceEquals(@object, other.@object) && IsParentOf(other);
}

string[] pathElements;

string[] GetPathElements() => pathElements
private string[] GetPathElements() => pathElements
?? (pathElements = path.ToLowerInvariant().Replace("][", "].[").Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries));

private bool IsParentOf(ObjectReference other)
{
string[] path = GetPathElements(), otherPath = other.GetPathElements();

if (otherPath.Length <= path.Length)
return false;

for (int i = 0; i < path.Length; i++)
if (path[i] != otherPath[i])
return false;
return true;
string[] path = GetPathElements();
string[] otherPath = other.GetPathElements();
return (otherPath.Length > path.Length) && otherPath.Take(path.Length).SequenceEqual(path);
}

/// <summary>
Expand Down

0 comments on commit bf43168

Please sign in to comment.