Skip to content

Fix equality checking of Comparable Mocks#1323

Merged
leonard84 merged 4 commits intospockframework:masterfrom
jdpopkin:fix_comparable
Sep 29, 2022
Merged

Fix equality checking of Comparable Mocks#1323
leonard84 merged 4 commits intospockframework:masterfrom
jdpopkin:fix_comparable

Conversation

@jdpopkin
Copy link
Copy Markdown
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
Copy Markdown

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