Skip to content

Commit

Permalink
refactor: Add new helper to evaluate expression with context
Browse files Browse the repository at this point in the history
  • Loading branch information
saig0 committed Sep 8, 2023
1 parent 38219f4 commit b9a449b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/scala/org/camunda/feel/impl/FeelEngineTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,30 +107,23 @@ 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.")
)
}

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.")
)
}

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.")
Expand Down

0 comments on commit b9a449b

Please sign in to comment.