Skip to content

Commit

Permalink
Argument matching: support arrays (#136)
Browse files Browse the repository at this point in the history
* Support for records in ItRec
Support for custom EqualityComparer in ItRec

* Fixed warnings and unnecessary code

* Support record types for argument matching (without matchers)

* Call original virtual method if the parameters does not match

* Argument matching: support arrays
  • Loading branch information
Laurensvanrun authored Sep 28, 2021
1 parent 41ab9af commit 3cea649
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Source/Delphi.Mocks.Helpers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ TValueHelper = record helper for TValue
function IsGuid: Boolean;
function IsInterface : Boolean;
function IsRecord : Boolean;
function IsArray : Boolean;
function AsDouble: Double;
function AsFloat: Extended;
function AsSingle: Single;
Expand All @@ -83,6 +84,7 @@ TRttiMethodHelper = class helper for TRttiMethod
function IsVirtual: Boolean;
end;

function CompareValue_Array(const Left, Right: TValue): Integer;
function CompareValue_Record(const Left, Right: TValue): Integer;
function CompareValue(const Left, Right: TValue): Integer;
function SameValue(const Left, Right: TValue): Boolean;
Expand Down Expand Up @@ -124,6 +126,8 @@ function CompareValue(const Left, Right: TValue): Integer;
Result := NativeInt(left.AsInterface) - NativeInt(right.AsInterface) // TODO: instance comparer
else if Left.IsRecord and Right.IsRecord then
Result := CompareValue_Record(Left, Right)
else if Left.IsArray and Right.IsArray then
Result := CompareValue_Array(Left, Right)
else if left.IsVariant and right.IsVariant then
begin
case VarCompareValue(left.AsVariant, right.AsVariant) of
Expand All @@ -138,6 +142,22 @@ function CompareValue(const Left, Right: TValue): Integer;
Result := 0;
end;

function CompareValue_Array(const Left, Right: TValue): Integer;
var
LMethod: TRttiMethod;
i: Integer;
begin
Result := Left.GetArrayLength - Right.GetArrayLength;

if Result = 0 then begin
for i := 0 to Left.GetArrayLength - 1 do begin
Result := CompareValue(Left.GetArrayElement(i), Right.GetArrayElement(i));
if Result <> 0 then
Exit;
end;
end;
end;

function CompareValue_Record(const Left, Right: TValue): Integer;
var
LMethod: TRttiMethod;
Expand Down Expand Up @@ -192,6 +212,11 @@ function TValueHelper.GetRttiType: TRttiType;

end;

function TValueHelper.IsArray: Boolean;
begin
Result := Kind in [tkArray, tkDynArray];
end;

function TValueHelper.IsBoolean: Boolean;
begin
Result := TypeInfo = System.TypeInfo(Boolean);
Expand Down
7 changes: 7 additions & 0 deletions Tests/Delphi.Mocks.Tests.TValue.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface
TValueTests = class
published
procedure Test_IsRecord;
procedure Test_IsArray;
end;
{$M-}

Expand All @@ -25,6 +26,12 @@ TMyRec = record
Value: String;
end;

procedure TValueTests.Test_IsArray;
begin
Assert.IsFalse(TValue.From<string>('test').IsArray);
Assert.IsTrue(TValue.From<TArray<string>>(['a', 'b']).IsArray);
end;

procedure TValueTests.Test_IsRecord;
var
r: TMyRec;
Expand Down
37 changes: 37 additions & 0 deletions Tests/Delphi.Mocks.Tests.Utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ TTestTValue = class
procedure Test_CompareValue_RecordEquals;
procedure Test_CompareValue_RecordNotEquals;
procedure Test_CompareValue_RecordNoEqualsOperator;

procedure Test_CompareValue_ArrayEquals;
procedure Test_CompareValue_ArrayNotEquals;
end;
{$M-}

Expand Down Expand Up @@ -91,6 +94,40 @@ procedure TTestTValue.Test_TValue_Equals_SameGuid_Instance;
Assert.IsTrue(v1.Equals(v2));
end;

procedure TTestTValue.Test_CompareValue_ArrayEquals;
var
a1, a2: TArray<string>;
begin
a1 := [];
a2 := [];
Assert.AreEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));

a1 := ['a', 'b'];
a2 := ['a', 'b'];
Assert.AreEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
end;

procedure TTestTValue.Test_CompareValue_ArrayNotEquals;
var
a1, a2: TArray<string>;
begin
a1 := ['a'];
a2 := ['a', 'b'];
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));

a1 := ['a', 'b'];
a2 := ['a'];
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));

a1 := [];
a2 := ['a'];
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));

a1 := ['a'];
a2 := [];
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
end;

procedure TTestTValue.Test_CompareValue_RecordEquals;
var
r1, r2: TMyRec;
Expand Down

0 comments on commit 3cea649

Please sign in to comment.