From 675a6b592657cb34e391a3fe0d42b06916672ea5 Mon Sep 17 00:00:00 2001 From: Philipp Ossler Date: Mon, 25 Apr 2022 09:17:48 +0200 Subject: [PATCH] test(engine): verify list filter with function * verify that a list can be filtered with a built-in and custom function * verify that the custom function is invoked with the expected elements --- .../InterpreterListExpressionTest.scala | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterListExpressionTest.scala b/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterListExpressionTest.scala index ee304274f..1d6aa35e5 100644 --- a/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterListExpressionTest.scala +++ b/src/test/scala/org/camunda/feel/impl/interpreter/InterpreterListExpressionTest.scala @@ -21,6 +21,8 @@ import org.camunda.feel.syntaxtree._ import org.scalatest.matchers.should.Matchers import org.scalatest.flatspec.AnyFlatSpec +import scala.collection.mutable.ListBuffer + /** * @author Philipp Ossler */ @@ -97,7 +99,7 @@ class InterpreterListExpressionTest ))) } - it should "be filtered via boolean expression" in { + it should "be filtered via comparison" in { eval("[1,2,3,4][item > 2]") should be( ValList(List(ValNumber(3), ValNumber(4)))) @@ -121,6 +123,39 @@ class InterpreterListExpressionTest eval("[1,2,3,4][i]", Map("i" -> -2)) should be(ValNumber(3)) } + it should "be filtered via boolean expression" in { + eval("[1,2,3,4][odd(item)]") should be( + ValList(List(ValNumber(1), ValNumber(3)))) + + eval("[1,2,3,4][even(item)]") should be( + ValList(List(ValNumber(2), ValNumber(4)))) + } + + it should "be filtered via custom function" in { + val functionInvocations: ListBuffer[Val] = ListBuffer.empty + + val result = eval( + expression = "[1,2,3,4][f(item)]", + variables = Map(), + functions = Map("f" -> ValFunction( + params = List("x"), + invoke = { + case List(x) => + functionInvocations += x + ValBoolean(x == ValNumber(3)) + } + ))) + + result should be(ValList(List(ValNumber(3)))) + + functionInvocations should be(List( + ValNumber(1), + ValNumber(2), + ValNumber(3), + ValNumber(4)) + ) + } + it should "be filtered multiple times (from literal)" in { eval("[[1]][1][1]") should be(ValNumber(1)) eval("[[[1]]][1][1][1]") should be(ValNumber(1))