forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectUnitTestDriverCore.ttinclude
More file actions
444 lines (399 loc) · 13.7 KB
/
DirectUnitTestDriverCore.ttinclude
File metadata and controls
444 lines (399 loc) · 13.7 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Threading.Tasks" #>
<#+
private const string TestDriverTestListProperty = "TestClasses";
/// <summary>
/// Generate direct test invocation driver code from test class informations.
/// </summary>
/// <param name="testClasses">A collection of test classes.</param>
private void GenerateTestDriver( IEnumerable<TestClass> testClasses )
{
var testClassList = ( testClasses ?? Enumerable.Empty<TestClass>() ).ToArray();
#>
/// <summary>
/// Represents a test class which groups related tests and holds their states.
/// </summary>
public partial class TestClass
{
/// <summary>
/// The null object for <see cref="Action" /> typed property.
/// </summary>
private static readonly Action Nop = () => {};
/// <summary>
/// The null object for <see cref="Action{TestClassInstance, object}"/> typed property.
/// </summary>
private static readonly Action<TestClassInstance, object> NoInitialization = ( c, i ) => {};
/// <summary>
/// Gets the name of the test class.
/// </summary>
/// <value>
/// The name of the test class. This value will not be <c>null</c>.
/// </value>
public string Name { get; private set; }
/// <summary>
/// Gets the delegate for fixture level setup routine.
/// </summary>
/// <value>
/// The delegate for fixture level setup routine. This value will not be <c>null</c> even if the underlying test class does not have any fixture level setup routines.
/// </value>
public Action FixtureSetup { get; set; }
/// <summary>
/// Gets the delegate for fixture level cleanup routine.
/// </summary>
/// <value>
/// The delegate for fixture level cleanup routine. This value will not be <c>null</c> even if the underlying test class does not have any fixture level cleanup routines.
/// </value>
public Action FixtureCleanup { get; set; }
/// <summary>
/// Gets the count of test methods in the test class.
/// </summary>
/// <value>
/// The ount of test methods in the test class.
/// </value>
public int MethodCount { get; private set; }
/// <summary>
/// The delegate which instantiate "test class" instance.
/// </summary>
private readonly Func<object> _instanceFactory;
/// <summary>
/// The delegate which initializes <see cref="TestClassInstance" /> instance with "test class" instance.
/// </summary>
private readonly Action<TestClassInstance, object> _testClassInstanceInitializer;
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="name">The name of the test class.</param>
/// <param name="instanceFactory">The delegate which instantiate "test class" instance.</param>
/// <param name="methodCount">The ount of test methods in the test class.</param>
/// <param name="testClassInstanceInitializer">The delegate which initializes <see cref="TestClassInstance" /> instance with "test class" instance.</param>
/// <exception cref="ArgumentException">The <paramref name="name"/> is <c>null</c> or empty.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="instanceFactory"/> is <c>null</c>.</exception>
public TestClass( string name, Func<object> instanceFactory, int methodCount, Action<TestClassInstance, object> testClassInstanceInitializer )
{
if ( String.IsNullOrEmpty( name ) )
{
throw new ArgumentException( "name cannot be null nor empty.", "name" );
}
if ( instanceFactory == null )
{
throw new ArgumentNullException( "instanceFactory" );
}
this.Name = name;
this._instanceFactory = instanceFactory;
this._testClassInstanceInitializer = testClassInstanceInitializer ?? NoInitialization;
this.MethodCount = methodCount;
this.FixtureSetup = Nop;
this.FixtureCleanup = Nop;
}
/// <summary>
/// Creates a new, initialized <see cref="TestClassInstance" /> which represents instantiated test class information.
/// </summary>
/// <returns>
/// The new, initialized <see cref="TestClassInstance" /> which represents instantiated test class information.
/// </returns>
public TestClassInstance NewTest()
{
var instance = this._instanceFactory();
var result = new TestClassInstance( this.MethodCount );
this._testClassInstanceInitializer( result, instance );
return result;
}
}
/// <summary>
/// Represents instantiated test class information.
/// </summary>
public partial class TestClassInstance
{
// A test class intance will be hold via delegate for its instance methods.
/// <summary>
/// The null object for <see cref="Action" /> typed property.
/// </summary>
private static readonly Action Nop = () => {};
/// <summary>
/// Gets the delegate for per test setup routine.
/// </summary>
/// <value>
/// The delegate for per test setup routine. This value will not be <c>null</c> even if the underlying test class does not have any per test setup routines.
/// </value>
public Action TestSetup { get; set; }
/// <summary>
/// Gets the delegate for per test cleanup routine.
/// </summary>
/// <value>
/// The delegate for per test cleanup routine. This value will not be <c>null</c> even if the underlying test class does not have any per test cleanup routines.
/// </value>
public Action TestCleanup { get; set; }
/// <summary>
/// Gets the list of the test methods.
/// </summary>
/// <value>
/// The list of the test methods. This value will not be <c>null</c>.
/// </value>
public IList<TestMethod> TestMethods { get; private set; }
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="methodCount">The ount of test methods in the test class.</param>
public TestClassInstance( int methodCount )
{
this.TestMethods = new List<TestMethod>( methodCount );
this.TestSetup = Nop;
this.TestCleanup = Nop;
}
}
/// <summary>
/// Represents a test method.
/// </summary>
public partial class TestMethod
{
// "test case" is not supported.
/// <summary>
/// Gets the name of the test method.
/// </summary>
/// <value>
/// The name of the test method. This value will not be <c>null</c>.
/// </value>
public string Name { get; private set; }
/// <summary>
/// Gets the delegate for instance methnod which is test method itself
/// </summary>
/// <value>
/// The delegate for instance methnod which is test method itself. This value will not be <c>null</c>.
/// </value>
public Action Method { get; private set; }
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="name">The name of the test method.</param>
/// <param name="method">The delegate for instance methnod which is test method itself.</param>
/// <exception cref="ArgumentException">The <paramref name="name"/> is <c>null</c> or empty.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="method"/> is <c>null</c>.</exception>
public TestMethod( string name, Action method )
{
if ( String.IsNullOrEmpty( name ) )
{
throw new ArgumentException( "name cannot be null nor empty.", "name" );
}
if ( method == null )
{
throw new ArgumentNullException( "method" );
}
this.Name = name;
this.Method = method;
}
}
/// <summary>
/// Implements running environment agnostics test driver features.
/// </summary>
public partial class TestDriver
{
/// <summary>
/// Gets the list of the test classes.
/// </summary>
/// <value>
/// The list of the test classes. This value will not be <c>null</c>.
/// </value>
protected IList<TestClass> <#= TestDriverTestListProperty #> { get; private set; }
/// <summary>
/// Initializes a new instance.
/// </summary>
protected TestDriver()
{
this.<#= TestDriverTestListProperty #> = new List<TestClass>( <#= testClassList.Length #> );
InitializeTestClasses( this.<#= TestDriverTestListProperty #> );
}
/// <summary>
/// Fills intialized <see cref="TestClass" /> to specified list.
/// </summary>
private static void InitializeTestClasses( IList<TestClass> testClasses )
{
<#+
foreach ( var testClass in testClassList )
{
var friendlyName = testClass.TypeFullName;
var lastDot = friendlyName.LastIndexOf( "." );
if ( lastDot >= 0 && friendlyName.Length > lastDot + 2 )
{
friendlyName = friendlyName.Substring( lastDot + 1 );
}
var initializerName = GetInitializerClassName( testClass.TypeFullName );
#>
{
var testClass =
new TestClass(
"<#= friendlyName #>",
<#= initializerName #>.CreateInstance,
<#= testClass.TestMethods.Count #>,
<#= initializerName #>.InitializeInstance
);
<#+
GenerateActionAssignmentIfNotEmpty( testClass.FixtureSetup, "testClass.FixtureSetup", testClass.TypeFullName + ".{0}" );
GenerateActionAssignmentIfNotEmpty( testClass.FixtureCleanup, "testClass.FixtureCleanup", testClass.TypeFullName + ".{0}" );
#>
testClasses.Add( testClass );
}
<#+
} // foreach ( testClasses )
#>
} // void InitializeTestClasses
} // partial class TestDriver
<#+
// To avoid UNetWeaver error, avoid lamdas as possible.
foreach ( var testClass in testClassList )
{
#>
internal static class <#= GetInitializerClassName( testClass.TypeFullName ) #>
{
public static object CreateInstance()
{
return new <#= testClass.TypeFullName #>();
}
public static void InitializeInstance( TestClassInstance testClassInstance, object testFixtureInstance )
{
var instance = ( ( <#= testClass.TypeFullName #> )testFixtureInstance );
<#+
GenerateActionAssignmentIfNotEmpty( testClass.TestSetup, "testClassInstance.TestSetup", "instance.{0}" );
GenerateActionAssignmentIfNotEmpty( testClass.TestCleanup, "testClassInstance.TestCleanup", "instance.{0}" );
foreach ( var method in testClass.TestMethods )
{
if ( method.ArgumentsList.Count == 0 )
{
#>
testClassInstance.TestMethods.Add( new TestMethod( "<#= method.Name #>", new Action( instance.<#= method.Name #> ) ) );
<#+
}
else
{
#>
testClassInstance.TestMethods.Add(
new TestMethod(
"<#= method.Name #>",
() => {
<#+
foreach( var arguments in method.ArgumentsList )
{
#>
instance.<#= method.Name #>( <#= String.Join( ", ", arguments ) #> );
<#+
}
#>
}
)
);
<#+
}
}
#>
}
}
<#+
}
} // GenerateTestDriver
private void GenerateActionAssignmentIfNotEmpty( string value, string target, string expressionTemplate )
{
if ( !String.IsNullOrEmpty( value ) )
{
#>
<#= target #> = new Action( <#= String.Format( CultureInfo.InvariantCulture, expressionTemplate, value ) #> );
<#+
}
}
/// <summary>
/// Returns the class name for initializer.
/// </summary>
/// <param name="typeFullName">The type full name of the test class.</param>
/// <returns>
/// The class name for initializer.
/// </returns>
private static string GetInitializerClassName( string typeFullName )
{
return typeFullName.Replace( '.', '_' ) + "Initializer";
}
/// <summary>
/// Represents the test class for code generaton.
/// </summary>
private class TestClass
{
public string TypeFullName { get; private set; }
/// <summary>
/// Gets or sets the name of fixture level test setup method.
/// </summary>
/// <value>
/// The name for of fixture level test setup method. This value will be <c>null</c> if not exists.
/// </value>
public string FixtureSetup { get; set; }
/// <summary>
/// Gets or sets the name of fixture level test cleanup method.
/// </summary>
/// <value>
/// The name for of fixture level test cleanup method. This value will be <c>null</c> if not exists.
/// </value>
public string FixtureCleanup { get; set; }
/// <summary>
/// Gets or sets the name of per test setup method.
/// </summary>
/// <value>
/// The name for of per test setup method. This value will be <c>null</c> if not exists.
/// </value>
public string TestSetup { get; set; }
/// <summary>
/// Gets or sets the name of per test cleanup method.
/// </summary>
/// <value>
/// The name for of per test cleanup method. This value will be <c>null</c> if not exists.
/// </value>
public string TestCleanup { get; set; }
/// <summary>
/// Gets the list of the test methods.
/// </summary>
/// <value>
/// The list of the test methods. This value will not be <c>null</c>.
/// </value>
public IList<TestMethod> TestMethods { get; private set; }
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="name">The name of the test class.</param>
/// <param name="methods">The test methods.</param>
public TestClass( string typeFullName, IEnumerable<TestMethod> methods )
{
this.TypeFullName = typeFullName;
this.TestMethods = methods == null ? new List<TestMethod>() : methods.ToList();
}
}
/// <summary>
/// Represents the test method for code generaton.
/// </summary>
private struct TestMethod
{
/// <summary>
/// Gets the name of the test method.
/// </summary>
/// <value>
/// The name of the test method. This value will not be <c>null</c>.
/// </value>
public string Name { get; private set; }
/// <summary>
/// Gets the arguments for the test method in C# syntax
/// </summary>
/// <value>
/// The arguments for the test method in C# syntax. Each element is arguments for single test method invocation.
/// This value will not be <c>null</c>.
/// </value>
public IList<IList<string>> ArgumentsList { get; private set; }
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="name">The name of the test method.</param>
/// <param name="arguments">The arguments for the test method.</param>
public TestMethod( string name, IList<IList<string>> argumentsList )
{
this.Name = name;
this.ArgumentsList = argumentsList;
}
}
#>