This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
mockable.d
520 lines (467 loc) · 13.7 KB
/
mockable.d
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
* Mixin template to enable mocking.
*
* Many methods implement compile-time parameters (file, line) that are set at the call site.
* It is preferred that these parameters are ignored when using these methods.
*
* License:
* MIT. See LICENSE for full details.
*/
module dunit.mockable;
/**
* Imports.
*/
public import dunit.reflection;
/**
* A template mixin used to inject code into a class to provide mockable behaviour.
* Code is nested within the host class to provide access to all host types.
* $(B Only injects code when using the -unittest compiler switch.)
*
* Caveats:
* Only module level types can be made mockable.
*
* Example:
* ---
* import dunit.mockable;
*
* class T
* {
* mixin Mockable!(T);
* }
* ---
*/
public mixin template Mockable(C) if (is(C == class) || is(C == interface))
{
version(unittest):
/*
* Struct for holding method count information.
*/
private struct MethodCount
{
/**
* The expected minimum count of the method.
*/
public ulong minimum;
/**
* The expected maximum count of the method.
*/
public ulong maximum;
/**
* The actual count of the method.
*/
public ulong actual;
}
/*
* Struct for holding the location of the disableParentMethods method.
*/
private struct FileLocation
{
/**
* The name of the file.
*/
public string file;
/**
* The line number in the file.
*/
public size_t line;
}
/**
* Injected by the Mockable mixin template this method allows creation of mock object instances of the mockable class.
*
* Params:
* args = The constructor arguments of the host class.
*
* Example:
* ---
* import dunit.mockable;
*
* class T
* {
* mixin Mockable!(T);
* }
*
* unittest
* {
* import dunit.toolkit;
*
* auto mock = T.getMock();
*
* assertTrue(cast(T)mock); // Mock extends T.
* }
* ---
*
* Templates:
*
* Templated classes are supported when creating mocks.
* Simply include the template parameters for the class when calling the 'getMock' method.
* ---
* auto mock = T!(int).getMock(); // Get a mock of T!(int).
* ---
*/
static public auto getMock(A...)(A args)
{
return new Mock!(C)(args);
}
/**
* Injected by the Mockable mixin template this class contains all mocking behaviour.
*
* This Mock class extends any class it's injected into and provides methods to interact with the mocked instance.
* An instance of this class can dynamically replace any of its methods at runtime using the 'mockMethod' method.
* All mocked methods can optionally have their call counts asserted to be within set limits.
*/
private static class Mock(C) : C if (is(C == class) || is(C == interface))
{
import dunit.error;
import dunit.moduleunittester;
import std.range;
import std.string;
import std.traits;
/*
* The friendly class name.
*/
private enum string className = C.stringof;
/*
* Records the call limits and call counts of each method.
*/
private MethodCount[string] _methodCount;
/*
* Boolean representing whether to use the parent methods or not.
*/
private bool _useParentMethods = true;
/*
* A structure to hold file location for the disableParentMethods method.
*/
private FileLocation _disableMethodsLocation;
/**
* Inject the necessary class code.
*/
static if (is(C == class))
{
mixin(DUnitConstructorIterator!(C, "Constructor!(T, func)"));
}
mixin(DUnitMethodIterator!(C, "MethodDelegateProperty!(func)"));
mixin(DUnitMethodIterator!(C, "Method!(is(T == class), func)"));
/*
* Get the storage classes of the passed delegate.
*
* Params:
* method = The delegate to inspect.
*
* Returns:
* An array containing the storage classes of all delegate parameters.
*/
private string[] getStorageClasses(T)(T method)
{
string[] storageClasses;
string code;
foreach (storageClass; ParameterStorageClassTuple!(method))
{
code = "";
static if (storageClass == ParameterStorageClass.scope_)
{
code ~= "scope ";
}
static if (storageClass == ParameterStorageClass.lazy_)
{
code ~= "lazy ";
}
static if (storageClass == ParameterStorageClass.out_)
{
code ~= "out ";
}
static if (storageClass == ParameterStorageClass.ref_)
{
code ~= "ref ";
}
storageClasses ~= code;
}
return storageClasses;
}
/*
* Get the types of the passed delegate.
*
* Params:
* method = The delegate to inspect.
*
* Returns:
* An array containing the types of all delegate parameters.
*/
private string[] getTypes(T)(T method)
{
string[] types;
foreach (type; ParameterTypeTuple!(method))
{
types ~= type.stringof;
}
return types;
}
/*
* Generate a signature using the passed name and method. The signature is used to
* match up delegates to methods behind the scenes.
*
* Params:
* name = The name of the method to generate the signature for.
* method = A delegate to inspect, retreiving parameter details.
*
* Returns:
* A string containing a method signature.
*/
private string generateSignature(T)(string name, T method)
{
string[] storageClasses = this.getStorageClasses!(T)(method);
string[] types = this.getTypes!(T)(method);
string[] parameters;
foreach (storageClass, type; zip(storageClasses, types))
{
parameters ~= format("%s%s", storageClass, type);
}
return format("%s:%s(%s)", ReturnType!(method).stringof, name, parameters.join(", "));
}
/**
* Replace a method in the mock object by adding a mock method to be called in its place.
* $(B By default parent methods are called in lieu of any replacements unless parent methods are disabled.)
*
* Params:
* name = The name of the method to replace. (Only the name should be used, no parameters, etc.)
* delegate_ = The delegate to be used instead of calling the original method.
* The delegate must have the exact signature of the method being replaced.
* minimumCount = The minimum amount of times this method must be called when asserting calls.
* maximumCount = The maximum amount of times this method must be called when asserting calls.
* file = The file name where the error occurred. The value is added automatically at the call site.
* line = The line where the error occurred. The value is added automatically at the call site.
*
* Throws:
* DUnitAssertError if the passed delegate does not match the signature of any overload of the method named.
*
* Caveats:
* $(OL
* $(LI In the case of replacing overloaded methods the delegate signature defines which method to replace. Helpful errors will be raised on non matching signatures.)
* $(LI Templated methods are final by default and therefore cannot be mocked.)
* )
*
* See_Also:
* $(LINK2 mockable.html#disableParentMethods, disableParentMethods(...))
*
* Example:
* ---
* import dunit.mockable;
*
* class T
* {
* int getValue()
* {
* return 1;
* }
*
* mixin Mockable!(T);
* }
*
* unittest
* {
* import dunit.toolkit;
*
* auto mock = T.getMock();
*
* // Replace the 'getValue' method.
* mock.mockMethod("getValue", delegate(){
* return 2;
* });
*
* mock.getValue().assertEqual(2);
* }
* ---
*/
public void mockMethod(T)(string name, T delegate_, ulong minimumCount = 0, ulong maximumCount = ulong.max, string file = __FILE__, size_t line = __LINE__)
{
string signature = this.generateSignature!(T)(name, delegate_);
this._methodCount[signature] = MethodCount(minimumCount, maximumCount);
switch(signature)
{
mixin(DUnitMethodIterator!(C, "MethodSignatureSwitch!(func)"));
default:
auto error = new DUnitAssertError("Delegate does not match method signature", file, line);
error.addInfo("Method name", format("%s.%s", this.className, name));
error.addError("Delegate signature", signature);
throw error;
}
}
/**
* Disable parent methods being called if mock replacements are not implemented.
* If parent methods have been disabled a helpful assert error will be raised on any attempt to call methods that haven't been replaced.
* This is helpful if it's necessary to disable all behaviour of the mocked class.
*
* Has no effect on mock objects derived from interfaces, by default all mocked interface methods assert an error until replaced.
*
* Params:
* file = The file name where the error occurred. The value is added automatically at the call site.
* line = The line where the error occurred. The value is added automatically at the call site.
*
* See_Also:
* $(LINK2 mockable.html#mockMethod, mockMethod(...))
*
* Example:
* ---
* import dunit.mockable;
*
* class T
* {
* mixin Mockable!(T);
* }
*
* unittest
* {
* auto mock = T.getMock();
* mock.disableParentMethods();
*
* // All mock object methods that are used in the test
* // must now be replaced to avoid an error being thrown.
* }
* ---
*/
public void disableParentMethods(string file = __FILE__, size_t line = __LINE__)
{
this._disableMethodsLocation.file = file;
this._disableMethodsLocation.line = line;
this._useParentMethods = false;
}
/**
* Assert all replaced methods are called the defined amount of times.
*
* Params:
* message = The error message to display.
* file = The file name where the error occurred. The value is added automatically at the call site.
* line = The line where the error occurred. The value is added automatically at the call site.
*
* Throws:
* DUnitAssertError if any method was called outside of preset boundries.
*
* Example:
* ---
* import dunit.mockable;
*
* class T
* {
* int getValue()
* {
* return 1;
* }
*
* mixin Mockable!(T);
* }
*
* unittest
* {
* import dunit.toolkit;
*
* auto mock = T.getMock();
*
* // Replace method while defining a minimum call limit.
* mock.mockMethod("getValue", delegate(){
* return 2;
* }, 1);
*
* // Increase the call count of 'getValue' by one.
* mock.getValue().assertEqual(2);
*
* // Assert methods calls are within defined limits.
* mock.assertMethodCalls();
* }
* ---
*/
public void assertMethodCalls(string message = "Failed asserting call count", string file = __FILE__, size_t line = __LINE__)
{
foreach (signature, methodCount; this._methodCount)
{
if (methodCount.actual < methodCount.minimum)
{
auto error = new DUnitAssertError(message, file, line);
error.addInfo("Method", format("%s.%s", this.className, signature));
error.addExpectation("Minimum allowed calls", methodCount.minimum);
error.addError("Actual calls", methodCount.actual);
throw error;
}
if (methodCount.actual > methodCount.maximum)
{
auto error = new DUnitAssertError(message, file, line);
error.addInfo("Method", format("%s.%s", this.className, signature));
error.addExpectation("Maximum allowed calls", methodCount.maximum);
error.addError("Actual calls", methodCount.actual);
throw error;
}
}
}
}
}
unittest
{
import dunit.toolkit;
interface T
{
public int method1(int param) const;
public void method2(int param) const;
public int method3(int param) const pure @safe nothrow;
public void method4(int param) const pure @safe nothrow;
public int method5(int param) const pure @trusted nothrow;
public void method6(int param) const pure @trusted nothrow;
mixin Mockable!T;
}
auto mock = T.getMock();
mock.mockMethod("method1", delegate(int param) { return 2*param; });
mock.mockMethod("method2", delegate(int param) { });
mock.mockMethod("method3", delegate(int param) { return 2*param; });
mock.mockMethod("method4", delegate(int param) { });
mock.mockMethod("method5", delegate(int param) { return 2*param; });
mock.mockMethod("method6", delegate(int param) { });
mock.method1(10).assertEqual(20);
mock.method2(10);
mock.method3(10).assertEqual(20);
mock.method4(10);
mock.method5(10).assertEqual(20);
mock.method6(10);
}
unittest
{
import dunit.toolkit;
static class T
{
public int method1(int param) const { return 0; };
public void method2(int param) const {};
public int method3(int param) const pure @safe nothrow { return 0; };
public void method4(int param) const pure @safe nothrow {};
public int method5(int param) const pure @trusted nothrow { return 0; };
public void method6(int param) const pure @trusted nothrow {};
mixin Mockable!T;
}
auto mock = T.getMock();
mock.mockMethod("method1", delegate(int param) { return 2*param; });
mock.mockMethod("method2", delegate(int param) { });
mock.mockMethod("method3", delegate(int param) { return 2*param; });
mock.mockMethod("method4", delegate(int param) { });
mock.mockMethod("method5", delegate(int param) { return 2*param; });
mock.mockMethod("method6", delegate(int param) { });
mock.method1(10).assertEqual(20);
mock.method2(10);
mock.method3(10).assertEqual(20);
mock.method4(10);
mock.method5(10).assertEqual(20);
mock.method6(10);
}
unittest
{
import dunit.toolkit;
static class T
{
public int method1() nothrow
{
assert(false, "thrown from method1");
}
public void method2()
{
throw new Exception("thrown from method2");
}
mixin Mockable!(T);
}
auto mock = T.getMock();
mock.method1().assertThrow!Throwable("thrown from method1");
mock.method2().assertThrow!Exception("thrown from method2");
}