This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
forked from VSoftTechnologies/Delphi-Mocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delphi.Mocks.MethodData.pas
657 lines (561 loc) · 24.9 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
{***************************************************************************}
{ }
{ 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,
Delphi.Mocks.ParamMatcher;
type
TSetupMethodDataParameters = record
BehaviorMustBeDefined : boolean;
AllowRedefineBehaviorDefinitions: boolean;
IsStub: boolean;
class function Create(const AIsStub: boolean; const ABehaviorMustBeDefined, AAllowRedefineBehaviorDefinitions: boolean): TSetupMethodDataParameters; static;
end;
TMethodData = class(TInterfacedObject,IMethodData)
private
FTypeName : string;
FMethodName : string;
FBehaviors : TList<IBehavior>;
FReturnDefault : TValue;
FExpectations : TList<IExpectation>;
FSetupParameters: TSetupMethodDataParameters;
FAutoMocker : IAutoMock;
procedure StubNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType : TRttiType; out Result : TValue);
procedure MockNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType : TRttiType; out Result : TValue);
protected
//Behaviors
procedure WillReturnDefault(const returnValue : TValue);
procedure WillReturnWhen(const Args: TArray<TValue>; const returnValue : TValue; const matchers : TArray<IMatcher>);
procedure WillRaiseAlways(const exceptionClass : ExceptClass; const message : string);
procedure WillRaiseWhen(const exceptionClass : ExceptClass; const message : string;const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
procedure WillExecute(const func : TExecuteFunc);
procedure WillExecuteWhen(const func : TExecuteFunc; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
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>; const matchers : TArray<IMatcher>);
procedure Once;
procedure NeverWhen(const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Never;
procedure AtLeastOnceWhen(const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure AtLeastOnce;
procedure AtLeastWhen(const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure AtLeast(const times : Cardinal);
procedure AtMostWhen(const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure AtMost(const times : Cardinal);
procedure BetweenWhen(const a,b : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Between(const a,b : Cardinal);
procedure ExactlyWhen(const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Exactly(const times : Cardinal);
procedure BeforeWhen(const ABeforeMethodName : string ; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Before(const ABeforeMethodName : string);
procedure AfterWhen(const AAfterMethodName : string;const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure After(const AAfterMethodName : string);
function Verify(var report : string) : boolean;
public
constructor Create(const ATypeName : string; const AMethodName : string; const ASetupParameters: TSetupMethodDataParameters; const AAutoMocker : IAutoMock = nil);
destructor Destroy;override;
end;
{$IFNDEF DELPHI_XE_UP}
ENotImplemented = class(Exception);
{$ENDIF}
implementation
uses
Windows,
System.TypInfo,
Delphi.Mocks.Utils,
Delphi.Mocks.Behavior,
Delphi.Mocks.Expectation;
{ TMethodData }
constructor TMethodData.Create(const ATypeName : string; const AMethodName : string; const ASetupParameters: TSetupMethodDataParameters; const AAutoMocker : IAutoMock = nil);
begin
FTypeName := ATypeName;
FMethodName := AMethodName;
FBehaviors := TList<IBehavior>.Create;
FExpectations := TList<IExpectation>.Create;
FReturnDefault := TValue.Empty;
FSetupParameters := ASetupParameters;
FAutoMocker := AAutoMocker;
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) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Exactly for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateExactly(FMethodName,times);
FExpectations.Add(expectation);
end;
procedure TMethodData.ExactlyWhen(const times: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.ExactlyWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Exactly for method [%s] with args.', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateExactlyWhen(FMethodName, times, Args, matchers);
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.MockNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType: TRttiType; out Result: TValue);
var
behavior : IBehavior;
mock : IProxy;
begin
Result := TValue.Empty;
//If auto mocking has been turned on and this return type is either a class or interface, mock it.
if FAutoMocker <> nil then
begin
//TODO: Add more options for how to handle properties and procedures.
if returnType = nil then
Exit;
case returnType.TypeKind of
tkClass,
tkRecord,
tkInterface:
begin
mock := FAutoMocker.Mock(returnType.Handle);
result := TValue.From<IProxy>(mock);
//Add a behaviour to return the value next time.
behavior := TBehavior.CreateWillReturnWhen(Args, Result, TArray<IMatcher>.Create());
FBehaviors.Add(behavior);
end
else
Result := FReturnDefault;
end;
Exit;
end;
//If we have no return type defined, and the default return type is empty
if (returnType <> nil) and (FReturnDefault.IsEmpty) then
//Say we didn't have a default return value
raise EMockException.Create(Format('[%s] has no default return value or return type was defined for method [%s]', [FTypeName, FMethodName]));
//If we have either a return type, or a default return value then check whether behaviour must be defined.
if FSetupParameters.BehaviorMustBeDefined and (AExpectationHitCtr = 0) and (FReturnDefault.IsEmpty) then
//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]));
Result := FReturnDefault;
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>; const matchers : TArray<IMatcher>);
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) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation At Least for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
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) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation At Least Once for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateAtLeastOnce(FMethodName);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtLeastOnceWhen(const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.AtLeastOnceWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation At Least Once When for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateAtLeastOnceWhen(FMethodName, Args, matchers);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtLeastWhen(const times: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.AtLeastWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation At Least When for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateAtLeastWhen(FMethodName, times, Args, matchers);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtMost(const times: Cardinal);
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.AtMost,TExpectationType.AtMostWhen]);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation At Most for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateAtMost(FMethodName, times);
FExpectations.Add(expectation);
end;
procedure TMethodData.AtMostWhen(const times: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.AtMostWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation At Most When for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateAtMostWhen(FMethodName, times, Args, matchers);
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>; const matchers : TArray<IMatcher>);
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) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Between for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateBetween(FMethodName,a,b);
FExpectations.Add(expectation);
end;
procedure TMethodData.BetweenWhen(const a, b: Cardinal;const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.BetweenWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Between When for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateBetweenWhen(FMethodName, a, b, Args, matchers);
FExpectations.Add(expectation);
end;
procedure TMethodData.Never;
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.Never ,TExpectationType.NeverWhen]);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Never for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateNever(FMethodName);
FExpectations.Add(expectation);
end;
procedure TMethodData.NeverWhen(const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.NeverWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Never When for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateNeverWhen(FMethodName, Args, matchers);
FExpectations.Add(expectation);
end;
procedure TMethodData.Once;
var
expectation : IExpectation;
begin
expectation := FindExpectation([TExpectationType.Once,TExpectationType.OnceWhen]);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Once for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateOnce(FMethodName);
FExpectations.Add(expectation);
end;
procedure TMethodData.OnceWhen(const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
expectation : IExpectation;
begin
expectation := FindExpectation(TExpectationType.OnceWhen,Args);
if (expectation <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockException.Create(Format('[%s] already defines Expectation Once When for method [%s]', [FTypeName, FMethodName]))
else if expectation <> nil then
FExpectations.Remove(expectation);
expectation := TExpectation.CreateOnceWhen(FMethodName, Args, matchers);
FExpectations.Add(expectation);
end;
procedure TMethodData.RecordHit(const Args: TArray<TValue>; const returnType : TRttiType; out Result: TValue);
var
behavior : IBehavior;
expectation : IExpectation;
expectationHitCtr: integer;
returnValue : TValue;
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
returnValue := behavior.Execute(Args, returnType)
else
begin
if FSetupParameters.IsStub then
StubNoBehaviourRecordHit(Args, expectationHitCtr, returnType, returnValue)
else
MockNoBehaviourRecordHit(Args, expectationHitCtr, returnType, returnValue);
end;
if returnType <> nil then
Result := returnValue;
end;
procedure TMethodData.StubNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType: TRttiType; out Result: TValue);
begin
//If we have no return type defined, and the default return type is empty
if (returnType <> nil) and (FReturnDefault.IsEmpty) then
begin
//Return the default value for the passed in return type
Result := GetDefaultValue(returnType);
end
else if FSetupParameters.BehaviorMustBeDefined and (AExpectationHitCtr = 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;
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) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockSetupException.Create(Format('[%s] already defines WillExecute for method [%s]', [FTypeName, FMethodName]))
else if behavior <> nil then
FBehaviors.Remove(behavior);
behavior := TBehavior.CreateWillExecute(func);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillExecuteWhen(const func: TExecuteFunc;const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillExecuteWhen,Args);
if (behavior <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockSetupException.Create(Format('[%s] already defines WillExecute When for method [%s]', [FTypeName, FMethodName]))
else if behavior <> nil then
FBehaviors.Remove(behavior);
behavior := TBehavior.CreateWillExecuteWhen(Args, func, matchers);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillRaiseAlways(const exceptionClass: ExceptClass; const message : string);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillRaiseAlways);
if (behavior <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockSetupException.Create(Format('[%s] already defines Will Raise Always for method [%s]', [FTypeName, FMethodName]))
else if behavior <> nil then
FBehaviors.Remove(behavior);
behavior := TBehavior.CreateWillRaise(exceptionClass, message);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillRaiseWhen(const exceptionClass: ExceptClass; const message : string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillRaise,Args);
if (behavior <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockSetupException.Create(Format('[%s] already defines Will Raise When for method [%s]', [FTypeName, FMethodName]))
else if behavior <> nil then
FBehaviors.Remove(behavior);
behavior := TBehavior.CreateWillRaiseWhen(Args,exceptionClass, message, matchers);
FBehaviors.Add(behavior);
end;
procedure TMethodData.WillReturnDefault(const returnValue: TValue);
begin
if (not FReturnDefault.IsEmpty) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) 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; const matchers : TArray<IMatcher>);
var
behavior : IBehavior;
begin
behavior := FindBehavior(TBehaviorType.WillReturn,Args);
if (behavior <> nil) AND (not FSetupParameters.AllowRedefineBehaviorDefinitions) then
raise EMockSetupException.Create(Format('[%s] already defines Will Return When for method [%s]', [FTypeName, FMethodName]))
else if behavior <> nil then
FBehaviors.Remove(behavior);
behavior := TBehavior.CreateWillReturnWhen(Args, returnValue, matchers);
FBehaviors.Add(behavior);
end;
{ TSetupMethodDataParameters }
class function TSetupMethodDataParameters.Create(const AIsStub: boolean; const ABehaviorMustBeDefined, AAllowRedefineBehaviorDefinitions: boolean): TSetupMethodDataParameters;
begin
result.IsStub := AIsStub;
result.BehaviorMustBeDefined := ABehaviorMustBeDefined;
result.AllowRedefineBehaviorDefinitions := AAllowRedefineBehaviorDefinitions;
end;
end.