-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathMethodInfoExtensions.cs
303 lines (269 loc) · 12.7 KB
/
MethodInfoExtensions.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions;
internal static class MethodInfoExtensions
{
/// <summary>
/// Verifies that the class initialize has the correct signature.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <returns>True if the method has the right Assembly/Class initialize signature.</returns>
internal static bool HasCorrectClassOrAssemblyInitializeSignature(this MethodInfo method)
{
DebugEx.Assert(method != null, "method should not be null.");
ParameterInfo[] parameters = method.GetParameters();
return
method is { IsStatic: true, IsPublic: true } &&
(parameters.Length == 1) &&
parameters[0].ParameterType == typeof(TestContext) &&
method.IsValidReturnType();
}
/// <summary>
/// Verifies that the class cleanup has the correct signature.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <returns>True if the method has the right Assembly/Class cleanup signature.</returns>
internal static bool HasCorrectClassOrAssemblyCleanupSignature(this MethodInfo method)
{
DebugEx.Assert(method != null, "method should not be null.");
return
method is { IsStatic: true, IsPublic: true } &&
HasCorrectClassOrAssemblyCleanupParameters(method) &&
method.IsValidReturnType();
}
private static bool HasCorrectClassOrAssemblyCleanupParameters(MethodInfo method)
{
ParameterInfo[] parameters = method.GetParameters();
return parameters.Length == 0 || (parameters.Length == 1 && parameters[0].ParameterType == typeof(TestContext));
}
/// <summary>
/// Verifies that the test Initialize/cleanup has the correct signature.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <returns>True if the method has the right test init/cleanup signature.</returns>
internal static bool HasCorrectTestInitializeOrCleanupSignature(this MethodInfo method)
{
DebugEx.Assert(method != null, "method should not be null.");
return
method is { IsStatic: false, IsPublic: true } &&
(method.GetParameters().Length == 0) &&
method.IsValidReturnType();
}
/// <summary>
/// Verifies that the test method has the correct signature.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <param name="ignoreParameterLength">Indicates whether parameter length is to be ignored.</param>
/// <param name="discoverInternals">True if internal test classes and test methods should be discovered in
/// addition to public test classes and methods.</param>
/// <returns>True if the method has the right test method signature.</returns>
internal static bool HasCorrectTestMethodSignature(this MethodInfo method, bool ignoreParameterLength, bool discoverInternals = false)
{
DebugEx.Assert(method != null, "method should not be null.");
return
method is { IsAbstract: false, IsStatic: false } &&
(method.IsPublic || (discoverInternals && method.IsAssembly)) &&
(method.GetParameters().Length == 0 || ignoreParameterLength) &&
method.IsValidReturnType(); // Match return type Task for async methods only. Else return type void.
}
/// <summary>
/// Check is return type is void for non async and Task for async methods.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <returns>True if the method has a void/task return type..</returns>
internal static bool IsValidReturnType(this MethodInfo method)
=> ReflectHelper.MatchReturnType(method, typeof(Task))
|| ReflectHelper.MatchReturnType(method, typeof(ValueTask))
|| (ReflectHelper.MatchReturnType(method, typeof(void)) && method.GetAsyncTypeName() == null);
/// <summary>
/// For async methods compiler generates different type and method.
/// Gets the compiler generated type name for given async test method.
/// </summary>
/// <param name="method">The method to verify.</param>
/// <returns>Compiler generated type name for given async test method..</returns>
internal static string? GetAsyncTypeName(this MethodInfo method)
{
AsyncStateMachineAttribute? asyncStateMachineAttribute = ReflectHelper.Instance.GetFirstNonDerivedAttributeOrDefault<AsyncStateMachineAttribute>(method, inherit: false);
return asyncStateMachineAttribute?.StateMachineType?.FullName;
}
/// <summary>
/// Invoke a <see cref="MethodInfo"/> as an asynchronous <see cref="Task"/>.
/// </summary>
/// <param name="methodInfo">
/// <see cref="MethodInfo"/> instance.
/// </param>
/// <param name="classInstance">
/// Instance of the on which methodInfo is invoked.
/// </param>
/// <param name="arguments">
/// Arguments for the methodInfo invoke.
/// </param>
internal static async Task InvokeAsync(this MethodInfo methodInfo, object? classInstance, params object?[]? arguments)
{
ParameterInfo[]? methodParameters = methodInfo.GetParameters();
// check if test method expected parameter values but no test data was provided,
// throw error with appropriate message.
if (methodParameters is { Length: > 0 } && arguments == null)
{
throw new TestFailedException(
ObjectModel.UnitTestOutcome.Error,
string.Format(
CultureInfo.InvariantCulture,
Resource.CannotRunTestMethodNoDataError,
methodInfo.DeclaringType!.FullName,
methodInfo.Name));
}
object? invokeResult;
if (arguments is not null
&& (arguments.Length != 1 || arguments[0] is not null)
&& methodParameters.Length == 1
&& methodParameters[0].ParameterType == typeof(object[]))
{
invokeResult = methodInfo.Invoke(classInstance, [arguments]);
}
else
{
int methodParametersLengthOrZero = methodParameters?.Length ?? 0;
int argumentsLengthOrZero = arguments?.Length ?? 0;
#if WINDOWS_UWP
// There is a bug with UWP in release mode where the arguments are wrapped in an object[], so we need to unwrap it.
// See https://github.com/microsoft/testfx/issues/3071
if (argumentsLengthOrZero == 1
&& argumentsLengthOrZero < methodParametersLengthOrZero
&& arguments![0] is object[] args)
{
arguments = args;
argumentsLengthOrZero = args.Length;
}
#endif
try
{
if (methodInfo.IsGenericMethod)
{
methodInfo = ConstructGenericMethod(methodInfo, arguments);
}
invokeResult = methodInfo.Invoke(classInstance, arguments);
}
catch (Exception ex) when (ex is TargetParameterCountException or ArgumentException)
{
throw new TestFailedException(
ObjectModel.UnitTestOutcome.Error,
string.Format(
CultureInfo.InvariantCulture,
Resource.CannotRunTestArgumentsMismatchError,
methodInfo.DeclaringType!.FullName,
methodInfo.Name,
methodParametersLengthOrZero,
string.Join(", ", methodParameters?.Select(p => p.ParameterType.Name) ?? Array.Empty<string>()),
argumentsLengthOrZero,
string.Join(", ", arguments?.Select(a => a?.GetType().Name ?? "null") ?? Array.Empty<string>())), ex);
}
}
// If methodInfo is an async method, wait for returned task
if (invokeResult is Task task)
{
await task;
}
else if (invokeResult is ValueTask valueTask)
{
await valueTask;
}
}
/// <summary>
/// Invoke a <see cref="MethodInfo"/> as a synchronous <see cref="Task"/>.
/// </summary>
/// <param name="methodInfo">
/// <see cref="MethodInfo"/> instance.
/// </param>
/// <param name="classInstance">
/// Instance of the on which methodInfo is invoked.
/// </param>
/// <param name="arguments">
/// Arguments for the methodInfo invoke.
/// </param>
internal static void InvokeAsSynchronousTask(this MethodInfo methodInfo, object? classInstance, params object?[]? arguments)
=> InvokeAsync(methodInfo, classInstance, arguments).GetAwaiter().GetResult();
// Scenarios to test:
//
// [DataRow(null, "Hello")]
// [DataRow("Hello", null)]
// public void TestMethod<T>(T t1, T t2) { }
//
// [DataRow(0, "Hello")]
// public void TestMethod<T1, T2>(T2 p0, T1, p1) { }
private static MethodInfo ConstructGenericMethod(MethodInfo methodInfo, object?[]? arguments)
{
DebugEx.Assert(methodInfo.IsGenericMethod, "ConstructGenericMethod should only be called for a generic method.");
if (arguments is null)
{
// An example where this could happen is:
// [TestMethod]
// public void MyTestMethod<T>() { }
throw new TestFailedException(ObjectModel.UnitTestOutcome.Error, string.Format(CultureInfo.InvariantCulture, Resource.GenericParameterCantBeInferredBecauseNoArguments, methodInfo.Name));
}
Type[] genericDefinitions = methodInfo.GetGenericArguments();
var map = new (Type GenericDefinition, Type? Substitution)[genericDefinitions.Length];
for (int i = 0; i < map.Length; i++)
{
map[i] = (genericDefinitions[i], null);
}
ParameterInfo[] parameters = methodInfo.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
Type parameterType = parameters[i].ParameterType;
if (!parameterType.IsGenericMethodParameter() || arguments[i] is null)
{
continue;
}
Type substitution = arguments[i]!/*Very strange nullability warning*/.GetType();
int mapIndexForParameter = GetMapIndexForParameterType(parameterType, map);
Type? existingSubstitution = map[mapIndexForParameter].Substitution;
if (existingSubstitution is null || substitution.IsAssignableFrom(existingSubstitution))
{
map[mapIndexForParameter] = (parameterType, substitution);
}
else if (existingSubstitution.IsAssignableFrom(substitution))
{
// Do nothing. We already have a good existing substitution.
}
else
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resource.GenericParameterConflict, parameterType.Name, existingSubstitution, substitution));
}
}
for (int i = 0; i < map.Length; i++)
{
// TODO: Better to throw? or tolerate and transform to typeof(object)?
// This is reachable in the following case for example:
// [DataRow(null)]
// public void TestMethod<T>(T t) { }
Type substitution = map[i].Substitution ?? throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resource.GenericParameterCantBeInferred, map[i].GenericDefinition.Name));
genericDefinitions[i] = substitution;
}
try
{
return methodInfo.MakeGenericMethod(genericDefinitions);
}
catch (Exception e)
{
// The caller catches ArgumentExceptions and will lose the original exception details.
// We transform the exception to TestFailedException here to preserve its details.
throw new TestFailedException(ObjectModel.UnitTestOutcome.Error, e.TryGetMessage(), e.TryGetStackTraceInformation(), e);
}
}
private static int GetMapIndexForParameterType(Type parameterType, (Type GenericDefinition, Type? Substitution)[] map)
{
for (int i = 0; i < map.Length; i++)
{
if (parameterType == map[i].GenericDefinition)
{
return i;
}
}
throw ApplicationStateGuard.Unreachable();
}
}