/
MethodInfoSelectorAssertions.cs
233 lines (203 loc) · 10.5 KB
/
MethodInfoSelectorAssertions.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using FluentAssertions.Common;
using FluentAssertions.Execution;
namespace FluentAssertions.Types
{
/// <summary>
/// Contains assertions for the <see cref="MethodInfo"/> objects returned by the parent <see cref="MethodInfoSelector"/>.
/// </summary>
[DebuggerNonUserCode]
public class MethodInfoSelectorAssertions
{
/// <summary>
/// Initializes a new instance of the <see cref="MethodInfoSelectorAssertions"/> class.
/// </summary>
/// <param name="methods">The methods to assert.</param>
public MethodInfoSelectorAssertions(params MethodInfo[] methods)
{
SubjectMethods = methods;
}
/// <summary>
/// Gets the object which value is being asserted.
/// </summary>
public IEnumerable<MethodInfo> SubjectMethods { get; private set; }
/// <summary>
/// Asserts that the selected methods are virtual.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndConstraint<MethodInfoSelectorAssertions> BeVirtual(string because = "", params object[] becauseArgs)
{
MethodInfo[] nonVirtualMethods = GetAllNonVirtualMethodsFromSelection();
string failureMessage =
"Expected all selected methods to be virtual{reason}, but the following methods are not virtual:" +
Environment.NewLine +
GetDescriptionsFor(nonVirtualMethods);
Execute.Assertion
.ForCondition(!nonVirtualMethods.Any())
.BecauseOf(because, becauseArgs)
.FailWith(failureMessage);
return new AndConstraint<MethodInfoSelectorAssertions>(this);
}
/// <summary>
/// Asserts that the selected methods are not virtual.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndConstraint<MethodInfoSelectorAssertions> NotBeVirtual(string because = "", params object[] becauseArgs)
{
MethodInfo[] virtualMethods = GetAllVirtualMethodsFromSelection();
string failureMessage =
"Expected all selected methods not to be virtual{reason}, but the following methods are virtual:" +
Environment.NewLine +
GetDescriptionsFor(virtualMethods);
Execute.Assertion
.ForCondition(!virtualMethods.Any())
.BecauseOf(because, becauseArgs)
.FailWith(failureMessage);
return new AndConstraint<MethodInfoSelectorAssertions>(this);
}
private MethodInfo[] GetAllNonVirtualMethodsFromSelection()
{
IEnumerable<MethodInfo> query =
from method in SubjectMethods
where method.IsNonVirtual()
select method;
return query.ToArray();
}
private MethodInfo[] GetAllVirtualMethodsFromSelection()
{
IEnumerable<MethodInfo> query =
from method in SubjectMethods
where !method.IsNonVirtual()
select method;
return query.ToArray();
}
/// <summary>
/// Asserts that the selected methods are decorated with the specified <typeparamref name="TAttribute"/>.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndConstraint<MethodInfoSelectorAssertions> BeDecoratedWith<TAttribute>(string because = "", params object[] becauseArgs)
where TAttribute : Attribute
{
return BeDecoratedWith<TAttribute>(_ => true, because, becauseArgs);
}
/// <summary>
/// Asserts that the selected methods are decorated with an attribute of type <typeparamref name="TAttribute"/>
/// that matches the specified <paramref name="isMatchingAttributePredicate"/>.
/// </summary>
/// <param name="isMatchingAttributePredicate">
/// The predicate that the attribute must match.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndConstraint<MethodInfoSelectorAssertions> BeDecoratedWith<TAttribute>(
Expression<Func<TAttribute, bool>> isMatchingAttributePredicate, string because = "", params object[] becauseArgs)
where TAttribute : Attribute
{
Guard.ThrowIfArgumentIsNull(isMatchingAttributePredicate, nameof(isMatchingAttributePredicate));
MethodInfo[] methodsWithoutAttribute = GetMethodsWithout(isMatchingAttributePredicate);
string failureMessage =
"Expected all selected methods to be decorated with {0}{reason}, but the following methods are not:" +
Environment.NewLine +
GetDescriptionsFor(methodsWithoutAttribute);
Execute.Assertion
.ForCondition(!methodsWithoutAttribute.Any())
.BecauseOf(because, becauseArgs)
.FailWith(failureMessage, typeof(TAttribute));
return new AndConstraint<MethodInfoSelectorAssertions>(this);
}
/// <summary>
/// Asserts that the selected methods are not decorated with the specified <typeparamref name="TAttribute"/>.
/// </summary>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndConstraint<MethodInfoSelectorAssertions> NotBeDecoratedWith<TAttribute>(string because = "", params object[] becauseArgs)
where TAttribute : Attribute
{
return NotBeDecoratedWith<TAttribute>(_ => true, because, becauseArgs);
}
/// <summary>
/// Asserts that the selected methods are not decorated with an attribute of type <typeparamref name="TAttribute"/>
/// that matches the specified <paramref name="isMatchingAttributePredicate"/>.
/// </summary>
/// <param name="isMatchingAttributePredicate">
/// The predicate that the attribute must match.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndConstraint<MethodInfoSelectorAssertions> NotBeDecoratedWith<TAttribute>(
Expression<Func<TAttribute, bool>> isMatchingAttributePredicate, string because = "", params object[] becauseArgs)
where TAttribute : Attribute
{
Guard.ThrowIfArgumentIsNull(isMatchingAttributePredicate, nameof(isMatchingAttributePredicate));
MethodInfo[] methodsWithAttribute = GetMethodsWith(isMatchingAttributePredicate);
string failureMessage =
"Expected all selected methods to not be decorated with {0}{reason}, but the following methods are:" +
Environment.NewLine +
GetDescriptionsFor(methodsWithAttribute);
Execute.Assertion
.ForCondition(!methodsWithAttribute.Any())
.BecauseOf(because, becauseArgs)
.FailWith(failureMessage, typeof(TAttribute));
return new AndConstraint<MethodInfoSelectorAssertions>(this);
}
private MethodInfo[] GetMethodsWithout<TAttribute>(Expression<Func<TAttribute, bool>> isMatchingPredicate)
where TAttribute : Attribute
{
return SubjectMethods.Where(method => !method.IsDecoratedWith(isMatchingPredicate)).ToArray();
}
private MethodInfo[] GetMethodsWith<TAttribute>(Expression<Func<TAttribute, bool>> isMatchingPredicate)
where TAttribute : Attribute
{
return SubjectMethods.Where(method => method.IsDecoratedWith(isMatchingPredicate)).ToArray();
}
private static string GetDescriptionsFor(IEnumerable<MethodInfo> methods)
{
return string.Join(Environment.NewLine,
methods.Select(MethodInfoAssertions.GetDescriptionFor).ToArray());
}
/// <summary>
/// Returns the type of the subject the assertion applies on.
/// </summary>
#pragma warning disable CA1822 // Do not change signature of a public member
protected string Context => "method";
#pragma warning restore CA1822
}
}