Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Nov 11, 2021
1 parent b6bab8f commit 0d3a06f
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 14 deletions.
43 changes: 43 additions & 0 deletions Src/FluentAssertions/Equivalency/EquivalencyAssertionOptions.cs
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using FluentAssertions.Common;
using FluentAssertions.Equivalency.Execution;
Expand Down Expand Up @@ -70,6 +71,48 @@ 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<TExpectation, TSubject>(
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)
{
if ((expectationPropertyPath is null) || (subjectPropertyPath is null))
{
throw new InvalidOperationException(string.Empty);
}

return this;
}

public EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(
string expectationPropertyName,
string subjectPropertyName)
{
if ((expectationPropertyName is null) || (subjectPropertyName is null))
{
throw new InvalidOperationException(string.Empty);
}

return this;
}
}

/// <summary>
Expand Down
@@ -0,0 +1,10 @@
namespace FluentAssertions.Equivalency.Matching
{
internal class MappedMemberMatchingRule : IMemberMatchingRule
{
public IMember Match(IMember expectedMember, object subject, INode parent, IEquivalencyAssertionOptions options)
{
throw new System.NotImplementedException();
}
}
}
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
215 changes: 201 additions & 14 deletions Tests/FluentAssertions.Specs/Equivalency/BasicEquivalencySpecs.cs
Expand Up @@ -16,18 +16,6 @@ namespace FluentAssertions.Specs.Equivalency
{
public class BasicEquivalencySpecs
{
public enum LocalOtherType : byte
{
Default,
NonDefault
}

public enum LocalType : byte
{
Default,
NonDefault
}

#region General

[Fact]
Expand Down Expand Up @@ -746,6 +734,18 @@ public void When_the_graph_contains_guids_it_should_properly_format_them()

#region Selection Rules

public enum LocalOtherType : byte
{
Default,
NonDefault
}

public enum LocalType : byte
{
Default,
NonDefault
}

[Fact]
public void When_specific_properties_have_been_specified_it_should_ignore_the_other_properties()
{
Expand Down Expand Up @@ -2155,8 +2155,6 @@ public DerivedWithCovariantOverride(DerivedWithProperty prop)

#endif

#endregion

public interface IInterfaceWithTwoProperties
{
int Value1 { get; set; }
Expand Down Expand Up @@ -2215,6 +2213,8 @@ public void Including_an_interface_property_through_inheritance_should_work()
.RespectingRuntimeTypes());
}

#endregion

#region Matching Rules

[Fact]
Expand Down Expand Up @@ -3431,6 +3431,193 @@ public void When_a_property_of_a_nested_object_doesnt_match_it_should_clearly_in

#endregion

#region Mapping

[Fact]
public void Properties_mapped_by_name_with_equivalent_values_should_not_fail_the_assertion()
{
// Arrange
var subject = new SubjectWithProperty1
{
Property1 = "Hello"
};

var expectation = new ExpectationWithProperty2
{
Property2 = "Hello"
};

// Act / Assert
subject.Should()
.BeEquivalentTo(expectation, opt => opt
.WithMapping("Property2", "Property1"));
}

[Fact]
public void Properties_mapped_using_an_expression_with_equivalent_values_should_not_fail_the_assertion()
{
// Arrange
var subject = new SubjectWithProperty1
{
Property1 = "Hello"
};

var expectation = new ExpectationWithProperty2
{
Property2 = "Hello"
};

// Act / Assert
subject.Should()
.BeEquivalentTo(expectation, opt => opt
.WithMapping<SubjectWithProperty1>(e => e.Property2, e => e.Property1));
}

[Fact]
public void Nested_properties_mapped_by_name_with_equivalent_values_should_not_fail_the_assertion()
{
// Arrange
var subject = new
{
Parent = new[]
{
new SubjectWithProperty1
{
Property1 = "Hello"
}
}
};

var expectation = new
{
Parent = new[]
{
new ExpectationWithProperty2
{
Property2 = "Hello"
}
}
};

// Act / Assert
subject.Should()
.BeEquivalentTo(expectation, opt => opt
.WithMapping("Parent.Property2", "Parent.Property1"));
}

[Fact]
public void Nested_properties_mapped_using_an_expression_with_equivalent_values_should_not_fail_the_assertion()
{
// Arrange
var subject = new ParentOfSubjectWithProperty1(new[]
{
new SubjectWithProperty1
{
Property1 = "Hello"
}
});

var expectation = new ParentOfExpectationWithProperty2(new[]
{
new ExpectationWithProperty2
{
Property2 = "Hello"
}
});

// Act / Assert
subject.Should()
.BeEquivalentTo(expectation, opt => opt
.WithMapping<ParentOfSubjectWithProperty1>(
e => e.Parent[0].Property2,
s => s.Parent[0].Property1));
}

[Fact]
public void Nested_properties_mapped_by_the_nested_name_with_equivalent_values_should_not_fail_the_assertion()
{
// Arrange
var subject = new ParentOfSubjectWithProperty1(new[]
{
new SubjectWithProperty1
{
Property1 = "Hello"
}
});

var expectation = new ParentOfExpectationWithProperty2(new[]
{
new ExpectationWithProperty2
{
Property2 = "Hello"
}
});

// Act / Assert
subject.Should()
.BeEquivalentTo(expectation, opt => opt
.WithMapping<ExpectationWithProperty2, SubjectWithProperty1>("Property2", "Property1"));
}

[Fact]
public void Nested_properties_mapped_by_using_a_nested_expression_with_equivalent_values_should_not_fail_the_assertion()
{
// Arrange
var subject = new ParentOfSubjectWithProperty1(new[]
{
new SubjectWithProperty1
{
Property1 = "Hello"
}
});

var expectation = new ParentOfExpectationWithProperty2(new[]
{
new ExpectationWithProperty2
{
Property2 = "Hello"
}
});

// Act / Assert
subject.Should()
.BeEquivalentTo(expectation, opt => opt
.WithMapping<ExpectationWithProperty2, SubjectWithProperty1>(
e => e.Property2, s => s.Property1));
}

internal class ParentOfExpectationWithProperty2
{
public ExpectationWithProperty2[] Parent { get; }

public ParentOfExpectationWithProperty2(ExpectationWithProperty2[] parent)
{
Parent = parent;
}
}

internal class ParentOfSubjectWithProperty1
{
public SubjectWithProperty1[] Parent { get; }

public ParentOfSubjectWithProperty1(SubjectWithProperty1[] parent)
{
Parent = parent;
}
}

internal class SubjectWithProperty1
{
public string Property1 { get; set; }
}

internal class ExpectationWithProperty2
{
public string Property2 { get; set; }
}

#endregion

#region Cyclic References

[Fact]
Expand Down

0 comments on commit 0d3a06f

Please sign in to comment.