From b9a449b235a500d1173bfc80ad2fe456f1ff4b4f Mon Sep 17 00:00:00 2001 From: Philipp Ossler Date: Fri, 8 Sep 2023 06:40:21 +0200 Subject: [PATCH] refactor: Add new helper to evaluate expression with context --- .../feel/api/context/CustomContextTest.scala | 20 ++++++++----------- .../camunda/feel/impl/FeelEngineTest.scala | 20 +++++++++++++++++++ ...terNonExistingVariableExpressionTest.scala | 14 ++++--------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/test/scala/org/camunda/feel/api/context/CustomContextTest.scala b/src/test/scala/org/camunda/feel/api/context/CustomContextTest.scala index 2fcf5cfe6..2de333ff1 100644 --- a/src/test/scala/org/camunda/feel/api/context/CustomContextTest.scala +++ b/src/test/scala/org/camunda/feel/api/context/CustomContextTest.scala @@ -62,18 +62,17 @@ class CustomContextTest extends AnyFlatSpec with Matchers with FeelEngineTest wi } - engine.evaluateExpression( + evaluateExpression( expression = "a", context = myCustomContext ) should returnResult(2) - engine.evaluateExpression( + evaluateExpression( expression = "floor(3.8)", context = myCustomContext) should returnResult(3) - engine.evaluateUnaryTests( + evaluateUnaryTests( expression = "2", - inputValue = 2, context = myCustomContext) should returnResult(true) } @@ -109,14 +108,13 @@ class CustomContextTest extends AnyFlatSpec with Matchers with FeelEngineTest wi override val functionProvider = myFunctionProvider } - engine.evaluateExpression( + evaluateExpression( expression = "a + f(2) + a + f(8)", context = myCustomContext ) should returnResult(18) variableCallCount should be(2) functionCallCount should be(2) - } it should "evaluate expression" in { @@ -126,7 +124,7 @@ class CustomContextTest extends AnyFlatSpec with Matchers with FeelEngineTest wi override val variableProvider = SimpleTestContext(variables) } - engine.evaluateExpression( + evaluateExpression( expression = "foo", context = context ) should returnResult(7) @@ -139,7 +137,7 @@ class CustomContextTest extends AnyFlatSpec with Matchers with FeelEngineTest wi override val variableProvider = SimpleTestContext(variables) } - engine.evaluateExpression( + evaluateExpression( expression = "bar", context = context ) should returnNull() @@ -159,9 +157,8 @@ class CustomContextTest extends AnyFlatSpec with Matchers with FeelEngineTest wi List(inputVariableContext, SimpleTestContext(variables))) } - engine.evaluateUnaryTests( + evaluateUnaryTests( expression = "foo", - inputValue = 8, context = context) should returnResult(false) } @@ -174,9 +171,8 @@ class CustomContextTest extends AnyFlatSpec with Matchers with FeelEngineTest wi List(inputVariableContext, SimpleTestContext(variables))) } - engine.evaluateUnaryTests( + evaluateUnaryTests( expression = "foo", - inputValue = 8, context = context) should returnResult(false) } diff --git a/src/test/scala/org/camunda/feel/impl/FeelEngineTest.scala b/src/test/scala/org/camunda/feel/impl/FeelEngineTest.scala index d23aa6655..5bbb3909f 100644 --- a/src/test/scala/org/camunda/feel/impl/FeelEngineTest.scala +++ b/src/test/scala/org/camunda/feel/impl/FeelEngineTest.scala @@ -39,6 +39,13 @@ trait FeelEngineTest { engine.evaluateExpression(expression, context) } + def evaluateExpression( + expression: String, + context: Context + ): EvaluationResult = { + engine.evaluateExpression(expression, context) + } + def evaluateUnaryTests( expression: String, inputValue: Any, @@ -57,6 +64,19 @@ trait FeelEngineTest { ) } + def evaluateUnaryTests( + expression: String, + context: Context + ): EvaluationResult = { + // use parse + evaluate to avoid setting an input value + val parsedExpression = engine.parseUnaryTests(expression).parsedExpression + + engine.evaluate( + expression = parsedExpression, + context = context + ) + } + def evaluateFunction(function: String): ValFunction = { engine.evaluateExpression(function) match { case SuccessfulEvaluationResult(result: ValFunction, _) => result diff --git a/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterNonExistingVariableExpressionTest.scala b/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterNonExistingVariableExpressionTest.scala index cd9dee97f..fdc972711 100644 --- a/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterNonExistingVariableExpressionTest.scala +++ b/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterNonExistingVariableExpressionTest.scala @@ -17,6 +17,7 @@ package org.camunda.feel.impl.interpreter; import org.camunda.feel.api.EvaluationFailureType +import org.camunda.feel.context.Context import org.camunda.feel.impl.{EvaluationResultMatchers, FeelEngineTest} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers @@ -106,10 +107,7 @@ class InterpreterNonExistingVariableExpressionTest } "A non-existing input value" should "be equal to null" in { - // use parse + evaluate to avoid setting an input value - val parsedExpression = engine.parseUnaryTests("null").parsedExpression - - engine.evaluate(parsedExpression) should ( + evaluateUnaryTests(expression = "null", context = Context.EmptyContext) should ( returnResult(true) and reportFailure( failureType = EvaluationFailureType.NO_VARIABLE_FOUND, failureMessage = "No input value found.") @@ -117,9 +115,7 @@ class InterpreterNonExistingVariableExpressionTest } it should "not be equal to a non-null value" in { - val parsedExpression = engine.parseUnaryTests("2").parsedExpression - - engine.evaluate(parsedExpression) should ( + evaluateUnaryTests("2", context = Context.EmptyContext) should ( returnResult(false) and reportFailure( failureType = EvaluationFailureType.NO_VARIABLE_FOUND, failureMessage = "No input value found.") @@ -127,9 +123,7 @@ class InterpreterNonExistingVariableExpressionTest } it should "not compare to a non-null value" in { - val parsedExpression = engine.parseUnaryTests("< 2").parsedExpression - - engine.evaluate(parsedExpression) should ( + evaluateUnaryTests("< 2", context = Context.EmptyContext) should ( returnNull() and reportFailure( failureType = EvaluationFailureType.NO_VARIABLE_FOUND, failureMessage = "No input value found.")