From 1fa2b0e192fd0386a47e006287764c1fe130da75 Mon Sep 17 00:00:00 2001 From: Matthew J Greenwood Date: Fri, 11 Dec 2015 06:22:11 -0500 Subject: [PATCH] added test, removed foobar from min test - derp --- .../function/numeric/AbstractAggregation.java | 6 ++--- .../jsonpath/internal/path/PathCompiler.java | 17 ++++++++++++-- .../internal/function/NestedFunctionTest.java | 22 +++++++++++++++++-- .../function/NumericPathFunctionTest.java | 2 +- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java b/json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java index 0f5cb954..8af57b44 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java @@ -43,17 +43,15 @@ public abstract class AbstractAggregation implements PathFunction { next(value); } } - return getValue(); } - else if (parameters != null) { + if (parameters != null) { for (Parameter param : parameters) { if (param.getCachedValue() instanceof Number) { Number value = (Number)param.getCachedValue(); next(value); } } - return getValue(); } - return null; + return getValue(); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java index bb9089f7..4dbb0b51 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java @@ -223,6 +223,20 @@ public class PathCompiler { return path.currentIsTail() || readNextToken(appender); } + /** + * Parse the parameters of a function call, either the caller has supplied JSON data, or the caller has supplied + * another path expression which must be evaluated and in turn invoked against the root document. In this tokenizer + * we're only concerned with parsing the path thus the output of this function is a list of parameters with the Path + * set if the parameter is an expression. If the parameter is a JSON document then the value of the cachedValue is + * set on the object. + * + * @param readPosition + * The current position within the stream we've advanced - TODO remove the need for this... + * @return + * An ordered list of parameters that are to processed via the function. Typically functions either process + * an array of values and/or can consume parameters in addition to the values provided from the consumption of + * an array. + */ private List parseFunctionParameters(int readPosition) { PathToken currentToken; List parameters = new ArrayList(); @@ -230,7 +244,7 @@ public class PathCompiler { Boolean insideParameter = false; int braceCount = 0, parenCount = 1; while (path.inBounds(readPosition)) { - char c = path.charAt(readPosition); + char c = path.charAt(readPosition++); if (c == OPEN_BRACE) { braceCount++; @@ -273,7 +287,6 @@ public class PathCompiler { parameter.append(c); } } - readPosition++; } path.setPosition(readPosition); return parameters; diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java index 6878087e..ef13e37a 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java @@ -28,7 +28,25 @@ public class NestedFunctionTest extends BaseFunctionTest { } @Test - public void testAverageOfDoubles() { - verifyMathFunction(conf, "$.avg({$.numbers.min()}, {$.numbers.max()})", 5.5); + public void testParameterAverageFunctionCall() { + verifyMathFunction(conf, "$avg({$.numbers.min()}, {$.numbers.max()})", 5.5); + } + + @Test + public void testArrayAverageFunctionCall() { + verifyMathFunction(conf, "$.numbers.avg()", 5.5); + } + + /** + * This test calculates the following: + * + * For each number in $.numbers 1 -> 10 add each number up, + * then add 1 (min), 10 (max) + * + * Alternatively 1+2+3+4+5+6+7+8+9+10+1+10 == 66 + */ + @Test + public void testArrayAverageFunctionCallWithParameters() { + verifyMathFunction(conf, "$.numbers.sum({$.numbers.min()}, {$.numbers.max()})", 66.0); } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/NumericPathFunctionTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/NumericPathFunctionTest.java index 3b9a3cb7..f3903aa0 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/NumericPathFunctionTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/NumericPathFunctionTest.java @@ -57,7 +57,7 @@ public class NumericPathFunctionTest extends BaseFunctionTest { @Test public void testMinOfDouble() { - verifyMathFunction(conf, "$.numbers.min(foobar)", 1d); + verifyMathFunction(conf, "$.numbers.min()", 1d); } @Test