From 445b9c4ebe6d565e8bd78ac8e159760dc9bb8d00 Mon Sep 17 00:00:00 2001 From: Michael Thacker Date: Fri, 6 Apr 2018 17:10:27 -0700 Subject: [PATCH] Fix compiling of functions with single character arguments --- .../com/jayway/jsonpath/internal/path/PathCompiler.java | 2 +- .../test/java/com/jayway/jsonpath/PathCompilerTest.java | 8 ++++++++ .../jsonpath/internal/function/NestedFunctionTest.java | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) 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 0317311e..e3e88b9c 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 @@ -195,7 +195,7 @@ public class PathCompiler { } else if (c == OPEN_PARENTHESIS) { isFunction = true; - endPosition = readPosition++; + endPosition = readPosition; break; } readPosition++; diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java index 699ba631..a4b6feb5 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java @@ -252,6 +252,14 @@ public class PathCompilerTest { assertThat(result).containsExactly("] it"); } + @Test + public void a_function_can_be_compiled() { + assertThat(compile("$.aaa.foo()").toString()).isEqualTo("$['aaa'].foo()"); + assertThat(compile("$.aaa.foo(5)").toString()).isEqualTo("$['aaa'].foo(...)"); + assertThat(compile("$.aaa.foo($.bar)").toString()).isEqualTo("$['aaa'].foo(...)"); + assertThat(compile("$.aaa.foo(5,10,15)").toString()).isEqualTo("$['aaa'].foo(...)"); + } + @Test(expected = InvalidPathException.class) public void array_indexes_must_be_separated_by_commas() { compile("$[0, 1, 2 4]"); 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 77226b4a..7ab9f972 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 @@ -58,6 +58,12 @@ public class NestedFunctionTest extends BaseFunctionTest { verifyMathFunction(conf, "$.sum(5, 3, $.numbers.max(), 2)", 20.0); } + @Test + public void testSimpleLiteralArgument() { + verifyMathFunction(conf, "$.sum(5)", 5.0); + verifyMathFunction(conf, "$.sum(50)", 50.0); + } + @Test public void testStringConcat() { verifyTextFunction(conf, "$.text.concat()", "abcdef");