Skip to content

Commit f086a92

Browse files
pbaczjoel-costigliola
authored andcommitted
Fix: AbstractOptionalDoubleAssert.hasValue(double) fails with NaN
Fix #3401
1 parent 3cf3900 commit f086a92

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

assertj-core/src/main/java/org/assertj/core/api/AbstractOptionalDoubleAssert.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,30 @@ public SELF isNotEmpty() {
115115
}
116116

117117
/**
118-
* Verifies that the actual {@link java.util.OptionalDouble} has the value in argument.
118+
* Verifies that the actual {@link java.util.OptionalDouble} has the value in argument. The check is consistent
119+
* with {@link java.util.OptionalDouble#equals(Object)} since 3.26.0.
119120
* <p>
120-
* Assertion will pass :
121-
* <pre><code class='java'> assertThat(OptionalDouble.of(8.0)).hasValue(8.0);
121+
* <pre><code class='java'> // assertions succeed:
122+
* assertThat(OptionalDouble.of(8.0)).hasValue(8.0);
122123
* assertThat(OptionalDouble.of(8.0)).hasValue(Double.valueOf(8.0));
123-
* assertThat(OptionalDouble.of(Double.NaN)).hasValue(Double.NaN); </code></pre>
124-
* <p>
125-
* Assertion will fail :
126-
* <pre><code class='java'> assertThat(OptionalDouble.empty()).hasValue(8.0);
124+
* assertThat(OptionalDouble.of(Double.NaN)).hasValue(Double.NaN);
125+
* assertThat(OptionalDouble.of(Double.POSITIVE_INFINITY)).hasValue(Double.POSITIVE_INFINITY);
126+
* assertThat(OptionalDouble.of(Double.NEGATIVE_INFINITY)).hasValue(Double.NEGATIVE_INFINITY);
127+
*
128+
* // assertions fail:
129+
* assertThat(OptionalDouble.empty()).hasValue(8.0);
127130
* assertThat(OptionalDouble.of(7)).hasValue(8.0);</code></pre>
128131
*
129132
* @param expectedValue the expected value inside the {@link java.util.OptionalDouble}.
130133
* @return this assertion object.
131134
* @throws java.lang.AssertionError if actual value is empty.
132135
* @throws java.lang.AssertionError if actual is null.
133-
* @throws java.lang.AssertionError if actual has not the value as expected.
136+
* @throws java.lang.AssertionError if actual does not have the expected value.
134137
*/
135138
public SELF hasValue(double expectedValue) {
136139
isNotNull();
137140
if (!actual.isPresent()) throwAssertionError(shouldContain(expectedValue));
138-
if (expectedValue != actual.getAsDouble())
141+
if (Double.compare(expectedValue, actual.getAsDouble()) != 0)
139142
throw Failures.instance().failure(info, shouldContain(actual, expectedValue), actual.getAsDouble(), expectedValue);
140143
return myself;
141144
}

assertj-core/src/test/java/org/assertj/core/api/optionaldouble/OptionalDoubleAssert_hasValue_Test.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,63 @@
1313
package org.assertj.core.api.optionaldouble;
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
16-
import static org.assertj.core.api.Assertions.catchThrowable;
1716
import static org.assertj.core.api.Assertions.catchThrowableOfType;
17+
import static org.assertj.core.api.BDDAssertions.then;
1818
import static org.assertj.core.error.OptionalShouldContain.shouldContain;
19-
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;
19+
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
2020
import static org.assertj.core.util.FailureMessages.actualIsNull;
2121

2222
import java.util.OptionalDouble;
2323

2424
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
2527
import org.opentest4j.AssertionFailedError;
2628

2729
class OptionalDoubleAssert_hasValue_Test {
2830

31+
@ParameterizedTest
32+
@ValueSource(doubles = { 10.0, -10.0, 0.0, -0.0, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY })
33+
void should_pass_if_optionalDouble_has_the_expected_value(double value) {
34+
assertThat(OptionalDouble.of(value)).hasValue(value);
35+
}
36+
37+
@Test
38+
void should_pass_if_optionalDouble_has_the_expected_value_as_Double() {
39+
assertThat(OptionalDouble.of(10.0)).hasValue(Double.valueOf(10.0));
40+
}
41+
2942
@Test
3043
void should_fail_when_optionalDouble_is_null() {
3144
// GIVEN
3245
OptionalDouble nullActual = null;
46+
// WHEN
47+
AssertionError assertionError = expectAssertionError(() -> assertThat(nullActual).hasValue(10.0));
3348
// THEN
34-
assertThatAssertionErrorIsThrownBy(() -> assertThat(nullActual).hasValue(10.0)).withMessage(actualIsNull());
35-
}
36-
37-
@Test
38-
void should_pass_if_optionalDouble_has_expected_value() {
39-
assertThat(OptionalDouble.of(10.0)).hasValue(10.0);
49+
then(assertionError).hasMessage(actualIsNull());
4050
}
4151

4252
@Test
43-
void should_fail_if_optionalDouble_does_not_have_expected_value() {
53+
void should_fail_if_optionalDouble_does_not_have_the_expected_value() {
4454
// GIVEN
4555
OptionalDouble actual = OptionalDouble.of(5.0);
4656
double expectedValue = 10.0;
4757
// WHEN
4858
AssertionFailedError error = catchThrowableOfType(() -> assertThat(actual).hasValue(expectedValue),
4959
AssertionFailedError.class);
5060
// THEN
51-
assertThat(error).hasMessage(shouldContain(actual, expectedValue).create());
52-
assertThat(error.getActual().getStringRepresentation()).isEqualTo(String.valueOf(actual.getAsDouble()));
53-
assertThat(error.getExpected().getStringRepresentation()).isEqualTo(String.valueOf(expectedValue));
61+
then(error).hasMessage(shouldContain(actual, expectedValue).create());
62+
then(error.getActual().getStringRepresentation()).isEqualTo(String.valueOf(actual.getAsDouble()));
63+
then(error.getExpected().getStringRepresentation()).isEqualTo(String.valueOf(expectedValue));
5464
}
5565

5666
@Test
5767
void should_fail_if_optionalDouble_is_empty() {
5868
// GIVEN
5969
double expectedValue = 10.0;
6070
// WHEN
61-
Throwable error = catchThrowable(() -> assertThat(OptionalDouble.empty()).hasValue(expectedValue));
71+
AssertionError error = expectAssertionError(() -> assertThat(OptionalDouble.empty()).hasValue(expectedValue));
6272
// THEN
63-
assertThat(error).hasMessage(shouldContain(expectedValue).create());
73+
then(error).hasMessage(shouldContain(expectedValue).create());
6474
}
6575
}

0 commit comments

Comments
 (0)