Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Dec 26, 2021
1 parent b6bab8f commit b91d5d0
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 15 deletions.
38 changes: 38 additions & 0 deletions Src/FluentAssertions/Equivalency/EquivalencyAssertionOptions.cs
Expand Up @@ -2,9 +2,11 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using FluentAssertions.Common;
using FluentAssertions.Equivalency.Execution;
using FluentAssertions.Equivalency.Matching;
using FluentAssertions.Equivalency.Ordering;
using FluentAssertions.Equivalency.Selection;

Expand Down Expand Up @@ -70,6 +72,42 @@ public EquivalencyAssertionOptions<IEnumerable<TExpectation>> AsCollection()
return new EquivalencyAssertionOptions<IEnumerable<TExpectation>>(
new CollectionMemberAssertionOptionsDecorator(this));
}

public EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(
Expression<Func<TExpectation, object>> expectationPropertyPath,
Expression<Func<TSubject, object>> subjectPropertyPath)
{
return WithMapping(
expectationPropertyPath.GetMemberPath().ToString(),
subjectPropertyPath.GetMemberPath().ToString());
}

public EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(
Expression<Func<TNestedExpectation, object>> expectationProperty,
Expression<Func<TNestedSubject, object>> subjectProperty)
{
return WithMapping<TNestedExpectation, TNestedSubject>(
expectationProperty.GetMemberPath().ToString(),
subjectProperty.GetMemberPath().ToString());
}

public EquivalencyAssertionOptions<TExpectation> WithMapping(
string expectationPropertyPath,
string subjectPropertyPath)
{
AddMatchingRule(new MappedMemberMatchingRule(expectationPropertyPath, subjectPropertyPath));

return this;
}

public EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(
string expectationPropertyName,
string subjectPropertyName)
{
AddMatchingRule(new MappedMemberMatchingRule(typeof(TNestedExpectation), expectationPropertyName, typeof(TNestedSubject), subjectPropertyName));

return this;
}
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions Src/FluentAssertions/Equivalency/INode.cs
Expand Up @@ -16,6 +16,9 @@ public interface INode
/// <summary>
/// Gets the name of this node.
/// </summary>
/// <example>
/// "Property2"
/// </example>
string Name { get; }

/// <summary>
Expand All @@ -26,11 +29,17 @@ public interface INode
/// <summary>
/// Gets the path from the root object UNTIL the current node, separated by dots or index/key brackets.
/// </summary>
/// <example>
/// "Parent[0].Property2"
/// </example>
string Path { get; }

/// <summary>
/// Gets the full path from the root object up to and including the name of the node.
/// </summary>
/// <example>
/// "Parent[0]"
/// </example>
string PathAndName { get; }

/// <summary>
Expand All @@ -41,6 +50,9 @@ public interface INode
/// <summary>
/// Gets the path including the description of the subject.
/// </summary>
/// <example>
/// "property subject.Parent[0].Property2"
/// </example>
string Description { get; }

/// <summary>
Expand Down
@@ -0,0 +1,56 @@

// <auto-generated/>
using System;
using System.Reflection;
using FluentAssertions.Common;

