Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMock<IMyInterface>.MyMethod(X) leads to access violation when X is a record type #125

Open
alydouglas opened this issue Apr 9, 2020 · 3 comments

Comments

@alydouglas
Copy link

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.

@vincentparrett
Copy link
Member

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.

@alydouglas
Copy link
Author

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
@Laurensvanrun
Copy link
Contributor

This issue should be closed, because it works nowadays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants