-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestBuilder.cs
More file actions
387 lines (349 loc) · 16 KB
/
TestBuilder.cs
File metadata and controls
387 lines (349 loc) · 16 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace IntelliTect.TestTools.TestFramework
{
public class TestBuilder
{
/// <summary>
/// Constructs a TestBuilder instance with a given test name.
/// </summary>
/// <param name="testMethodName">The name for the test method, defaults to the calling member.</param>
public TestBuilder([CallerMemberName] string? testMethodName = null)
{
TestMethodName = testMethodName ?? "UndefinedTestMethodName";
AddLogger<DebugLogger>();
}
private string? TestCaseName { get; set; }
private string TestMethodName { get; set; }
private int TestCaseId { get; set; }
private List<Block> TestBlocks { get; } = new();
private List<Block> FinallyBlocks { get; } = new();
private IServiceCollection Services { get; } = new ServiceCollection();
private bool HasLogger { get; set; } = true;
private List<Exception> ValidationExceptions { get; } = new();
/// <summary>
/// Used when a test case may be associated to a unique ID.
/// </summary>
/// <param name="testCaseKey">The key associated to this test.</param>
/// <returns></returns>
public TestBuilder AddTestCaseId(int testCaseKey)
{
TestCaseId = testCaseKey;
return this;
}
/// <summary>
/// Used to give a friendly name to the test case. Defaults to the test method name.
/// </summary>
/// <param name="testCaseName">A friendly name associated to the test case.</param>
/// <returns></returns>
public TestBuilder AddTestCaseName(string testCaseName)
{
TestCaseName = testCaseName;
return this;
}
/// <summary>
/// Adds a test block (some related group of test actions) with an optional list of arguments.<br />
/// Any argument passed here will override all other matched arguments for the blocks TestBlock.Execute() method.
/// </summary>
/// <typeparam name="T">The type of dependency a test block needs to execute.</typeparam>
/// <param name="testBlockArgs">The list of arguments to fulfill a set of Execute(params object[]) parameters.</param>
/// <returns>This</returns>
public TestBuilder AddTestBlock<T>(params object[] testBlockArgs) where T : ITestBlock
{
Block tb = CreateBlock<T>(testBlockArgs);
TestBlocks.Add(tb);
return this;
}
/// <summary>
/// Adds a test block (some related group of test actions) that includes asynchronous code with an optional list of arguments.<br />
/// Any argument passed here will override all other matched arguments for the blocks TestBlock.Execute() method.
/// </summary>
/// <typeparam name="T">The type of dependency a test block needs to execute.</typeparam>
/// <param name="testBlockArgs">The list of arguments to fulfill a set of Execute(params object[]) parameters.</param>
/// <returns>This</returns>
public TestBuilder AddAsyncTestBlock<T>(params object[] testBlockArgs) where T : ITestBlock
{
Block tb = CreateBlock<T>(testBlockArgs);
tb.IsAsync = true;
TestBlocks.Add(tb);
return this;
}
/// <summary>
/// Adds a finally block, a special test block that will always run after all test blocks, regardless of if a prior test block fails.
/// </summary>
/// <typeparam name="T">The type of dependency a test block needs to execute.</typeparam>
/// <param name="finallyBlockArgs">The list of arguments to fulfill a set of Execute(params object[]) parameters.</param>
/// <returns>This</returns>
public TestBuilder AddFinallyBlock<T>(params object[] finallyBlockArgs) where T : ITestBlock
{
Block fb = CreateBlock<T>(finallyBlockArgs);
fb.IsFinallyBlock = true;
FinallyBlocks.Add(fb);
return this;
}
/// <summary>
/// Adds a finally block, a special test block that includes asynchronous code that will always run after all test blocks, regardless of if a prior test block fails.
/// </summary>
/// <typeparam name="T">The type of dependency a test block needs to execute.</typeparam>
/// <param name="finallyBlockArgs">The list of arguments to fulfill a set of Execute(params object[]) parameters.</param>
/// <returns>This</returns>
public TestBuilder AddAsyncFinallyBlock<T>(params object[] testBlockArgs) where T : ITestBlock
{
Block fb = CreateBlock<T>(testBlockArgs);
fb.IsFinallyBlock = true;
fb.IsAsync = true;
FinallyBlocks.Add(fb);
return this;
}
/// <summary>
/// Adds a service as a factory a container that is used to fulfill TestBlock dependencies.
/// </summary>
/// <typeparam name="T">The type of dependency a test block needs to execute.</typeparam>
/// <param name="serviceFactory">The factory to provide an instance of the type needed for a test block to execute.</param>
/// <returns></returns>
public TestBuilder AddDependencyService<T>(Func<IServiceProvider, object> serviceFactory)
{
Services.AddSingleton(typeof(T), serviceFactory);
return this;
}
/// <summary>
/// Adds a service as a Type to the container that is used to fulfill TestBlock dependencies.
/// </summary>
/// <typeparam name="T">The type of test block, as an ITestBlock, to run</typeparam>
/// <returns>This</returns>
public TestBuilder AddDependencyService<T>()
{
Services.AddSingleton(typeof(T));
return this;
}
/// <summary>
/// Adds a service as a Type with an Implementation that is used to fulfill TestBlock dependencies.
/// </summary>
/// <typeparam name="TServiceType">The type of the service to add.</typeparam>
/// <typeparam name="TImplementationType">A specific implementation of the type.</typeparam>
/// <returns>This</returns>
public TestBuilder AddDependencyService<TServiceType, TImplementationType>()
{
Services.AddSingleton(typeof(TServiceType), typeof(TImplementationType));
return this;
}
/// <summary>
/// Adds an instance of a Type to the container that is needed for a TestBlock to execute.
/// </summary>
/// <param name="objToAdd">The instance of a Type that a TestBlock needs</param>
/// <returns>this</returns>
public TestBuilder AddDependencyInstance(object objToAdd)
{
if (objToAdd is null) throw new ArgumentNullException(nameof(objToAdd));
Services.AddSingleton(objToAdd.GetType(), objToAdd);
return this;
}
/// <summary>
/// Adds an instance of a Type to the container that is needed for a TestBlock to execute.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="objToAdd">The object to add.</param>
/// <returns></returns>
public TestBuilder AddDependencyInstance<T>(object objToAdd)
{
if (objToAdd is null) throw new ArgumentNullException(nameof(objToAdd));
Services.AddSingleton(typeof(T), objToAdd);
return this;
}
/// <summary>
/// Adds a new logger to be used during test execution. This will remove any existing loggers.
/// </summary>
/// <typeparam name="T">Type of logger.</typeparam>
/// <returns></returns>
public TestBuilder AddLogger<T>() where T : ITestCaseLogger
{
RemoveLogger();
Services.AddSingleton(typeof(ITestCaseLogger), typeof(T));
HasLogger = true;
return this;
}
/// <summary>
/// Removes the current logger from the test case dependency registration.<br />
/// NOTE: if you remove the logger but have a test block or dependency that needs it, an error will occur.
/// </summary>
/// <returns>this</returns>
public TestBuilder RemoveLogger()
{
ServiceDescriptor? logger = Services.FirstOrDefault(d => d.ServiceType == typeof(ITestCaseLogger));
if (logger is { }) Services.Remove(logger);
HasLogger = false;
return this;
}
/// <summary>
/// Builds the test case. This will validate that test block dependencies are satisfied.
/// </summary>
/// <returns>An object that can be used to execute a test case.</returns>
/// <exception cref="AggregateException"></exception>
public TestCase Build()
{
if (string.IsNullOrWhiteSpace(TestCaseName))
{
TestCaseName = TestMethodName;
}
TestCase testCase = new(TestCaseName!, TestMethodName, TestCaseId, Services);
testCase.HasLogger = HasLogger;
Services.AddSingleton(testCase);
// Probably need to profile all of this for performance at some point.
// Need to make sure if we're running hundreds or thousands of tests that we're not adding significant amount of time to that.
List<Type?> outputs = new();
foreach (Block tb in TestBlocks)
{
GatherDependencies(tb, outputs);
testCase.TestBlocks.Add(tb);
}
foreach (Block fb in FinallyBlocks)
{
GatherDependencies(fb, outputs);
testCase.FinallyBlocks.Add(fb);
}
if (ValidationExceptions.Count > 0)
{
throw new AggregateException(ValidationExceptions);
}
return testCase;
}
private Block CreateBlock<T>(params object[] args)
{
Services.AddTransient(typeof(T));
MethodInfo execute = FindExecuteMethod(typeof(T));
Block b = new(typeof(T), execute);
foreach (object a in args)
{
if(b.ExecuteArgumentOverrides.ContainsKey(a.GetType()))
{
ValidationExceptions.Add(new ArgumentException($"TestBlock: {typeof(T)} - Multiple execute argument overrides of the same type are not allowed: {a.GetType()}"));
}
else
{
b.ExecuteArgumentOverrides.Add(a.GetType(), a);
}
}
return b;
}
private static MethodInfo FindExecuteMethod(Type type)
{
List<MethodInfo>? executeMethod = type.GetMethods().Where(m => m.Name.ToUpperInvariant() == "EXECUTE").ToList();
if (executeMethod.Count is not 1)
{
// Don't add to validation messages we don't have any reasonable assurance what the dependencies should be.
throw new InvalidOperationException(
$"TestBlock: {type} - There must be one and only one Execute method on a test block.");
}
return executeMethod[0];
}
private void GatherDependencies(
Block tb,
List<Type?> outputs)
{
ConstructorInfo[]? constructors = tb.Type.GetConstructors();
if (constructors.Length > 1)
{
// Don't add to validation messages we don't have any reasonable assurance what the dependencies should be.
throw new InvalidOperationException(
$"TestBlock: {tb.Type} - TestFramework supports zero or one constructors on test blocks.");
}
tb.ConstructorParams = constructors[0].GetParameters();
tb.PropertyParams = tb.Type.GetProperties();
tb.ExecuteParams = tb.ExecuteMethod.GetParameters();
// Currently do not support Fields. Should we check for them anyway at least to throw?
HashSet<Type> inputs = new();
foreach (var c in tb.ConstructorParams)
{
inputs.Add(c.ParameterType);
}
foreach (var p in tb.PropertyParams)
{
if(p.CanWrite)
{
inputs.Add(p.PropertyType);
}
}
foreach (var e in tb.ExecuteParams)
{
inputs.Add(e.ParameterType);
}
if (!HasLogger) inputs.RemoveWhere(i => i == typeof(ITestCaseLogger));
if (tb.ExecuteArgumentOverrides.Count is not 0)
{
if (tb.ExecuteArgumentOverrides.Count > tb.ExecuteParams.Length)
{
ValidationExceptions.Add(new ArgumentException($"TestBlock: {tb.Type} - Too many execute overrides were provided. More were handed in than parameters on Execute method."));
}
else
{
foreach (KeyValuePair<Type, object> eao in tb.ExecuteArgumentOverrides)
{
if (!tb.ExecuteParams.Any(ep => ep.ParameterType == eao.Key))
{
ValidationExceptions.Add(new ArgumentException($"TestBlock: {tb.Type} - Unable to find corresponding Execute parameter for override argument {eao}"));
}
else
{
// Input is satisfied by execute argument override.
// No need to check later.
inputs.Remove(eao.Key);
}
}
}
}
foreach (Type i in inputs)
{
CheckContainerForFirstLevelDependency(i, outputs, $"TestBlock: {tb.Type} - Unable to satisfy test block input: {i}.");
}
Type executeReturns = tb.ExecuteMethod.ReturnType;
if (tb.IsAsync)
{
if (executeReturns.GenericTypeArguments.Any())
{
// How do we want to handle multiple type arguments?
// Or maybe we just don't support returning something like Func<string, string, string>?)
executeReturns = executeReturns.GenericTypeArguments.Last();
tb.AsyncReturnType = executeReturns;
}
else
{
// Treat Task like void for now.
// Currently there's no use case for passing Tasks between test blocks.
executeReturns = typeof(void);
}
}
if (executeReturns != typeof(void))
{
if (executeReturns.GetInterfaces().Any(x => x == typeof(IBlockData)))
{
Type[] blockData = executeReturns.GenericTypeArguments;
foreach (Type bd in blockData)
{
outputs.Add(bd);
}
}
else
{
outputs.Add(executeReturns);
}
}
}
private void CheckContainerForFirstLevelDependency(Type typeToCheck, List<Type?> testBlockOutputs, string errorMessage)
{
ServiceDescriptor? obj = Services.FirstOrDefault(x => x.ServiceType == typeToCheck || x.ImplementationType == typeToCheck);
if (obj is null)
{
Type? output = testBlockOutputs.FirstOrDefault(o => o == typeToCheck || o == typeToCheck);
if (output is null)
{
if (typeToCheck is ITestCaseLogger) return;
ValidationExceptions.Add(new InvalidOperationException(errorMessage));
}
}
}
}
}