You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have started using Delphi-Mocks to help unit test a development that makes heavy use of records as input parameters.
I created a TMock of one of the interfaces and set it up using the Setup.WillReturn(Y).When.MyMethod(X) syntax where X in this case is a record type. However, when MyMethod is then called with X on the mock, it leads to an access violation in the Delphi.Mocks.Helpers.TValueHelper.
On investigation, it is caused when the logic in SameValue triggers AsPointer which in turn triggers Systtem.Rtti.TValue.ExtractRawDataNoCopy and then System.Rti.TValueDataImpl.ExtractRawDataNoCopy. This leads to an access violation as more than the size of a pointer is moved when the underlying type is a record.
As a modification to the TValueHelper I added a step to check whether the type is of kind tkRecord but this only helps identify that it is a record, so in addition, also added a mechanism for custom comparers to be registered by the user for specific record types which are then used to check if the two records are equal.
This is a similar mechanism to Generics.Collections where an IComparer can be defined for lists using record types but not ideal, however, seems to be the safest methodology to ensure a good comparison is made.
The text was updated successfully, but these errors were encountered:
Please do submit these changes as a pull request to this repository. I can see the change on your fork, but it looks like you did s pull request to the fork rather than this repo? Also, be mindful of formatting etc. as your commits show every line changed due to formatting.
I will do. I've only used GitHub in the past to pull down repos or work on single branch projects. I was just experimenting with the pull request stuff yesterday when I added my changes and then realised afterwards it had committed it as all being changed due to formatting (reminder to review the diffs before committing!)
Will get it sorted over the weekend do a pull request.
alydouglas
pushed a commit
to alydouglas/Delphi-Mocks
that referenced
this issue
Apr 10, 2020
…mpareValue for tkRecord
Added ability to register custom comparison routines which cann be used for all types but primarily designed to be used with records
I have started using Delphi-Mocks to help unit test a development that makes heavy use of records as input parameters.
I created a TMock of one of the interfaces and set it up using the Setup.WillReturn(Y).When.MyMethod(X) syntax where X in this case is a record type. However, when MyMethod is then called with X on the mock, it leads to an access violation in the Delphi.Mocks.Helpers.TValueHelper.
On investigation, it is caused when the logic in SameValue triggers AsPointer which in turn triggers Systtem.Rtti.TValue.ExtractRawDataNoCopy and then System.Rti.TValueDataImpl.ExtractRawDataNoCopy. This leads to an access violation as more than the size of a pointer is moved when the underlying type is a record.
As a modification to the TValueHelper I added a step to check whether the type is of kind tkRecord but this only helps identify that it is a record, so in addition, also added a mechanism for custom comparers to be registered by the user for specific record types which are then used to check if the two records are equal.
This is a similar mechanism to Generics.Collections where an IComparer can be defined for lists using record types but not ideal, however, seems to be the safest methodology to ensure a good comparison is made.
The text was updated successfully, but these errors were encountered: