Skip to content

Commit 7b11780

Browse files
sglienkevincentparrett
authored andcommitted
implemented support for more types in parameter matching
1 parent 708ee15 commit 7b11780

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

Source/Base/Spring.pas

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5974,6 +5974,13 @@ function EqualsClass2Class(const left, right: TValue): Boolean;
59745974
Result := left.AsObject = right.AsObject;
59755975
end;
59765976

5977+
function EqualsMeth2Meth(const left, right: TValue): Boolean;
5978+
begin
5979+
Result := (left.TypeInfo = right.TypeInfo)
5980+
and (TValueData(left).FAsMethod.Code = TValueData(right).FAsMethod.Code)
5981+
and (TValueData(left).FAsMethod.Data = TValueData(right).FAsMethod.Data);
5982+
end;
5983+
59775984
function EqualsPointer2Pointer(const left, right: TValue): Boolean;
59785985
begin
59795986
Result := left.AsPointer = right.AsPointer;
@@ -6190,7 +6197,7 @@ function EqualsSet2Set(const left, right: TValue): Boolean;
61906197
// tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
61916198
EqualsFail, EqualsFail, EqualsFail, EqualsFail, EqualsFail,
61926199
// tkString, tkSet, tkClass, tkMethod, tkWChar,
6193-
EqualsFail, EqualsFail, EqualsFail, EqualsFail, EqualsFail, // TODO: tkMethod
6200+
EqualsFail, EqualsFail, EqualsFail, EqualsMeth2Meth, EqualsFail, // TODO: tkMethod
61946201
// tkLString, tkWString, tkVariant, tkArray, tkRecord,
61956202
EqualsFail, EqualsFail, EqualsFail, EqualsFail, EqualsFail,
61966203
// tkInterface, tkInt64, tkDynArray, tkUString, tkClassRef

Source/Core/Mocking/Spring.Mocking.Matching.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ class function TMatcherFactory.GetIndex(const v: TValue): Integer;
383383
const
384384
Handlers: array[TTypeKind] of function(const v: TValue): Integer = (
385385
GetIndexFail, GetIndexOrdinal, GetIndexOrdinal, GetIndexOrdinal, GetIndexFloat,
386-
GetIndexFail, GetIndexOrdinal, GetIndexObject, GetIndexFail, GetIndexString,
386+
GetIndexFail, GetIndexOrdinal, GetIndexObject, GetIndexPointer, GetIndexString,
387387
GetIndexString, GetIndexString, GetIndexVariant, GetIndexArray, GetIndexRecord,
388-
GetIndexInterface, GetIndexOrdinal, GetIndexArray, GetIndexString, GetIndexFail,
389-
GetIndexObject, GetIndexFail {$IF Declared(tkMRecord)}, GetIndexFail{$IFEND});
388+
GetIndexInterface, GetIndexOrdinal, GetIndexArray, GetIndexString, GetIndexPointer,
389+
GetIndexObject, GetIndexPointer {$IF Declared(tkMRecord)}, GetIndexRecord{$IFEND});
390390
begin
391391
Result := Handlers[TValueData(v).FTypeInfo.Kind](v) - 1;
392392
end;
@@ -395,10 +395,10 @@ class procedure TMatcherFactory.SetIndex(typeInfo: PTypeInfo; index: Integer; va
395395
const
396396
Handlers: array[TTypeKind] of procedure (typeInfo: PTypeInfo; index: Integer; var Result) = (
397397
SetIndexFail, SetIndexOrdinal, SetIndexOrdinal, SetIndexOrdinal, SetIndexFloat,
398-
SetIndexFail, SetIndexOrdinal, SetIndexObject, SetIndexFail, SetIndexString,
398+
SetIndexFail, SetIndexOrdinal, SetIndexObject, SetIndexPointer, SetIndexString,
399399
SetIndexString, SetIndexString, SetIndexVariant, SetIndexArray, SetIndexRecord,
400-
SetIndexInterface, SetIndexOrdinal, SetIndexArray, SetIndexString, SetIndexFail,
401-
SetIndexObject, SetIndexFail {$IF Declared(tkMRecord)}, SetIndexFail{$IFEND});
400+
SetIndexInterface, SetIndexOrdinal, SetIndexArray, SetIndexString, SetIndexPointer,
401+
SetIndexObject, SetIndexPointer {$IF Declared(tkMRecord)}, SetIndexRecord{$IFEND});
402402
begin
403403
Handlers[typeInfo.Kind](typeInfo, index + 1, Result);
404404
end;

Tests/Source/Core/Spring.Tests.Mocking.pas

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ interface
3434

3535
type
3636
TParameterMatchingTests = class(TTestCase)
37+
private
38+
procedure Notify(Sender: TObject);
3739
published
3840
procedure ArgsEvaluationOrder;
3941
procedure ArgsStackProperlyCleaned;
@@ -48,6 +50,11 @@ TParameterMatchingTests = class(TTestCase)
4850
procedure TestRegex;
4951
procedure TestEnum;
5052
procedure TestSet;
53+
procedure TestClass;
54+
procedure TestMethod;
55+
{$IFDEF DELPHIX_SYDNEY_UP}
56+
procedure TestMRecord;
57+
{$ENDIF}
5158

