moosetechnology / Famix

An abstract representation of source code. Famix is generic and can describe applications in multiple programming languages.
MIT License
13 stars 23 forks source link

Generate Equality Check for entities #799

Closed badetitou closed 3 months ago

badetitou commented 3 months ago

This PR aims to add in the generator the code to generate equality checks (method = and hash). We keep the current default behavior -> no breaking change possible :)

I propose adding a method named: withEqualityCheckOn: that takes the list of properties as input to check for equality. It then generates the = and hash methods considering this list.

I have modified FamixTest4Generator to take into consideration this update. For instance, I added to person a firstName and lastName. And I added the quality check on them

    person property: #firstName type: #String.
    person property: #lastName type: #String.
    person withEqualityCheckOn: { #firstName. #lastName }.

The same for book with an id

I update the setup to add identity information. And I added the test testEqualityStudent to particularly test this feature

For method =

The generation follows this pattern:

= aFamixTest4Person 

    <generated>
    ^ aFamixTest4Person firstName = self firstName and: [ aFamixTest4Person lastName = self lastName] 

And for hash

hash

    <generated>
    ^ self firstName hash bitXor: self lastName hash 

I based this template on existing methods in the system