forked from VSoftTechnologies/Delphi-Mocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelphi.Mocks.MethodData.pas
538 lines (470 loc) · 18.5 KB
/
Delphi.Mocks.MethodData.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
{***************************************************************************}
{ }
{ 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.MethodData;
{$I 'Delphi.Mocks.inc'}
interface
uses
Rtti,
SysUtils,
Generics.Collections,
Delphi.Mocks,
Delphi.Mocks.Interfaces;
type
TMethodData = class(TInterfacedObject,IMethodData)
private
FTypeName : string;
FMethodName : string;
FBehaviors : TList<IBehavior>;
FReturnDefault : TValue;
FExpectations : TList<IExpectation>;
FIsStub : boolean;
FBehaviorMustBeDefined: boolean;
protected
//Behaviors
procedure WillReturnDefault(const returnValue : TValue);
procedure WillReturnWhen(const Args: TArray<TValue>; const returnValue : TValue);
procedure WillRaiseAlways(const exceptionClass : ExceptClass; const message : string);
procedure WillRaiseWhen(const exceptionClass : ExceptClass; const message : string;const Args: TArray<TValue>);
procedure WillExecute(const func : TExecuteFunc);
procedure WillExecuteWhen(const func : TExecuteFunc; const Args: TArray<TValue>);
function FindBehavior(const behaviorType : TBehaviorType; const Args: TArray<TValue>) : IBehavior;overload;
function FindBehavior(const behaviorType : TBehaviorType) : IBehavior; overload;
function FindBestBehavior(const Args: TArray<TValue>) : IBehavior;
procedure RecordHit(const Args: TArray<TValue>; const returnType : TRttiType; out Result : TValue);
//Expectations
function FindExpectation(const expectationType : TExpectationType; const Args: TArray<TValue>) : IExpectation;overload;
function FindExpectation(const expectationTypes : TExpectationTypes) : IExpectation;overload;
procedure OnceWhen(const Args : TArray<TValue>);
procedure Once;
procedure NeverWhen(const Args : TArray<TValue>);
procedure Never;
procedure AtLeastOnceWhen(const Args : TArray<TValue>);
procedure AtLeastOnce;
procedure AtLeastWhen(const times : Cardinal; const Args : TArray<TValue>);
procedure AtLeast(const times : Cardinal);
procedure AtMostWhen(const times : Cardinal; const Args : TArray<TValue>);
procedure AtMost(const times : Cardinal);
procedure BetweenWhen(const a,b : Cardinal; const Args : TArray<TValue>);
procedure Between(const a,b : Cardinal);
procedure ExactlyWhen(const times : Cardinal; const Args : TArray<TValue>);
procedure Exactly(const times : Cardinal);
procedure BeforeWhen(const ABeforeMethodName : string ; const Args : TArray<TValue>);
procedure Before(const ABeforeMethodName : string);
procedure AfterWhen(const AAfterMethodName : string;const Args : TArray<TValue>);
procedure After(const AAfterMethodName : string);
function Verify(var report : string) : boolean;
public
constructor Create(const ATypeName : string; const AMethodName : string; const AIsStub : boolean; const ABehaviorMustBeDefined: boolean);
destructor Destroy;override;
end;
{$IFNDEF DELPHI_XE_UP}
ENotImplemented = class(Exception);
{$ENDIF}
implementation
uses
Delphi.Mocks.Utils,
Delphi.Mocks.Behavior,
Delphi.Mocks.Expectation;
{ TMethodData }
constructor TMethodData.Create(const ATypeName : string; const AMethodName : string; const AIsStub : boolean; const ABehaviorMustBeDefined: boolean);
begin
FTypeName := ATypeName;
FMethodName := AMethodName;
FBehaviors := TList<IBehavior>.Create;
FExpectations := TList<IExpectation>.Create;
FReturnDefault := TValue.Empty;
FBehaviorMustBeDefined := ABehaviorMustBeDefined;
FIsStub := AIsStub;
end;
destructor TMethodData.Destroy;
begin
FBehaviors.Free;
FExpectations.Free;
inherited;
end;
procedure TMethodData.Exactly(const times: Cardinal);
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.Exactly,TExpectationType.ExactlyWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Exactly for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateExactly(FMethodName,times);
FExpectations.Add(expectation);
end;
procedure TMethodData.ExactlyWhen(const times: Cardinal;
const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.ExactlyWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Exactly for method [%s] with args.', [FTypeName, FMethodName]));
expectation := TExpectation.CreateExactlyWhen(FMethodName,times,Args);
FExpectations.Add(expectation);
end;
function TMethodData.FindBehavior(const behaviorType: TBehaviorType; const Args: TArray<TValue>): IBehavior;
var
behavior : IBehavior;
begin
result := nil;
for behavior in FBehaviors do
begin
if behavior.BehaviorType = behaviorType then
begin
if behavior.Match(Args) then
begin
result := behavior;
exit;
end;
end;
end;
end;
function TMethodData.FindBehavior(const behaviorType: TBehaviorType): IBehavior;
var
behavior : IBehavior;
begin
result := nil;
for behavior in FBehaviors do
begin
if behavior.BehaviorType = behaviorType then
begin
result := behavior;
exit;
end;
end;
end;
function TMethodData.FindBestBehavior(const Args: TArray<TValue>): IBehavior;
begin
//First see if we have an always throws;
result := FindBehavior(TBehaviorType.WillRaiseAlways);
if Result <> nil then
exit;
//then find an always execute
result := FindBehavior(TBehaviorType.WillExecute);
if Result <> nil then
exit;
result := FindBehavior(TBehaviorType.WillExecuteWhen,Args);
if Result <> nil then
exit;
result := FindBehavior(TBehaviorType.WillReturn,Args);
if Result <> nil then
exit;
result := FindBehavior(TBehaviorType.ReturnDefault,Args);
if Result <> nil then
exit;
result := nil;
end;
function TMethodData.FindExpectation(const expectationType : TExpectationType; const Args: TArray<TValue>): IExpectation;
var
expectation : IExpectation;
begin
result := nil;
for expectation in FExpectations do
begin
if expectation.ExpectationType = expectationType then
begin
if expectation.Match(Args) then
begin
result := expectation;
exit;
end;
end;
end;
end;
function TMethodData.FindExpectation(const expectationTypes : TExpectationTypes): IExpectation;
var
expectation : IExpectation;
begin
result := nil;
for expectation in FExpectations do
begin
if expectation.ExpectationType in expectationTypes then
begin
result := expectation;
exit;
end;
end;
end;
procedure TMethodData.After(const AAfterMethodName: string);
begin
raise ENotImplemented.Create('After not implented');
end;
procedure TMethodData.AfterWhen(const AAfterMethodName: string;const Args: TArray<TValue>);
begin
raise ENotImplemented.Create('AfterWhen not implented');
end;
procedure TMethodData.AtLeast(const times: Cardinal);
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.AtLeast,TExpectationType.AtLeastOnce,TExpectationType.AtLeastOnceWhen,TExpectationType.AtLeastWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation At Least for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateAtLeast(FMethodName,times);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtLeastOnce;
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.AtLeast,TExpectationType.AtLeastOnce,TExpectationType.AtLeastOnceWhen,TExpectationType.AtLeastWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation At Least Once for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateAtLeastOnce(FMethodName);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtLeastOnceWhen(const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.AtLeastOnceWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation At Least Once When for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateAtLeastOnceWhen(FMethodName,Args);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtLeastWhen(const times: Cardinal; const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.AtLeastWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation At Least When for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateAtLeastWhen(FMethodName,times,Args);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtMost(const times: Cardinal);
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.AtMost,TExpectationType.AtMostWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation At Most for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateAtMost(FMethodName,times);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtMostWhen(const times: Cardinal;
const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.AtMostWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation At Most When for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateAtMostWhen(FMethodName,times,Args);
FExpectations.Add(expectation);
end;
procedure TMethodData.Before(const ABeforeMethodName: string);
begin
raise ENotImplemented.Create('Before not implented');
end;
procedure TMethodData.BeforeWhen(const ABeforeMethodName: string; const Args: TArray<TValue>);
begin
raise ENotImplemented.Create('BeforeWhen not implented');
end;
procedure TMethodData.Between(const a, b: Cardinal);
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.Between,TExpectationType.BetweenWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Between for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateBetween(FMethodName,a,b);
FExpectations.Add(expectation);
end;
procedure TMethodData.BetweenWhen(const a, b: Cardinal;const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.BetweenWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Between When for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateBetweenWhen(FMethodName,a,b,Args);
FExpectations.Add(expectation);
end;
procedure TMethodData.Never;
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.Never ,TExpectationType.NeverWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Never for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateNever(FMethodName);
FExpectations.Add(expectation);
end;
procedure TMethodData.NeverWhen(const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.NeverWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Never When for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateNeverWhen(FMethodName,Args);
FExpectations.Add(expectation);
end;
procedure TMethodData.Once;
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.Once,TExpectationType.OnceWhen]);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Once for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateOnce(FMethodName);
FExpectations.Add(expectation);
end;
procedure TMethodData.OnceWhen(const Args: TArray<TValue>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.OnceWhen,Args);
if expectation <> nil then
raise EMockException.Create(Format('[%s] already defines Expectation Once When for method [%s]', [FTypeName, FMethodName]));
expectation := TExpectation.CreateOnceWhen(FMethodName,Args);
FExpectations.Add(expectation);
end;
procedure TMethodData.RecordHit(const Args: TArray<TValue>; const returnType : TRttiType; out Result: TValue);
var
behavior : IBehavior;
returnVal : TValue;
expectation : IExpectation;
expectationHitCtr: integer;
begin
expectationHitCtr := 0;
for expectation in FExpectations do
begin
if expectation.Match(Args) then
begin
expectation.RecordHit;
inc(expectationHitCtr);
end;
end;
behavior := FindBestBehavior(Args);
if behavior <> nil then
returnVal := behavior.Execute(Args,returnType)
else
begin
if (returnType <> nil) and (FReturnDefault.IsEmpty) then
begin
if FIsStub then
begin
//Stubs return default values.
result := GetDefaultValue(returnType);
end
else
begin
//If it's not a stub then we say we didn't have a default return value
raise EMockException.Create(Format('[%s] has no default return value defined for method [%s]', [FTypeName, FMethodName]));
end;
end
else if FBehaviorMustBeDefined and (expectationHitCtr = 0) and (FReturnDefault.IsEmpty) then
begin
//If we must have default behaviour defined, and there was nothing defined raise a mock exception.
raise EMockException.Create(Format('[%s] has no behaviour or expectation defined for method [%s]', [FTypeName, FMethodName]));
end;
returnVal := FReturnDefault;
end;
if returnType <> nil then
Result := returnVal;
end;
function TMethodData.Verify(var report : string) : boolean;
var
expectation : IExpectation;
begin
result := true;
report := '';
for expectation in FExpectations do
begin
if not expectation.ExpectationMet then
begin
result := False;
if report <> '' then
report := report + #13#10 + ' '
else
report := ' ';
report := report + expectation.Report;
end;
end;
if not result then
report := ' Method : ' + FMethodName + #13#10 + report;
end;
//Behaviors
procedure TMethodData.WillExecute(const func: TExecuteFunc);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillExecute);
if behavior <> nil then
raise EMockSetupException.Create(Format('[%s] already defines WillExecute for method [%s]', [FTypeName, FMethodName]));
behavior := TBehavior.CreateWillExecute(func);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillExecuteWhen(const func: TExecuteFunc;const Args: TArray<TValue>);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillExecuteWhen,Args);
if behavior <> nil then
raise EMockSetupException.Create(Format('[%s] already defines WillExecute When for method [%s]', [FTypeName, FMethodName]));
behavior := TBehavior.CreateWillExecuteWhen(Args, func);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillRaiseAlways(const exceptionClass: ExceptClass; const message : string);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillRaiseAlways);
if behavior <> nil then
raise EMockSetupException.Create(Format('[%s] already defines Will Raise Always for method [%s]', [FTypeName, FMethodName]));
behavior := TBehavior.CreateWillRaise(exceptionClass,message);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillRaiseWhen(const exceptionClass: ExceptClass; const message : string; const Args: TArray<TValue>);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillRaise,Args);
if behavior <> nil then
raise EMockSetupException.Create(Format('[%s] already defines Will Raise When for method [%s]', [FTypeName, FMethodName]));
behavior := TBehavior.CreateWillRaiseWhen(Args,exceptionClass,message);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillReturnDefault(const returnValue: TValue);
begin
if not FReturnDefault.IsEmpty then
raise EMockSetupException.Create(Format('[%s] already defines Will Return Default for method [%s]', [FTypeName, FMethodName]));
FReturnDefault := returnValue;
end;
procedure TMethodData.WillReturnWhen(const Args: TArray<TValue>; const returnValue: TValue);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillReturn,Args);
if behavior <> nil then
raise EMockSetupException.Create(Format('[%s] already defines Will Return When for method [%s]', [FTypeName, FMethodName]));
behavior := TBehavior.CreateWillReturnWhen(Args,returnValue);
FBehaviors.Add(behavior);
end;
end.