-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathTestMethodValidator.cs
78 lines (68 loc) · 3.62 KB
/
TestMethodValidator.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
// 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.Extensions;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
/// <summary>
/// Determines if a method is a valid test method.
/// </summary>
[SuppressMessage("Performance", "CA1852: Seal internal types", Justification = "Overrides required for testability")]
internal class TestMethodValidator
{
private readonly ReflectHelper _reflectHelper;
private readonly bool _discoverInternals;
/// <summary>
/// Initializes a new instance of the <see cref="TestMethodValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
internal TestMethodValidator(ReflectHelper reflectHelper)
: this(reflectHelper, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TestMethodValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
/// <param name="discoverInternals">True to discover methods which are declared internal in addition to methods
/// which are declared public.</param>
internal TestMethodValidator(ReflectHelper reflectHelper, bool discoverInternals)
{
_reflectHelper = reflectHelper;
_discoverInternals = discoverInternals;
}
/// <summary>
/// Determines if a method is a valid test method.
/// </summary>
/// <param name="testMethodInfo"> The reflected method. </param>
/// <param name="type"> The reflected type. </param>
/// <param name="warnings"> Contains warnings if any, that need to be passed back to the caller. </param>
/// <returns> Return true if a method is a valid test method. </returns>
internal virtual bool IsValidTestMethod(MethodInfo testMethodInfo, Type type, ICollection<string> warnings)
{
// PERF: We are doing caching reflection here, meaning we will cache every method info in the
// assembly, this is because when we discover and run we will repeatedly inspect all the methods in the assembly, and this
// gives us a better performance.
// It would be possible to use non-caching reflection here if we knew that we are only doing discovery that won't be followed by run,
// but the difference is quite small, and we don't expect a huge amount of non-test methods in the assembly.
//
// Also skip all methods coming from object, because they cannot be tests.
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsAttributeDefined<TestMethodAttribute>(testMethodInfo, inherit: false))
{
return false;
}
bool isAccessible = testMethodInfo.IsPublic
|| (_discoverInternals && testMethodInfo.IsAssembly);
// Todo: Decide whether parameter count matters.
bool isValidTestMethod = isAccessible &&
testMethodInfo is { IsAbstract: false, IsStatic: false } &&
testMethodInfo.IsValidReturnType();
if (!isValidTestMethod)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorIncorrectTestMethodSignature, type.FullName, testMethodInfo.Name);
warnings.Add(message);
return false;
}
return true;
}
}