diff --git a/Source/Delphi.Mocks.Helpers.pas b/Source/Delphi.Mocks.Helpers.pas index df9bb6e..6310e91 100644 --- a/Source/Delphi.Mocks.Helpers.pas +++ b/Source/Delphi.Mocks.Helpers.pas @@ -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; @@ -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; @@ -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 @@ -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; @@ -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); diff --git a/Tests/Delphi.Mocks.Tests.TValue.pas b/Tests/Delphi.Mocks.Tests.TValue.pas index fb916a1..84b9f4e 100644 --- a/Tests/Delphi.Mocks.Tests.TValue.pas +++ b/Tests/Delphi.Mocks.Tests.TValue.pas @@ -10,6 +10,7 @@ interface TValueTests = class published procedure Test_IsRecord; + procedure Test_IsArray; end; {$M-} @@ -25,6 +26,12 @@ TMyRec = record Value: String; end; +procedure TValueTests.Test_IsArray; +begin + Assert.IsFalse(TValue.From('test').IsArray); + Assert.IsTrue(TValue.From>(['a', 'b']).IsArray); +end; + procedure TValueTests.Test_IsRecord; var r: TMyRec; diff --git a/Tests/Delphi.Mocks.Tests.Utils.pas b/Tests/Delphi.Mocks.Tests.Utils.pas index 17235b3..9db7cde 100644 --- a/Tests/Delphi.Mocks.Tests.Utils.pas +++ b/Tests/Delphi.Mocks.Tests.Utils.pas @@ -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-} @@ -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; +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; +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;