Browse Source

don't accept single '=' in array eval.

pull/7/head
Kalle Stenflo 13 years ago
parent
commit
557eb72d23
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
  2. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
  3. 6
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  4. 12
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java

@ -36,7 +36,7 @@ public class ArrayEvalFilter extends Filter {
@Override
public Object filter(Object obj, JsonProvider jsonProvider) {
//[?(@.isbn = 10)]
//[?(@.isbn == 10)]
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();

8
json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java

@ -28,7 +28,7 @@ public class ExpressionEvaluator {
Long a = (Long) actual;
Long e = Long.parseLong(expected.trim());
if ("=".equals(comparator) || "==".equals(comparator)) {
if ("==".equals(comparator)) {
return a.longValue() == e.longValue();
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return a.longValue() != e.longValue();
@ -45,7 +45,7 @@ public class ExpressionEvaluator {
Integer a = (Integer) actual;
Integer e = Integer.parseInt(expected.trim());
if ("=".equals(comparator)) {
if ("==".equals(comparator)) {
return a.intValue() == e.intValue();
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return a.intValue() != e.intValue();
@ -63,7 +63,7 @@ public class ExpressionEvaluator {
Double a = (Double) actual;
Double e = Double.parseDouble(expected.trim());
if ("=".equals(comparator) || "==".equals(comparator)) {
if ("==".equals(comparator)) {
return a.doubleValue() == e.doubleValue();
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return a.doubleValue() != e.doubleValue();
@ -87,7 +87,7 @@ public class ExpressionEvaluator {
expected = expected.substring(0, expected.length()-1);
}
if ("=".equals(comparator) || "==".equals(comparator)) {
if ("==".equals(comparator)) {
return a.equals(expected);
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return !a.equals(expected);

6
json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java

@ -17,7 +17,6 @@ public class ExpressionEvalTest {
@Test
public void long_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(1L, "=", "1"));
assertTrue(ExpressionEvaluator.eval(1L, "==", "1"));
assertTrue(ExpressionEvaluator.eval(2L, "!=", "1"));
assertTrue(ExpressionEvaluator.eval(2L, ">", "1"));
@ -30,14 +29,13 @@ public class ExpressionEvalTest {
assertFalse(ExpressionEvaluator.eval(1, ">=", "2"));
assertFalse(ExpressionEvaluator.eval(2, "<", "1"));
assertFalse(ExpressionEvaluator.eval(2, "<=", "1"));
assertFalse(ExpressionEvaluator.eval(1, "=", "2"));
assertFalse(ExpressionEvaluator.eval(1, "==", "2"));
assertFalse(ExpressionEvaluator.eval(1, "!=", "1"));
}
@Test
public void double_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(1D, "=", "1"));
assertTrue(ExpressionEvaluator.eval(1D, "==", "1"));
assertTrue(ExpressionEvaluator.eval(2D, "!=", "1"));
assertTrue(ExpressionEvaluator.eval(2D, ">", "1"));
@ -50,7 +48,7 @@ public class ExpressionEvalTest {
assertFalse(ExpressionEvaluator.eval(1D, ">=", "2"));
assertFalse(ExpressionEvaluator.eval(2D, "<", "1"));
assertFalse(ExpressionEvaluator.eval(2D, "<=", "1"));
assertFalse(ExpressionEvaluator.eval(1D, "=", "2"));
assertFalse(ExpressionEvaluator.eval(1D, "==", "2"));
assertFalse(ExpressionEvaluator.eval(1D, "!=", "1"));
}

12
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -77,7 +77,7 @@ public class JsonPathTest {
@Test
public void array_start_expands() throws Exception {
//assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE"));
assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@['parent'] = 'ONE')].child.name"), hasItems("NAME_ONE"));
assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@['parent'] == 'ONE')].child.name"), hasItems("NAME_ONE"));
}
@Test
@ -102,7 +102,7 @@ public class JsonPathTest {
@Test
public void filter_an_array() throws Exception {
List<Object> matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]");
List<Object> matches = JsonPath.read(ARRAY, "$.[?(@.value == 1)]");
assertEquals(1, matches.size());
System.out.println(matches);
@ -231,22 +231,22 @@ public class JsonPathTest {
@Test
public void dot_in_predicate_works() throws Exception {
assertThat(JsonPath.<List<String>>read(PRODUCT_JSON, "$.product[?(@.version='4.0')].codename"), hasItems("Montreal"));
assertThat(JsonPath.<List<String>>read(PRODUCT_JSON, "$.product[?(@.version=='4.0')].codename"), hasItems("Montreal"));
}
@Test
public void dots_in_predicate_works() throws Exception {
assertThat(JsonPath.<List<String>>read(PRODUCT_JSON, "$.product[?(@.attr.with.dot='A')].codename"), hasItems("Seattle"));
assertThat(JsonPath.<List<String>>read(PRODUCT_JSON, "$.product[?(@.attr.with.dot=='A')].codename"), hasItems("Seattle"));
}
@Test
public void all_books_with_category_reference() throws Exception {
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@.category='reference')].title"), hasItems("Sayings of the Century"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[?(@.category='reference')].title"), hasItems("Sayings of the Century"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@.category=='reference')].title"), hasItems("Sayings of the Century"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[?(@.category=='reference')].title"), hasItems("Sayings of the Century"));
}

Loading…
Cancel
Save