Skip to content

Commit 3cea649

Browse files
Argument matching: support arrays (#136)
* 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
1 parent 41ab9af commit 3cea649

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

Source/Delphi.Mocks.Helpers.pas

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ TValueHelper = record helper for TValue
6565
function IsGuid: Boolean;
6666
function IsInterface : Boolean;
6767
function IsRecord : Boolean;
68+
function IsArray : Boolean;
6869
function AsDouble: Double;
6970
function AsFloat: Extended;
7071
function AsSingle: Single;
@@ -83,6 +84,7 @@ TRttiMethodHelper = class helper for TRttiMethod
8384
function IsVirtual: Boolean;
8485
end;
8586

87+
function CompareValue_Array(const Left, Right: TValue): Integer;
8688
function CompareValue_Record(const Left, Right: TValue): Integer;
8789
function CompareValue(const Left, Right: TValue): Integer;
8890
function SameValue(const Left, Right: TValue): Boolean;
@@ -124,6 +126,8 @@ function CompareValue(const Left, Right: TValue): Integer;
124126
Result := NativeInt(left.AsInterface) - NativeInt(right.AsInterface) // TODO: instance comparer
125127
else if Left.IsRecord and Right.IsRecord then
126128
Result := CompareValue_Record(Left, Right)
129+
else if Left.IsArray and Right.IsArray then
130+
Result := CompareValue_Array(Left, Right)
127131
else if left.IsVariant and right.IsVariant then
128132
begin
129133
case VarCompareValue(left.AsVariant, right.AsVariant) of
@@ -138,6 +142,22 @@ function CompareValue(const Left, Right: TValue): Integer;
138142
Result := 0;
139143
end;
140144

145+
function CompareValue_Array(const Left, Right: TValue): Integer;
146+
var
147+
LMethod: TRttiMethod;
148+
i: Integer;
149+
begin
150+
Result := Left.GetArrayLength - Right.GetArrayLength;
151+
152+
if Result = 0 then begin
153+
for i := 0 to Left.GetArrayLength - 1 do begin
154+
Result := CompareValue(Left.GetArrayElement(i), Right.GetArrayElement(i));
155+
if Result <> 0 then
156+
Exit;
157+
end;
158+
end;
159+
end;
160+
141161
function CompareValue_Record(const Left, Right: TValue): Integer;
142162
var
143163
LMethod: TRttiMethod;
@@ -192,6 +212,11 @@ function TValueHelper.GetRttiType: TRttiType;
192212

193213
end;
194214

215+
function TValueHelper.IsArray: Boolean;
216+
begin
217+
Result := Kind in [tkArray, tkDynArray];
218+
end;
219+
195220
function TValueHelper.IsBoolean: Boolean;
196221
begin
197222
Result := TypeInfo = System.TypeInfo(Boolean);

Tests/Delphi.Mocks.Tests.TValue.pas

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface
1010
TValueTests = class
1111
published
1212
procedure Test_IsRecord;
13+
procedure Test_IsArray;
1314
end;
1415
{$M-}
1516

@@ -25,6 +26,12 @@ TMyRec = record
2526
Value: String;
2627
end;
2728

29+
procedure TValueTests.Test_IsArray;
30+
begin
31+
Assert.IsFalse(TValue.From<string>('test').IsArray);
32+
Assert.IsTrue(TValue.From<TArray<string>>(['a', 'b']).IsArray);
33+
end;
34+
2835
procedure TValueTests.Test_IsRecord;
2936
var
3037
r: TMyRec;

Tests/Delphi.Mocks.Tests.Utils.pas

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ TTestTValue = class
2727
procedure Test_CompareValue_RecordEquals;
2828
procedure Test_CompareValue_RecordNotEquals;
2929
procedure Test_CompareValue_RecordNoEqualsOperator;
30+
31+
procedure Test_CompareValue_ArrayEquals;
32+
procedure Test_CompareValue_ArrayNotEquals;
3033
end;
3134
{$M-}
3235

@@ -91,6 +94,40 @@ procedure TTestTValue.Test_TValue_Equals_SameGuid_Instance;
9194
Assert.IsTrue(v1.Equals(v2));
9295
end;
9396

97+
procedure TTestTValue.Test_CompareValue_ArrayEquals;
98+
var
99+
a1, a2: TArray<string>;
100+
begin
101+
a1 := [];
102+
a2 := [];
103+
Assert.AreEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
104+
105+
a1 := ['a', 'b'];
106+
a2 := ['a', 'b'];
107+
Assert.AreEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
108+
end;
109+
110+
procedure TTestTValue.Test_CompareValue_ArrayNotEquals;
111+
var
112+
a1, a2: TArray<string>;
113+
begin
114+
a1 := ['a'];
115+
a2 := ['a', 'b'];
116+
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
117+
118+
a1 := ['a', 'b'];
119+
a2 := ['a'];
120+
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
121+
122+
a1 := [];
123+
a2 := ['a'];
124+
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
125+
126+
a1 := ['a'];
127+
a2 := [];
128+
Assert.AreNotEqual(0, CompareValue(TValue.From(a1), TValue.From(a2)));
129+
end;
130+
94131
procedure TTestTValue.Test_CompareValue_RecordEquals;
95132
var
96133
r1, r2: TMyRec;

0 commit comments

Comments
 (0)