5259
procedure ReturnsMultipleValues;
5360
procedure WrapperObjectsNotLeaking;
@@ -88,6 +95,7 @@ MockSequenceTest = class(TTestCase)
8895
implementation
8996

9097
uses
98+
Classes,
9199
SysUtils,
92100
Spring,
93101
Spring.Mocking;
@@ -96,6 +104,16 @@ implementation
96104
TTestEnum = (One, Two, Three);
97105
ShortEnum = 0..31;
98106
TTestSet = set of {$IFNDEF DELPHIX_RIO_UP}ShortEnum{$ELSE}Byte{$ENDIF}; // see RSP-16153
107+
{$IFDEF DELPHIX_SYDNEY_UP}
108+
TTestMRec = record
109+
value: Integer;
110+
class operator Initialize(out value: TTestMRec);
111+
end;
112+
{$M+}
113+
IMockTestMRec = interface
114+
procedure Test(const rec: TTestMRec);
115+
end;
116+
{$ENDIF}
99117

100118
{$M+}
101119
IMockTest = interface
@@ -109,6 +127,8 @@ implementation
109127
procedure TestEnum(const value: TTestEnum);
110128
procedure TestSet(const n: Integer; const value: TTestSet; const i: Integer = 0);
111129
procedure TestObject(const obj: TObject);
130+
procedure TestClass(const cls: TClass);
131+
procedure TestMethod(const event: TNotifyEvent);
112132
function GetNext: Integer;
113133

114134
function GetEvent: IInvokableNotifyEvent<Integer>;
@@ -150,6 +170,17 @@ TFoo = class
150170
end;
151171

152172

173+
{$REGION 'TTestMRec'}
174+
175+
{$IFDEF DELPHIX_SYDNEY_UP}
176+
class operator TTestMRec.Initialize(out value: TTestMRec);
177+
begin
178+
end;
179+
{$ENDIF}
180+
181+
{$ENDREGION}
182+
183+
153184
{$REGION 'TParameterMatchingTests'}
154185

155186
procedure TParameterMatchingTests.ArgsEvaluationOrder;
@@ -237,6 +268,10 @@ procedure TParameterMatchingTests.ArgsStackProperlyCleaned;
237268
Pass;
238269
end;
239270

271+
procedure TParameterMatchingTests.Notify(Sender: TObject);
272+
begin
273+
end;
274+
240275
procedure TParameterMatchingTests.OutParameterCanBePassed;
241276
{$IFDEF DELPHIXE8_UP}
242277
var
@@ -306,6 +341,19 @@ procedure TParameterMatchingTests.ReturnsMultipleValues;
306341
mock.Instance.GetNumber;
307342
end;
308343

344+
procedure TParameterMatchingTests.TestClass;
345+
var
346+
mock: Mock<IMockTest>;
347+
begin
348+
mock.Setup.Executes.When.TestClass(TObject);
349+
mock.Instance.TestClass(TObject);
350+
mock.Received.TestClass(TObject);
351+
mock.Setup.Executes.When.TestClass(Arg.IsAny<TClass>);
352+
mock.Instance.TestClass(nil);
353+
mock.Received.TestClass(nil);
354+
Pass;
355+
end;
356+
309357
procedure TParameterMatchingTests.TestDynArray;
310358
var
311359
mock: Mock<IMockTest>;
@@ -332,6 +380,35 @@ procedure TParameterMatchingTests.TestEnum;
332380
Pass;
333381
end;
334382

383+
procedure TParameterMatchingTests.TestMethod;
384+
var
385+
mock: Mock<IMockTest>;
386+
begin
387+
mock.Setup.Executes.When.TestMethod(Notify);
388+
mock.Instance.TestMethod(Notify);
389+
mock.Received.TestMethod(Notify);
390+
mock.Setup.Executes.When.TestMethod(Arg.IsAny<TNotifyEvent>());
391+
mock.Instance.TestMethod(nil);
392+
mock.Received.TestMethod(nil);
393+
Pass;
394+
end;
395+
396+
{$IFDEF DELPHIX_SYDNEY_UP}
397+
procedure TParameterMatchingTests.TestMRecord;
398+
var
399+
mock: Mock<IMockTestMRec>;
400+
rec: TTestMRec;
401+
begin
402+
mock.Setup.Executes.When.Test(rec);
403+
mock.Instance.Test(rec);
404+
mock.Received.Test(rec);
405+
mock.Setup.Executes.When.Test(Arg.IsAny<TTestMRec>);
406+
mock.Instance.Test(rec);
407+
mock.Received(2).Test(Arg.IsAny<TTestMRec>);
408+
Pass;
409+
end;
410+
{$ENDIF}
411+
335412
procedure TParameterMatchingTests.TestRecord;
336413
var
337414
mock: Mock<TFoo>;

0 commit comments

Comments
 (0)