-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathDynamicDataOperations.cs
More file actions
177 lines (149 loc) · 7.52 KB
/
DynamicDataOperations.cs
File metadata and controls
177 lines (149 loc) · 7.52 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
internal static class DynamicDataOperations
{
private const BindingFlags MemberLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
public static IEnumerable<object[]> GetData(Type? dynamicDataDeclaringType, DynamicDataSourceType dynamicDataSourceType, string dynamicDataSourceName, object?[] dynamicDataSourceArguments, MethodInfo methodInfo)
{
// Check if the declaring type of test data is passed in. If not, default to test method's class type.
dynamicDataDeclaringType ??= methodInfo.DeclaringType;
DebugEx.Assert(dynamicDataDeclaringType is not null, "Declaring type of test data cannot be null.");
object? obj = null;
switch (dynamicDataSourceType)
{
case DynamicDataSourceType.AutoDetect:
#pragma warning disable IDE0045 // Convert to conditional expression - it becomes less readable.
if (GetPropertyConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataPropertyInfo)
{
obj = GetDataFromProperty(dynamicDataPropertyInfo);
}
else if (GetMethodConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataMethodInfo)
{
obj = GetDataFromMethod(dynamicDataMethodInfo, dynamicDataSourceArguments);
}
else if (GetFieldConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataFieldInfo)
{
obj = GetDataFromField(dynamicDataFieldInfo);
}
else
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, FrameworkMessages.DynamicDataSourceShouldExistAndBeValid, dynamicDataSourceName, dynamicDataDeclaringType.FullName));
}
#pragma warning restore IDE0045 // Convert to conditional expression
break;
case DynamicDataSourceType.Property:
PropertyInfo property = GetPropertyConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName)
?? throw new ArgumentNullException($"{DynamicDataSourceType.Property} {dynamicDataSourceName}");
obj = GetDataFromProperty(property);
break;
case DynamicDataSourceType.Method:
MethodInfo method = GetMethodConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName)
?? throw new ArgumentNullException($"{DynamicDataSourceType.Method} {dynamicDataSourceName}");
obj = GetDataFromMethod(method, dynamicDataSourceArguments);
break;
case DynamicDataSourceType.Field:
FieldInfo field = GetFieldConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName)
?? throw new ArgumentNullException($"{DynamicDataSourceType.Field} {dynamicDataSourceName}");
obj = GetDataFromField(field);
break;
}
if (obj == null)
{
throw new ArgumentNullException(
string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataValueNull,
dynamicDataSourceName,
dynamicDataDeclaringType.FullName));
}
if (!TryGetData(obj, out IEnumerable<object[]>? data))
{
throw new ArgumentNullException(
string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataIEnumerableNull,
dynamicDataSourceName,
dynamicDataDeclaringType.FullName));
}
// Data is valid, return it.
return data;
}
private static object? GetDataFromMethod(MethodInfo method, object?[] arguments)
{
if (!method.IsStatic || method.ContainsGenericParameters)
{
throw new NotSupportedException(
string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataInvalidMethodLayout,
method.DeclaringType?.FullName is { } typeFullName ? $"{typeFullName}.{method.Name}" : method.Name));
}
ParameterInfo[] methodParameters = method.GetParameters();
ParameterInfo? lastParameter = methodParameters.Length > 0 ? methodParameters[methodParameters.Length - 1] : null;
if (lastParameter is not null &&
(lastParameter.GetCustomAttribute<ParamArrayAttribute>() is not null ||
lastParameter.GetCustomAttribute<ParamCollectionAttribute>() is not null))
{
throw new NotSupportedException(
string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataInvalidMethodLayout,
method.DeclaringType?.FullName is { } typeFullName ? $"{typeFullName}.{method.Name}" : method.Name));
}
// Note: the method is static.
return method.Invoke(null, arguments.Length == 0 ? null : arguments);
}
private static object? GetDataFromField(FieldInfo field)
{
if (!field.IsStatic)
{
throw new NotSupportedException(
string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataInvalidFieldLayout,
field.DeclaringType?.FullName is { } typeFullName ? $"{typeFullName}.{field.Name}" : field.Name));
}
// Note: the field is static.
return field.GetValue(null);
}
private static object? GetDataFromProperty(PropertyInfo property)
{
if (property.GetGetMethod(true) is not { IsStatic: true })
{
throw new NotSupportedException(
string.Format(
CultureInfo.InvariantCulture,
FrameworkMessages.DynamicDataInvalidPropertyLayout,
property.DeclaringType?.FullName is { } typeFullName ? $"{typeFullName}.{property.Name}" : property.Name));
}
// Note: the property getter is static.
return property.GetValue(null, null);
}
private static bool TryGetData(object dataSource, [NotNullWhen(true)] out IEnumerable<object[]>? data)
{
if (dataSource is IEnumerable<object[]> enumerableObjectArray)
{
data = enumerableObjectArray;
return true;
}
if (dataSource is IEnumerable enumerable and not string)
{
List<object[]> objects = [];
foreach (object? entry in enumerable)
{
objects.Add([entry]);
}
data = objects;
return true;
}
data = null;
return false;
}
private static FieldInfo? GetFieldConsideringInheritance(Type type, string fieldName)
=> type.GetField(fieldName, MemberLookup);
private static PropertyInfo? GetPropertyConsideringInheritance(Type type, string propertyName)
=> type.GetProperty(propertyName, MemberLookup);
private static MethodInfo? GetMethodConsideringInheritance(Type type, string methodName)
=> type.GetMethod(methodName, MemberLookup);
}