Skip to content

Commit 337e8b2

Browse files
authored
Add stash support to close_to for YAML tests (#141459)
1 parent 15a07d1 commit 337e8b2

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

test/yaml-rest-runner/src/main/java/org/elasticsearch/test/rest/yaml/section/CloseToAssertion.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public static CloseToAssertion parse(XContentParser parser) throws IOException {
3737
throw new IllegalArgumentException("expected a map with value and error but got a map with " + map.size() + " fields");
3838
}
3939
Object valObj = map.get("value");
40-
if (valObj instanceof Number == false) {
41-
throw new IllegalArgumentException("value is missing or not a number");
40+
if (valObj == null) {
41+
throw new IllegalArgumentException("value is missing");
4242
}
4343
Object errObj = map.get("error");
4444
if (errObj instanceof Number == false) {
4545
throw new IllegalArgumentException("error is missing or not a number");
4646
}
47-
return new CloseToAssertion(location, fieldValueTuple.v1(), ((Number) valObj).doubleValue(), ((Number) errObj).doubleValue());
47+
return new CloseToAssertion(location, fieldValueTuple.v1(), valObj, ((Number) errObj).doubleValue());
4848
} else {
4949
throw new IllegalArgumentException(
5050
"expected a map with value and error but got " + fieldValueTuple.v2().getClass().getSimpleName()
@@ -57,7 +57,7 @@ public static CloseToAssertion parse(XContentParser parser) throws IOException {
5757

5858
private final double error;
5959

60-
public CloseToAssertion(XContentLocation location, String field, Double expectedValue, Double error) {
60+
public CloseToAssertion(XContentLocation location, String field, Object expectedValue, Double error) {
6161
super(location, field, expectedValue);
6262
this.error = error;
6363
}
@@ -69,10 +69,14 @@ public final double getError() {
6969
@Override
7070
protected void doAssert(Object actualValue, Object expectedValue) {
7171
logger.trace("assert that [{}] is close to [{}] with error [{}] (field [{}])", actualValue, expectedValue, error, getField());
72+
if ((expectedValue instanceof Number) == false) {
73+
throw new AssertionError("Expected value should be a number, but was [" + expectedValue + "], which is not a number");
74+
}
75+
7276
if (actualValue instanceof Number actualValueNumber) {
73-
assertThat(actualValueNumber.doubleValue(), closeTo((Double) expectedValue, error));
77+
assertThat(actualValueNumber.doubleValue(), closeTo(((Number) expectedValue).doubleValue(), error));
7478
} else {
75-
throw new AssertionError("excpected a value close to " + expectedValue + " but got " + actualValue + ", which is not a number");
79+
throw new AssertionError("expected a value close to " + expectedValue + " but got " + actualValue + ", which is not a number");
7680
}
7781
}
7882
}

test/yaml-rest-runner/src/test/java/org/elasticsearch/test/rest/yaml/section/AssertionTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void testCloseTo() throws Exception {
168168

169169
public void testInvalidCloseTo() throws Exception {
170170
parser = createParser(YamlXContent.yamlXContent, "{ field: 42 }");
171-
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> CloseToAssertion.parse(parser));
171+
Throwable exception = expectThrows(IllegalArgumentException.class, () -> CloseToAssertion.parse(parser));
172172
assertThat(exception.getMessage(), equalTo("expected a map with value and error but got Integer"));
173173

174174
parser = createParser(YamlXContent.yamlXContent, "{ field: { } }");
@@ -181,7 +181,12 @@ public void testInvalidCloseTo() throws Exception {
181181

182182
parser = createParser(YamlXContent.yamlXContent, "{ field: { foo: 13, bar: 15 } }");
183183
exception = expectThrows(IllegalArgumentException.class, () -> CloseToAssertion.parse(parser));
184-
assertThat(exception.getMessage(), equalTo("value is missing or not a number"));
184+
assertThat(exception.getMessage(), equalTo("value is missing"));
185+
186+
parser = createParser(YamlXContent.yamlXContent, "{ field: { value: \"foo\", error: 0.001 } }");
187+
CloseToAssertion closeToAssertion = CloseToAssertion.parse(parser);
188+
exception = expectThrows(AssertionError.class, () -> closeToAssertion.doAssert(42, closeToAssertion.getExpectedValue()));
189+
assertThat(exception.getMessage(), equalTo("Expected value should be a number, but was [foo], which is not a number"));
185190
}
186191

187192
public void testExists() throws IOException {

0 commit comments

Comments
 (0)