Skip to content

Commit

Permalink
VariableNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkaczor committed Aug 6, 2023
1 parent 59e477e commit 5506b11
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
21 changes: 12 additions & 9 deletions src/main/scala/br/com/virsox/scalexpr/SimpleExpression.scala
Expand Up @@ -41,19 +41,22 @@ trait Variable[T] extends SimpleExpression[T] {
* @return Value of the variable.
*/
override def resolve[A](context: A = EmptyContext)(implicit ctxLike: ContextLike[A]): T = {
val tokens = name.split("\\.")
val (eventNames, attribute) = tokens.splitAt(tokens.length - 1)
val tokens = name.split("\\.")
val (path, field) = tokens.splitAt(tokens.length - 1) match {
case (p, fieldArray) => (p, fieldArray(0))
}

val lastEventOption =
eventNames.foldLeft(Some(context): Option[A])((ctx, field) => ctx.flatMap(c => ctxLike.nested(c, field)))
val lastContext =
path.foldLeft(Some(context): Option[A])((ctx, field) => ctx.flatMap(c => ctxLike.nested(c, field)))

lastEventOption match {
lastContext match {
case Some(ctx) =>
ctxLike.get[T](ctx, attribute(0)) match {
case Some(value) => value
case None => throw new IllegalArgumentException("variable not found in context")
ctxLike.get[T](ctx, field) match {
case Some(value) => value
case None => throw VariableNotFoundException(name)
}
case None => throw new IllegalArgumentException("variable not found in context")
case None =>
throw VariableNotFoundException(name)
}
}
}
Expand Down
@@ -0,0 +1,3 @@
package br.com.virsox.scalexpr

case class VariableNotFoundException(variableName: String) extends Exception(s"Variable '$variableName' not found")
Expand Up @@ -110,9 +110,11 @@ class ExpressionParserTest extends AnyFlatSpec with Matchers {
}

it should "parse boolean expression with var" in new Fixture {
verify[Boolean](parser.parseBooleanExpression("""false || detected"""), {
BooleanConstant(false) || BooleanVar("detected")
})
verify[Boolean](
parser.parseBooleanExpression("""false || detected"""), {
BooleanConstant(false) || BooleanVar("detected")
}
)
}

it should "parse comparison expressions with variables on both sides" in new Fixture {
Expand Down
16 changes: 14 additions & 2 deletions src/test/scala/br/com/virsox/scalexpr/ExpressionTest.scala
@@ -1,10 +1,10 @@
package br.com.virsox.scalexpr

import java.time.Instant

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.time.Instant

/** * Tests for expression evaluation
*/
class ExpressionTest extends AnyFlatSpec with Matchers {
Expand Down Expand Up @@ -174,6 +174,18 @@ class ExpressionTest extends AnyFlatSpec with Matchers {

}

"Resolving expression" should "fail with VariableNotFoundException when variable is missing" in {
val strVar = StringVar("name")
val str2Var = StringVar("path.name")
val context: Map[String, Any] = Map.empty
val context2: Map[String, Any] = Map("path" -> Map.empty)

the [VariableNotFoundException] thrownBy (strVar == strVar).resolve(context) should have message "Variable 'name' not found"
the [VariableNotFoundException] thrownBy (str2Var == str2Var).resolve(context) should have message "Variable 'path.name' not found"
the [VariableNotFoundException] thrownBy (str2Var == str2Var).resolve(context2) should have message "Variable 'path.name' not found"

}

"A relational expression" should "evaluate when using dates" in new TestWithDates {

val cons1 = DateTimeConstant(date1)
Expand Down

0 comments on commit 5506b11

Please sign in to comment.