Skip to content

fix: closeTo matches any Number (Float properties with hasProperty) - #446

Open
arimu1 wants to merge 1 commit into
hamcrest:masterfrom
arimu1:fix/444-closeto-number-float
Open

arimu1 wants to merge 1 commit into
hamcrest:masterfrom
arimu1:fix/444-closeto-number-float

Conversation

@arimu1

@arimu1 arimu1 commented Aug 8, 2026

Copy link
Copy Markdown

Summary

IsCloseTo / Matchers.closeTo only accepted Double. TypeSafeMatcher then rejected other boxed numbers (e.g. Float from a bean getter) with:

property 'number' was a java.lang.Float (<1.0F>)

even when the numeric value was within the allowed delta. That breaks common chaining such as hasProperty("number", closeTo(...)) and hasProperty("number", is(closeTo(...))).

This changes the matcher to TypeSafeMatcher<Number> and compares via Number#doubleValue(), so Float/Integer/Long/etc. work the same as Double.

Fixes #444

Related prior work

Open PR #184 (2017, still CONFLICTING on the pre-module-merge layout) proposed the same Number widening and was never rebased after maintainers asked in 2020/2022. This is a fresh implementation on current master for the concrete #444 Float + hasProperty failure.

Test plan

  • ./gradlew :hamcrest:test --tests org.hamcrest.number.IsCloseToTest
  • ./gradlew :hamcrest:test (full module)
  • New coverage: Float/Integer/Long values; closeTo is confused by floats when chained #444 regression with hasProperty + is(closeTo(...)) (mismatch reports differed by, not type rejection)
TypeSafeMatcher<Double> rejected boxed Float/Integer values before
comparing, so hasProperty("x", closeTo(...)) failed for float bean
properties even when the numeric value was within delta.

Fixes hamcrest#444

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant