Browse Source

Make it possible to use isJson() and hasJsonPath() matchers directly on ReadContexts.

pull/263/head
Patrik Helsing 8 years ago
parent
commit
bd5b682c7b
  1. 2
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java
  2. 11
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java

2
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java

@ -58,6 +58,8 @@ public class IsJson<T> extends TypeSafeMatcher<T> {
return JsonPath.parse((String) object); return JsonPath.parse((String) object);
} else if (object instanceof File) { } else if (object instanceof File) {
return JsonPath.parse((File) object); return JsonPath.parse((File) object);
} else if (object instanceof ReadContext) {
return (ReadContext) object;
} else { } else {
return JsonPath.parse(object); return JsonPath.parse(object);
} }

11
json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java

@ -1,6 +1,8 @@
package com.jayway.jsonpath.matchers; package com.jayway.jsonpath.matchers;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import com.jayway.jsonpath.matchers.helpers.StrictParsingConfiguration; import com.jayway.jsonpath.matchers.helpers.StrictParsingConfiguration;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -209,4 +211,13 @@ public class JsonPathMatchersTest {
Object json = Configuration.defaultConfiguration().jsonProvider().parse(BOOKS_JSON); Object json = Configuration.defaultConfiguration().jsonProvider().parse(BOOKS_JSON);
assertThat(json, hasJsonPath("$.store.name", equalTo("Little Shop"))); assertThat(json, hasJsonPath("$.store.name", equalTo("Little Shop")));
} }
@Test
public void shouldMatchJsonPathOnReadContext() {
String test = "{\"foo\":\"bar\"}";
ReadContext context = JsonPath.parse(test);
assertThat(context, hasJsonPath("$.foo"));
assertThat(context, hasJsonPath("$.foo", equalTo("bar")));
assertThat(context, hasNoJsonPath("$.zoo"));
}
} }

Loading…
Cancel
Save