Browse Source

added test, removed foobar from min test - derp

pull/167/head
Matthew J Greenwood 9 years ago
parent
commit
1fa2b0e192
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  2. 17
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  3. 22
      json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java
  4. 2
      json-path/src/test/java/com/jayway/jsonpath/internal/function/NumericPathFunctionTest.java

6
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();
}
}

17
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<Parameter> parseFunctionParameters(int readPosition) {
PathToken currentToken;
List<Parameter> parameters = new ArrayList<Parameter>();
@ -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;

22
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);
}
}

2
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

Loading…
Cancel
Save