namespace FluentAssertions.Equivalency.Matching
{
internal class MappedMemberMatchingRule : IMemberMatchingRule
{
private readonly string expectationPropertyPath;
private readonly string subjectPropertyPath;
private Type nestedExpectationType = null;
private readonly string expectationPropertyName;
private Type nestedSubjectType = null;
private readonly string subjectPropertyName;

public MappedMemberMatchingRule(string expectationPropertyPath, string subjectPropertyPath)
{
// TODO: What if the property path is empty or null?
// TODO: What if the two paths have a different parent?

this.expectationPropertyPath = expectationPropertyPath;
this.subjectPropertyPath = subjectPropertyPath;
}

public MappedMemberMatchingRule(Type expectationType, string expectationPropertyName, Type subjectType, string subjectPropertyName)
{
// TODO: What if the property is more than just a member name
// TODO: declaredtype vs reflectedtype

nestedExpectationType = expectationType;
this.expectationPropertyName = expectationPropertyName;
nestedSubjectType = subjectType;
this.subjectPropertyName = subjectPropertyName;
}

public IMember Match(IMember expectedMember, object subject, INode parent, IEquivalencyAssertionOptions options)
{
// TODO: What if the subject property does not exist
// TODO: What if the subject is null
// TODO: What if the expectation property does not exist
// TODO: What if the expectation is null

if (expectedMember.ReflectedType.IsSameOrInherits(nestedExpectationType) && subject.GetType() == nestedSubjectType)
{
if (expectedMember.Name == expectationPropertyName)
{
return MemberFactory.Create(subject.GetType().GetProperty(subjectPropertyName), parent);
}
}

return null;
}
}
}
Expand Up @@ -820,7 +820,7 @@ protected TSelf AddSelectionRule(IMemberSelectionRule selectionRule)
return (TSelf)this;
}

private TSelf AddMatchingRule(IMemberMatchingRule matchingRule)
protected TSelf AddMatchingRule(IMemberMatchingRule matchingRule)
{
matchingRules.Insert(0, matchingRule);
return (TSelf)this;
Expand Down
Expand Up @@ -751,6 +751,10 @@ namespace FluentAssertions.Equivalency
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<System.Collections.Generic.IEnumerable<TExpectation>> AsCollection() { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Excluding(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Including(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping(string expectationPropertyPath, string subjectPropertyPath) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(System.Linq.Expressions.Expression<System.Func<TNestedExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TNestedSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(string expectationPropertyName, string subjectPropertyName) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithStrictOrderingFor(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
}
public enum EquivalencyResult
Expand Down
Expand Up @@ -751,6 +751,10 @@ namespace FluentAssertions.Equivalency
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<System.Collections.Generic.IEnumerable<TExpectation>> AsCollection() { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Excluding(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Including(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping(string expectationPropertyPath, string subjectPropertyPath) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(System.Linq.Expressions.Expression<System.Func<TNestedExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TNestedSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(string expectationPropertyName, string subjectPropertyName) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithStrictOrderingFor(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
}
public enum EquivalencyResult
Expand Down
Expand Up @@ -751,6 +751,10 @@ namespace FluentAssertions.Equivalency
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<System.Collections.Generic.IEnumerable<TExpectation>> AsCollection() { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Excluding(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Including(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping(string expectationPropertyPath, string subjectPropertyPath) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(System.Linq.Expressions.Expression<System.Func<TNestedExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TNestedSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(string expectationPropertyName, string subjectPropertyName) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithStrictOrderingFor(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
}
public enum EquivalencyResult
Expand Down
Expand Up @@ -744,6 +744,10 @@ namespace FluentAssertions.Equivalency
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<System.Collections.Generic.IEnumerable<TExpectation>> AsCollection() { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Excluding(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Including(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping(string expectationPropertyPath, string subjectPropertyPath) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(System.Linq.Expressions.Expression<System.Func<TNestedExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TNestedSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(string expectationPropertyName, string subjectPropertyName) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithStrictOrderingFor(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
}
public enum EquivalencyResult
Expand Down
Expand Up @@ -751,6 +751,10 @@ namespace FluentAssertions.Equivalency
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<System.Collections.Generic.IEnumerable<TExpectation>> AsCollection() { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Excluding(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> Including(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping(string expectationPropertyPath, string subjectPropertyPath) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(System.Linq.Expressions.Expression<System.Func<TNestedExpectation, object>> expectationProperty, System.Linq.Expressions.Expression<System.Func<TNestedSubject, object>> subjectProperty) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(string expectationPropertyName, string subjectPropertyName) { }
public FluentAssertions.Equivalency.EquivalencyAssertionOptions<TExpectation> WithStrictOrderingFor(System.Linq.Expressions.Expression<System.Func<TExpectation, object>> expression) { }
}
public enum EquivalencyResult
Expand Down

0 comments on commit b91d5d0

Please sign in to comment.