-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
EquivalencyAssertionOptions.cs
191 lines (172 loc) · 9.07 KB
/
EquivalencyAssertionOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#region
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using FluentAssertions.Common;
using FluentAssertions.Equivalency.Execution;
using FluentAssertions.Equivalency.Matching;
using FluentAssertions.Equivalency.Ordering;
using FluentAssertions.Equivalency.Selection;
#endregion
namespace FluentAssertions.Equivalency
{
// REFACTOR rename to EquivalencyOptions
/// <summary>
/// Represents the run-time type-specific behavior of a structural equivalency assertion.
/// </summary>
public class EquivalencyAssertionOptions<TExpectation> :
SelfReferenceEquivalencyAssertionOptions<EquivalencyAssertionOptions<TExpectation>>
{
public EquivalencyAssertionOptions()
{
}
public EquivalencyAssertionOptions(IEquivalencyAssertionOptions defaults)
: base(defaults)
{
}
/// <summary>
/// Excludes the specified (nested) member from the structural equality check.
/// </summary>
public EquivalencyAssertionOptions<TExpectation> Excluding(Expression<Func<TExpectation, object>> expression)
{
AddSelectionRule(new ExcludeMemberByPathSelectionRule(expression.GetMemberPath()));
return this;
}
/// <summary>
/// Selects a collection to define exclusions at.
/// Allows to navigate deeper by using <see cref="For{TNext}"/>.
/// </summary>
public NestedExclusionOptionBuilder<TExpectation, TNext> For<TNext>(Expression<Func<TExpectation, IEnumerable<TNext>>> expression)
{
var selectionRule = new ExcludeMemberByPathSelectionRule(expression.GetMemberPath());
AddSelectionRule(selectionRule);
return new NestedExclusionOptionBuilder<TExpectation, TNext>(this, selectionRule);
}
/// <summary>
/// Includes the specified member in the equality check.
/// </summary>
/// <remarks>
/// This overrides the default behavior of including all declared members.
/// </remarks>
public EquivalencyAssertionOptions<TExpectation> Including(Expression<Func<TExpectation, object>> expression)
{
AddSelectionRule(new IncludeMemberByPathSelectionRule(expression.GetMemberPath()));
return this;
}
/// <summary>
/// Causes the collection identified by <paramref name="expression"/> to be compared in the order
/// in which the items appear in the expectation.
/// </summary>
public EquivalencyAssertionOptions<TExpectation> WithStrictOrderingFor(
Expression<Func<TExpectation, object>> expression)
{
string expressionMemberPath = expression.GetMemberPath().ToString();
OrderingRules.Add(new PathBasedOrderingRule(expressionMemberPath));
return this;
}
/// <summary>
/// Creates a new set of options based on the current instance which acts on a a collection of the <typeparamref name="TExpectation"/>.
/// </summary>
public EquivalencyAssertionOptions<IEnumerable<TExpectation>> AsCollection()
{
return new EquivalencyAssertionOptions<IEnumerable<TExpectation>>(
new CollectionMemberAssertionOptionsDecorator(this));
}
/// <summary>
/// Maps a (nested) property or field of type <typeparamref name="TExpectation"/> to
/// a (nested) property or field of <typeparamref name="TSubject"/> using lambda expressions.
/// </summary>
/// <param name="expectationMemberPath">A field or property expression indicating the (nested) member to map from.</param>
/// <param name="subjectMemberPath">A field or property expression indicating the (nested) member to map to.</param>
/// <remarks>
/// The members of the subject and the expectation must have the same parent. Also, indexes in collections are ignored.
/// If the types of the members are different, the usual logic applies depending or not if conversion options were specified.
/// Fields can be mapped to properties and vice-versa.
/// </remarks>
public EquivalencyAssertionOptions<TExpectation> WithMapping<TSubject>(
Expression<Func<TExpectation, object>> expectationMemberPath,
Expression<Func<TSubject, object>> subjectMemberPath)
{
return WithMapping(
expectationMemberPath.GetMemberPath().ToString().WithoutSpecificCollectionIndices(),
subjectMemberPath.GetMemberPath().ToString().WithoutSpecificCollectionIndices());
}
/// <summary>
/// Maps a (nested) property or field of the expectation to a (nested) property or field of the subject using a path string.
/// </summary>
/// <param name="expectationMemberPath">
/// A field or property path indicating the (nested) member to map from in the format <c>Parent.Child.Collection[].Member</c>.
/// </param>
/// <param name="subjectMemberPath">
/// A field or property path indicating the (nested) member to map to in the format <c>Parent.Child.Collection[].Member</c>.
/// </param>
/// <remarks>
/// The members of the subject and the expectation must have the same parent. Also, indexes in collections are not allowed
/// and must be written as "[]". If the types of the members are different, the usual logic applies depending or not
/// if conversion options were specified.
/// Fields can be mapped to properties and vice-versa.
/// </remarks>
public EquivalencyAssertionOptions<TExpectation> WithMapping(
string expectationMemberPath,
string subjectMemberPath)
{
AddMatchingRule(new MappedPathMatchingRule(expectationMemberPath, subjectMemberPath));
return this;
}
/// <summary>
/// Maps a direct property or field of type <typeparamref name="TNestedExpectation"/> to
/// a direct property or field of <typeparamref name="TNestedSubject"/> using lambda expressions.
/// </summary>
/// <param name="expectationMember">A field or property expression indicating the member to map from.</param>
/// <param name="subjectMember">A field or property expression indicating the member to map to.</param>
/// <remarks>
/// Only direct members of <typeparamref name="TNestedExpectation"/> and <typeparamref name="TNestedSubject"/> can be
/// mapped to each other. Those types can appear anywhere in the object graphs that are being compared.
/// If the types of the members are different, the usual logic applies depending or not if conversion options were specified.
/// Fields can be mapped to properties and vice-versa.
/// </remarks>
public EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(
Expression<Func<TNestedExpectation, object>> expectationMember,
Expression<Func<TNestedSubject, object>> subjectMember)
{
return WithMapping<TNestedExpectation, TNestedSubject>(
expectationMember.GetMemberPath().ToString(),
subjectMember.GetMemberPath().ToString());
}
/// <summary>
/// Maps a direct property or field of type <typeparamref name="TNestedExpectation"/> to
/// a direct property or field of <typeparamref name="TNestedSubject"/> using member names.
/// </summary>
/// <param name="expectationMemberName">A field or property name indicating the member to map from.</param>
/// <param name="subjectMemberName">A field or property name indicating the member to map to.</param>
/// <remarks>
/// Only direct members of <typeparamref name="TNestedExpectation"/> and <typeparamref name="TNestedSubject"/> can be
/// mapped to each other, so no <c>.</c> or <c>[]</c> are allowed.
/// Those types can appear anywhere in the object graphs that are being compared.
/// If the types of the members are different, the usual logic applies depending or not if conversion options were specified.
/// Fields can be mapped to properties and vice-versa.
/// </remarks>
public EquivalencyAssertionOptions<TExpectation> WithMapping<TNestedExpectation, TNestedSubject>(
string expectationMemberName,
string subjectMemberName)
{
AddMatchingRule(new MappedMemberMatchingRule<TNestedExpectation, TNestedSubject>(
expectationMemberName,
subjectMemberName));
return this;
}
}
/// <summary>
/// Represents the run-time type-agnostic behavior of a structural equivalency assertion.
/// </summary>
public class EquivalencyAssertionOptions : SelfReferenceEquivalencyAssertionOptions<EquivalencyAssertionOptions>
{
public EquivalencyAssertionOptions()
{
IncludingNestedObjects();
IncludingFields();
IncludingProperties();
RespectingDeclaredTypes();
}
}
}