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 29, 2024
1 parent 4d8457e commit b2be6af
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 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 @@ -73,10 +75,49 @@ protected final Object resolveExpectedValue(ClientYamlTestExecutionContext execu
}

protected final Object getActualValue(ClientYamlTestExecutionContext executionContext) throws IOException {
Object actualValue;
if (executionContext.stash().containsStashedValue(field)) {
return executionContext.stash().getValue(field);
actualValue = executionContext.stash().getValue(field);
} else {
actualValue = executionContext.response(field);
}
// Expected/actual values have compatible types, or they're not expected to have the same. Can just return.
if (actualValue == null || expectedValue.getClass().isAssignableFrom(actualValue.getClass()) || !expectedAndActualHaveSameType()) {
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());
}
}
return executionContext.response(field);
// 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;
}

protected boolean expectedAndActualHaveSameType() {
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ protected void doAssert(Object actualValue, Object expectedValue) {
fail("'contains' only supports checking an object against a list of objects");
}
}

@Override
protected boolean expectedAndActualHaveSameType() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ protected void doAssert(Object actualValue, Object expectedValue) {
private String errorMessage() {
return "field [" + getField() + "] has a true value but it shouldn't";
}

@Override
protected boolean expectedAndActualHaveSameType() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ protected void doAssert(Object actualValue, Object expectedValue) {
private String errorMessage() {
return "field [" + getField() + "] doesn't have a true value";
}

@Override
protected boolean expectedAndActualHaveSameType() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ protected void doAssert(Object actualValue, Object expectedValue) {
private String errorMessage() {
return "field [" + getField() + "] doesn't have length [" + getExpectedValue() + "]";
}

@Override
protected boolean expectedAndActualHaveSameType() {
return false; // Expected value is a number, while actual will some type with a length.
}
}

0 comments on commit b2be6af

Please sign in to comment.