forked from VSoftTechnologies/Delphi-Mocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelphi.Mocks.pas
504 lines (413 loc) · 17.1 KB
/
Delphi.Mocks.pas
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
{***************************************************************************}
{ }
{ Delphi.Mocks }
{ }
{ Copyright (C) 2011 Vincent Parrett }
{ }
{ http://www.finalbuilder.com }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit Delphi.Mocks;
interface
{$I 'Delphi.Mocks.inc'}
uses
TypInfo,
Rtti,
sysutils,
Delphi.Mocks.WeakReference;
type
IWhen<T> = interface;
//Records the expectations we have when our Mock is used. We can then verify
//our expectations later.
IExpect<T> = interface
['{8B9919F1-99AB-4526-AD90-4493E9343083}']
function Once : IWhen<T>;overload;
procedure Once(const AMethodName : string);overload;
function Never : IWhen<T>;overload;
procedure Never(const AMethodName : string);overload;
function AtLeastOnce : IWhen<T>;overload;
procedure AtLeastOnce(const AMethodName : string);overload;
function AtLeast(const times : Cardinal) : IWhen<T>;overload;
procedure AtLeast(const AMethodName : string; const times : Cardinal);overload;
function AtMost(const times : Cardinal) : IWhen<T>;overload;
procedure AtMost(const AMethodName : string; const times : Cardinal);overload;
function Between(const a,b : Cardinal) : IWhen<T>;overload;
procedure Between(const AMethodName : string; const a,b : Cardinal);overload;
function Exactly(const times : Cardinal) : IWhen<T>;overload;
procedure Exactly(const AMethodName : string; const times : Cardinal);overload;
function Before(const AMethodName : string) : IWhen<T>;overload;
procedure Before(const AMethodName : string; const ABeforeMethodName : string);overload;
function After(const AMethodName : string) : IWhen<T>;overload;
procedure After(const AMethodName : string; const AAfterMethodName : string);overload;
end;
IWhen<T> = interface
['{A8C2E07B-A5C1-463D-ACC4-BA2881E8419F}']
function When : T;
end;
/// This is the definition for an anonymous function you can pass into
/// WillExecute. The args array will be the arguments passed to the method
/// called on the Mock. If the method returns a value then your anon func must
/// return that.. and the return type must match. The return type is passed in
/// so that you can ensure tha.
TExecuteFunc = reference to function (const args : TArray<TValue>; const ReturnType : TRttiType) : TValue;
IStubSetup<T> = interface
['{3E6AD69A-11EA-47F1-B5C3-63F7B8C265B1}']
function GetBehaviorMustBeDefined : boolean;
procedure SetBehaviorMustBeDefined(const value : boolean);
//set the return value for a method when called with the parameters specified on the When
function WillReturn(const value : TValue) : IWhen<T>;
//Will exedute the func when called with the specified parameters
function WillExecute(const func : TExecuteFunc) : IWhen<T>;overload;
//will always execute the func no matter what parameters are specified.
procedure WillExecute(const AMethodName : string; const func : TExecuteFunc);overload;
//set the default return value for a method when it is called with parameter values we
//haven't specified
procedure WillReturnDefault(const AMethodName : string; const value : TValue);
//set the Exception class that will be raised when the method is called with the parmeters specified
function WillRaise(const exceptionClass : ExceptClass; const message : string = '') : IWhen<T>;overload;
//This method will always raise an exception.. this behavior will trump any other defined behaviors
procedure WillRaise(const AMethodName : string; const exceptionClass : ExceptClass; const message : string = '');overload;
//If true, calls to methods for which we have not defined a behavior will cause verify to fail.
property BehaviorMustBeDefined : boolean read GetBehaviorMustBeDefined write SetBehaviorMustBeDefined;
end;
//We use the Setup to configure our expected behaviour rules and to verify
//that those expectations were met.
IMockSetup<T> = interface(IStubSetup<T>)
['{D6B21933-BF51-4937-877E-51B59A3B3268}']
//Set Expectations for methods
function Expect : IExpect<T>;
end;
IStubProxy<T> = interface
['{578BAF90-4155-4C0F-AAED-407057C6384F}']
function Setup : IStubSetup<T>;
function Proxy : T;
end;
IProxy = interface(IWeakReferenceableObject)
['{C97DC7E8-BE99-46FE-8488-4B356DD4AE29}']
function ProxyInterface : IInterface;
function ProxyFromType(const ATypeInfo : PTypeInfo) : IProxy;
procedure AddImplement(const AProxy : IProxy; const ATypeInfo : PTypeInfo);
function QueryImplementedInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
procedure SetParentProxy(const AProxy : IProxy);
end;
//used by the mock - need to find another place to put this.. circular references
//problem means we need it here for now.
IProxy<T> = interface(IProxy)
['{1E3A98C5-78BA-4D65-A4BA-B6992B8B4783}']
function Setup : IMockSetup<T>;
function Proxy : T;
end;
TStub<T> = record
private
FProxy : IStubProxy<T>;
public
class operator Implicit(const Value: TStub<T>): T;
function Setup : IStubSetup<T>;
function Instance : T;
function InstanceAsValue : TValue;
class function Create: TStub<T>; static;
// explicit cleanup. Not sure if we really need this.
procedure Free;
end;
//We use a record here to take advantage of operator overloading, the Implicit
//operator allows us to use the mock as the interface without holding a reference
//to the mock interface outside of the mock.
TMock<T> = record
private
FProxy : IProxy<T>;
class procedure CheckMockType(const ATypeInfo : PTypeInfo); static;
class procedure CheckMockInterface(const ATypeInfo : PTypeInfo); static;
class procedure CheckMockObject(const ATypeInfo : PTypeInfo); static;
public
class operator Implicit(const Value: TMock<T>): T;
function Setup : IMockSetup<T>; overload;
function Setup<I : IInterface> : IMockSetup<I>; overload;
//Verify that our expectations were met.
procedure Verify(const message : string = ''); overload;
procedure Verify<I : IInterface>(const message : string = ''); overload;
procedure VerifyAll(const message : string = '');
function CheckExpectations: string;
procedure Implement<I : IInterface>; overload;
function Instance : T; overload;
function Instance<I : IInterface> : I; overload;
function InstanceAsValue : TValue;
class function Create: TMock<T>; static;
// explicit cleanup. Not sure if we really need this.
procedure Free;
end;
//Exception Types that the mocks will raise.
EMockException = class(Exception);
EMockProxyAlreadyImplemented = class(EMockException);
EMockSetupException = class(EMockException);
EMockNoRTTIException = class(EMockException);
EMockNoProxyException = class(EMockException);
EMockVerificationException = class(EMockException);
TTypeInfoHelper = record helper for TTypeInfo
function NameStr : string; inline;
end;
implementation
uses
Classes,
Generics.Defaults,
Delphi.Mocks.Utils,
Delphi.Mocks.Interfaces,
Delphi.Mocks.Proxy,
Delphi.Mocks.ObjectProxy;
function TMock<T>.CheckExpectations: string;
var
su : IMockSetup<T>;
v : IVerify;
begin
if Supports(FProxy.Setup,IVerify,v) then
Result := v.CheckExpectations
else
raise EMockException.Create('Could not cast Setup to IVerify interface!');
end;
class function TMock<T>.Create: TMock<T>;
var
proxy : IInterface;
pInfo : PTypeInfo;
begin
//Make sure that we start off with a clean mock
FillChar(Result, SizeOf(Result), 0);
pInfo := TypeInfo(T);
//Raise exceptions if the mock doesn't meet the requirements.
CheckMockType(pInfo);
case pInfo.Kind of
//Create our proxy object, which will implement our object T
tkClass : proxy := TObjectProxy<T>.Create(false);
//Create our proxy interface object, which will implement our interface T
tkInterface : proxy := TProxy<T>.Create(false);
end;
//Push the proxy into the result we are returning.
if proxy.QueryInterface(GetTypeData(TypeInfo(IProxy<T>)).Guid, Result.FProxy) <> 0 then
//TODO: This raise seems superfluous as the only types which are created are controlled by us above. They all implement IProxy<T>
raise EMockNoProxyException.Create('Error casting to interface ' + pInfo.NameStr + ' , proxy does not appear to implememnt IProxy<T>');
end;
procedure TMock<T>.Free;
begin
FProxy := nil;
end;
procedure TMock<T>.Implement<I>;
var
proxy : IProxy<I>;
pInfo : PTypeInfo;
begin
if FProxy is TObjectProxy<T> then
raise ENotSupportedException.Create('Adding interface implementation to non interfaced objects not supported at this time');
pInfo := TypeInfo(I);
CheckMockInterface(pInfo);
proxy := TProxy<I>.Create;
FProxy.AddImplement(proxy, pInfo);
end;
class operator TMock<T>.Implicit(const Value: TMock<T>): T;
begin
result := Value.FProxy.Proxy;
end;
function TMock<T>.Instance : T;
var
pInfo : PTypeInfo;
begin
if FProxy = nil then
begin
pInfo := TypeInfo(T);
raise EMockException.Create(format('Mock [%s] did not have create called before its use.', [pInfo.Name]));
end;
result := FProxy.Proxy;
end;
function TMock<T>.Instance<I>: I;
var
prox : IInterface;
proxyI : IProxy<I>;
pInfo : PTypeInfo;
begin
result := nil;
//Does the proxy we have, or any of its children support a proxy for the passed
//in interface type?
pInfo := TypeInfo(I);
prox := FProxy.ProxyFromType(pInfo);
if prox = nil then
raise EMockException.CreateFmt('Mock does not implement [%s]', [pInfo.NameStr]);
if (prox = nil) or (not Supports(prox, IProxy<I>, proxyI)) then
raise EMockException.CreateFmt('Proxy for [%s] does not support [IProxy<T>].', [pInfo.NameStr]);
//Return the interface for the requested implementation.
result := proxyI.Proxy;
end;
function TMock<T>.InstanceAsValue: TValue;
begin
result := TValue.From<T>(Self);
end;
function TMock<T>.Setup: IMockSetup<T>;
begin
result := FProxy.Setup;
end;
{$O-}
function TMock<T>.Setup<I>: IMockSetup<I>;
var
setup : IProxy;
pInfo : PTypeInfo;
pMockSetupInfo : PTypeInfo;
begin
//We have to ask for the proxy who owns the implementation of the interface/object
//in question. The reason for this it that all proxies implement IProxy<T> and
//therefore we will just get the first proxy always.
//E.g. IProxy<IInterfaceOne> and IProxy<IInterfaceTwo> have the same GUID. Makes
//generic interfaces hard to use.
pInfo := TypeInfo(I);
//Get the proxy which implements
setup := FProxy.ProxyFromType(pInfo);
//If nill is returned then we don't implement the defined type.
if setup = nil then
raise EMockNoProxyException.CreateFmt('[%s] is not implement.', [pInfo.NameStr]);
//Now get it as the mocksetup that we requrie. Note that this doesn't ensure
//that I is actually implemented as all proxies implment IMockSetup<I>. This
//is what we only return the error that IMockSetup isn't implemented.
if not Supports(setup, IMockSetup<I>, result) then
begin
pMockSetupInfo := TypeInfo(IMockSetup<I>);
raise EMockNoProxyException.CreateFmt('[%s] Proxy does not implement [%s]', [pInfo.NameStr, pMockSetupInfo.NameStr]);
end;
end;
{$O+}
class procedure TMock<T>.CheckMockInterface(const ATypeInfo : PTypeInfo);
begin
//Check to make sure we have
if not CheckInterfaceHasRTTI(ATypeInfo) then
raise EMockNoRTTIException.Create(ATypeInfo.NameStr + ' does not have RTTI, specify {$M+} for the interface to enabled RTTI');
end;
class procedure TMock<T>.CheckMockObject(const ATypeInfo: PTypeInfo);
begin
//Check to make sure we have
if not CheckClassHasRTTI(ATypeInfo) then
raise EMockNoRTTIException.Create(ATypeInfo.NameStr + ' does not have RTTI, specify {$M+} for the object to enabled RTTI');
end;
class procedure TMock<T>.CheckMockType(const ATypeInfo: PTypeInfo);
begin
if not (ATypeInfo.Kind in [tkInterface,tkClass]) then
raise EMockException.Create(ATypeInfo.NameStr + ' is not an Interface or Class. TMock<T> supports interfaces and classes only');
case ATypeInfo.Kind of
//NOTE: We have a weaker requirement for an object proxy opposed to an interface proxy.
//NOTE: Object proxy doesn't require more than zero methods on the object.
tkClass : CheckMockObject(ATypeInfo);
tkInterface : CheckMockInterface(ATypeInfo);
else
raise EMockException.Create('Invalid type kind T');
end;
end;
procedure TMock<T>.Verify(const message: string);
var
v : IVerify;
begin
if Supports(FProxy.Setup, IVerify, v) then
v.Verify(message)
else
raise EMockException.Create('Could not cast Setup to IVerify interface!');
end;
{$O-}
procedure TMock<T>.Verify<I>(const message: string);
var
prox : IInterface;
interfaceV : IVerify;
pInfo : PTypeInfo;
begin
//Does the proxy we have, or any of its children support a proxy for the passed
//in interface type?
pInfo := TypeInfo(I);
prox := FProxy.ProxyFromType(pInfo);
if (prox = nil) or (not Supports(prox, IVerify, interfaceV)) then
raise EMockException.Create('Could not cast Setup to IVerify interface!');
interfaceV.Verify(message);
end;
{$O+}
procedure TMock<T>.VerifyAll(const message: string);
var
interfaceV : IVerify;
begin
if Supports(FProxy.Setup, IVerify, interfaceV) then
interfaceV.VerifyAll(message)
else
raise EMockException.Create('Could not cast Setup to IVerify interface!');
end;
{ TStub<T> }
class function TStub<T>.Create: TStub<T>;
var
proxy : IInterface;
pInfo : PTypeInfo;
begin
//Make sure that we start off with a clean mock
FillChar(Result, SizeOf(Result), 0);
pInfo := TypeInfo(T);
if not (pInfo.Kind in [tkInterface,tkClass]) then
raise EMockException.Create(pInfo.NameStr + ' is not an Interface or Class. TStub<T> supports interfaces and classes only');
case pInfo.Kind of
//NOTE: We have a weaker requirement for an object proxy opposed to an interface proxy.
//NOTE: Object proxy doesn't require more than zero methods on the object.
tkClass :
begin
//Check to make sure we have
if not CheckClassHasRTTI(pInfo) then
raise EMockNoRTTIException.Create(pInfo.NameStr + ' does not have RTTI, specify {$M+} for the object to enabled RTTI');
//Create our proxy object, which will implement our object T
proxy := TObjectProxy<T>.Create(true);
end;
tkInterface :
begin
//Check to make sure we have
if not CheckInterfaceHasRTTI(pInfo) then
raise EMockNoRTTIException.Create(pInfo.NameStr + ' does not have RTTI, specify {$M+} for the interface to enabled RTTI');
//Create our proxy interface object, which will implement our interface T
proxy := TProxy<T>.Create(true);
end;
else
raise EMockException.Create('Invalid type kind T');
end;
//Push the proxy into the result we are returning.
if proxy.QueryInterface(GetTypeData(TypeInfo(IStubProxy<T>)).Guid, Result.FProxy) <> 0 then
//TODO: This raise seems superfluous as the only types which are created are controlled by us above. They all implement IProxy<T>
raise EMockNoProxyException.Create('Error casting to interface ' + pInfo.NameStr + ' , proxy does not appear to implememnt IProxy<T>');
end;
procedure TStub<T>.Free;
begin
FProxy := nil;
end;
class operator TStub<T>.Implicit(const Value: TStub<T>): T;
begin
result := Value.FProxy.Proxy;
end;
function TStub<T>.Instance: T;
begin
result := FProxy.Proxy;
end;
function TStub<T>.InstanceAsValue: TValue;
begin
result := TValue.From<T>(Self);
end;
function TStub<T>.Setup: IStubSetup<T>;
begin
result := FProxy.Setup;
end;
{ TTypeInfoHelper }
function TTypeInfoHelper.NameStr: string;
begin
{$IFNDEF NEXTGEN}
result := string(Self.Name);
{$ELSE}
result := Self.NameFld.ToString;
{$ENDIF}
end;
end.