Skip to content

Commit

Permalink
Merge pull request #688 from rma-rripken/feature/float-close-to
Browse files Browse the repository at this point in the history
Adding a float matcher
  • Loading branch information
rma-rripken authored Jun 10, 2024
2 parents 7271dbd + e6c6308 commit 49a115b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.TreeMap;

import static cwms.cda.api.Controllers.*;
import static helpers.FloatCloseTo.floatCloseTo;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -329,8 +330,7 @@ void test_get_all_location_level() throws Exception {
assertThat(response.path("levels[0].level-units-id"),equalTo("ac-ft"));
assertThat(response.path("levels[0].level-date"),equalTo("2023-06-01T07:00:00Z"));
assertThat(response.path("levels[0].duration-id"),equalTo("1Day"));
actual0 = Float.valueOf((float) response.path("levels[0].constant-value")).doubleValue();
assertThat(actual0, closeTo(1.0, 0.01));
assertThat(response.path("levels[0].constant-value"), floatCloseTo(1.0, 0.01));

assertThat(response.path("levels[1].office-id"),equalTo(OFFICE));
assertThat(response.path("levels[1].location-level-id"),equalTo(levelId2));
Expand All @@ -340,8 +340,7 @@ void test_get_all_location_level() throws Exception {
assertThat(response.path("levels[1].level-units-id"),equalTo("ac-ft"));
assertThat(response.path("levels[1].level-date"),equalTo("2023-06-01T07:00:00Z"));
assertThat(response.path("levels[1].duration-id"),equalTo("1Day"));
actual1 = Float.valueOf((float) response.path("levels[1].constant-value")).doubleValue();
assertThat(actual1, closeTo(2.0, 0.01));
assertThat(response.path("levels[1].constant-value"), floatCloseTo(2.0, 0.01));
}

}
69 changes: 69 additions & 0 deletions cwms-data-api/src/test/java/helpers/FloatCloseTo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package helpers;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

/**
* A matcher that tests that an examined float is a number equal to a value within some range of
* acceptable error.
* RestAssured returns Floats from JSON paths and the hamcrest closeTo matcher only works with
* doubles. This matcher works just like the Hamcrest matcher but operates on float inputs.
* A slightly different name (floatCloseTo) is used to avoid confusion with the Hamcrest closeTo.
* <p/>
* For example:
* <pre>assertThat(1.03f, is(floatCloseTo(1.0, 0.03)))</pre>
*/
public class FloatCloseTo extends TypeSafeMatcher<Float> {
private final double delta;
private final double value;

public FloatCloseTo(double value, double error) {
this.delta = error;
this.value = value;
}

@Override
public boolean matchesSafely(Float item) {
return actualDelta(item) <= 0.0;
}

@Override
public void describeMismatchSafely(Float item, Description mismatchDescription) {
mismatchDescription.appendValue(item)
.appendText(" differed by ")
.appendValue(actualDelta(item));
}

@Override
public void describeTo(Description description) {
description.appendText("a numeric value within ")
.appendValue(delta)
.appendText(" of ")
.appendValue(value);
}

private double actualDelta(Float item) {
return (Math.abs((item - value)) - delta);
}

/**
* Creates a matcher of {@link Float}s that matches when an examined float is equal
* to the specified <code>operand</code>, within a range of +/- <code>error</code>.
* <p/>
* For example:
* <pre>assertThat(1.03f, is(floatCloseTo(1.0, 0.03)))</pre>
*
* @param operand
* the expected value of matching doubles
* @param error
* the delta (+/-) within which matches will be allowed
*/
@Factory
public static Matcher<Float> floatCloseTo(double operand, double error) {
return new FloatCloseTo(operand, error);
}


}

0 comments on commit 49a115b

Please sign in to comment.