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

Fix equality checking of Comparable Mocks #1323

Merged
merged 4 commits into from
Sep 29, 2022

Conversation

jdpopkin
Copy link
Contributor

EqualArgumentConstraint uses DefaultTypeTransformation.compareEqual
to check arguments' equality in interactions. For most objects, this is
equivalent to calling .equals, so Java Mocks come with an intuitive
default implemention of .equals. But for objects that are Comparable
(including Mocks of types that implement Comparable),
DefaultTypeTransformation.compareEqual(x, y) is equivalent to calling
x.compareTo(y) == 0. As a result, all Mocks of Comparable types are
treated as equal by EqualArgumentConstraint.

This commit fixes that by adding a default implementation of
.compareTo to Comparable Mocks.

`EqualArgumentConstraint` uses `DefaultTypeTransformation.compareEqual`
to check arguments' equality in interactions. For most objects, this is
equivalent to calling `.equals`, so Java Mocks come with an intuitive
default implemention of `.equals`. But for objects that are `Comparable`
(including Mocks of types that implement `Comparable`),
`DefaultTypeTransformation.compareEqual(x, y)` is equivalent to calling
`x.compareTo(y) == 0`. As a result, all Mocks of Comparable types are
treated as equal by `EqualArgumentConstraint`.

This commit fixes that by adding a default implementation of
`.compareTo` to `Comparable` Mocks.
@codecov
Copy link

codecov bot commented May 11, 2021

Codecov Report

Base: 79.84% // Head: 79.83% // Decreases project coverage by -0.01% ⚠️

Coverage data is based on head (41f03d7) compared to base (8795325).
Patch coverage: 50.00% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1323      +/-   ##
============================================
- Coverage     79.84%   79.83%   -0.02%     
- Complexity     4059     4065       +6     
============================================
  Files           414      415       +1     
  Lines         12848    12856       +8     
  Branches       1662     1665       +3     
============================================
+ Hits          10259    10263       +4     
- Misses         1988     1989       +1     
- Partials        601      604       +3     
Impacted Files Coverage Δ
...mework/mock/DefaultJavaLangObjectInteractions.java 63.63% <ø> (ø)
...ockframework/mock/DefaultCompareToInteraction.java 50.00% <50.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

leonard84
leonard84 previously approved these changes Sep 29, 2022
@leonard84 leonard84 changed the title Fix equality checking of Comparable Mocks (#1322) Fix equality checking of Comparable Mocks Sep 29, 2022
@leonard84 leonard84 merged commit 1ef0afb into spockframework:master Sep 29, 2022
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

Successfully merging this pull request may close these issues.

Interaction argument-matching treats all Mocks/Stubs of Comparables as equivalent
2 participants