Skip to content

Commit

Permalink
Add basic type conversion for YAML tests
Browse files Browse the repository at this point in the history
Our yaml rest tests parse expected values based on basic yaml parsing
rules, which don't draw a distinction between float and double values.

If there's a mismatch between the types of the expected and actual
values in an assertion, we should try parsing the actual value to the
expected type. For now, I just added support for the 32- and 64-bit
integer and floating point types.

Signed-off-by: Michael Froh <[email protected]>
  • Loading branch information
msfroh committed May 30, 2024
1 parent 4d8457e commit a679e02
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.fail;

/**
* Base class for executable sections that hold assertions
*/
Expand Down Expand Up @@ -79,6 +81,41 @@ protected final Object getActualValue(ClientYamlTestExecutionContext executionCo
return executionContext.response(field);
}

static Object convertActualValue(Object actualValue, Object expectedValue) {
if (actualValue == null || expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
return actualValue;
}
if (actualValue instanceof Number && expectedValue instanceof Number) {
if (expectedValue instanceof Float) {
return Float.parseFloat(actualValue.toString());
} else if (expectedValue instanceof Double) {
return Double.parseDouble(actualValue.toString());
} else if (expectedValue instanceof Integer) {
return Integer.parseInt(actualValue.toString());
} else if (expectedValue instanceof Long) {
return Long.parseLong(actualValue.toString());
}
}
// Force a class cast exception here, so developers can flesh out the above logic as needed.
try {
expectedValue.getClass().cast(actualValue);
} catch (ClassCastException e) {
fail(
"Type mismatch: Expected value ("
+ expectedValue
+ ") has type "
+ expectedValue.getClass()
+ ". "
+ "Actual value ("
+ actualValue
+ ") has type "
+ actualValue.getClass()
+ "."
);
}
return actualValue;
}

@Override
public XContentLocation getLocation() {
return location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public GreaterThanAssertion(XContentLocation location, String field, Object expe
@Override
protected void doAssert(Object actualValue, Object expectedValue) {
logger.trace("assert that [{}] is greater than [{}] (field: [{}])", actualValue, expectedValue, getField());
actualValue = convertActualValue(actualValue, expectedValue);
assertThat(
"value of [" + getField() + "] is not comparable (got [" + safeClass(actualValue) + "])",
actualValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public GreaterThanEqualToAssertion(XContentLocation location, String field, Obje
@Override
protected void doAssert(Object actualValue, Object expectedValue) {
logger.trace("assert that [{}] is greater than or equal to [{}] (field: [{}])", actualValue, expectedValue, getField());
actualValue = convertActualValue(actualValue, expectedValue);
assertThat(
"value of [" + getField() + "] is not comparable (got [" + safeClass(actualValue) + "])",
actualValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public LessThanAssertion(XContentLocation location, String field, Object expecte
@Override
protected void doAssert(Object actualValue, Object expectedValue) {
logger.trace("assert that [{}] is less than [{}] (field: [{}])", actualValue, expectedValue, getField());
actualValue = convertActualValue(actualValue, expectedValue);
assertThat(
"value of [" + getField() + "] is not comparable (got [" + safeClass(actualValue) + "])",
actualValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public LessThanOrEqualToAssertion(XContentLocation location, String field, Objec
@Override
protected void doAssert(Object actualValue, Object expectedValue) {
logger.trace("assert that [{}] is less than or equal to [{}] (field: [{}])", actualValue, expectedValue, getField());
actualValue = convertActualValue(actualValue, expectedValue);
assertThat(
"value of [" + getField() + "] is not comparable (got [" + safeClass(actualValue) + "])",
actualValue,
Expand Down

0 comments on commit a679e02

Please sign in to comment.