diff --git a/src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala b/src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala index b9c3b2254..a58166be2 100644 --- a/src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala +++ b/src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala @@ -688,22 +688,16 @@ object FeelParser { }.getOrElse(ConstNull) } - // replace escaped character with the provided replacement private def translateEscapes(input: String): String = { - val escapeMap = Map( - "\\b" -> "\b", - "\\t" -> "\t", - "\\n" -> "\n", - "\\f" -> "\f", - "\\r" -> "\r", - "\\\"" -> "\"", - "\\'" -> "'", - "\\s" -> " " - // Add more escape sequences as needed - ) - - escapeMap.foldLeft(input) { case (result, (escape, replacement)) => - result.replace(escape, replacement) - } + // replace all escape sequences + input + .replaceAll("(?'" in { - eval(""" "b" > "a" """) should be(ValBoolean(true)) - eval(""" "a" > "b" """) should be(ValBoolean(false)) + evaluateExpression(""" "b" > "a" """) should returnResult(true) + evaluateExpression(""" "a" > "b" """) should returnResult(false) } it should "compare with '>='" in { - eval(""" "b" >= "b" """) should be(ValBoolean(true)) - eval(""" "a" >= "b" """) should be(ValBoolean(false)) + evaluateExpression(""" "b" >= "b" """) should returnResult(true) + evaluateExpression(""" "a" >= "b" """) should returnResult(false) } it should "compare with null" in { - eval(""" "a" = null """) should be(ValBoolean(false)) - eval(""" null = "a" """) should be(ValBoolean(false)) - eval(""" "a" != null """) should be(ValBoolean(true)) + evaluateExpression(""" "a" = null """) should returnResult(false) + evaluateExpression(""" null = "a" """) should returnResult(false) + evaluateExpression(""" "a" != null """) should returnResult(true) } - it should "return not escaped characters" in { - - eval(""" "Hello\nWorld" """) should be(ValString("Hello\nWorld")) - eval(" x ", Map("x" -> "Hello\nWorld")) should be(ValString("Hello\nWorld")) + private val escapeSequences = Table( + ("Character", "Expected", "Display name"), + ('\n', '\n', "new line"), + ('\r', '\r', "carriage return"), + ('\t', '\t', "tab"), + ('\b', '\b', "backspace"), + ('\f', '\f', "form feed"), + ('\'', '\'', "single quote"), + ("\\\"", '"', "double quote"), + ("\\\\", '\\', "backslash") + ) - eval(""" "Hello\rWorld" """) should be(ValString("Hello\rWorld")) - eval(" x ", Map("x" -> "Hello\rWorld")) should be(ValString("Hello\rWorld")) + it should "contains an escape sequence" in { + forEvery(escapeSequences) { (character, expected, _) => + val expectedString = s"a $expected b" - eval(""" "Hello\'World" """) should be(ValString("Hello\'World")) - eval(" x ", Map("x" -> "Hello\'World")) should be(ValString("Hello\'World")) + evaluateExpression(s" \"a $character b\" ") should returnResult(expectedString) + evaluateExpression("char", Map("char" -> expectedString)) should returnResult(expectedString) + } + } - eval(""" "Hello\tWorld" """) should be(ValString("Hello\tWorld")) - eval(" x ", Map("x" -> "Hello\tWorld")) should be(ValString("Hello\tWorld")) + private val unicodeCharacters = Table( + ("Character", "Display name"), + ('\u269D', "\\u269D"), + ("\\U101EF", "\\U101EF") + ) - eval(""" "Hello\"World" """) should be(ValString("Hello\"World")) - eval(" x ", Map("x" -> "Hello\"World")) should be(ValString("Hello\"World")) + it should "contains unicode characters" in { + forEvery(unicodeCharacters) { (character, _) => + evaluateExpression(s" \"a $character b\" ") should returnResult(s"a $character b") + } } - List( - " \' ", - " \\ ", - " \n ", - " \r ", - " \t ", - """ \u269D """, - """ \U101EF """ + private val regexCharacters = Table( + ("Character", "Display name"), + ("\\s", "\\s"), + ("\\S", "\\S"), + ("\\d", "\\d"), + ("\\w", "\\w"), + ("\\R", "\\R"), + ("\\h", "\\h"), + ("\\v", "\\v"), + ("\\\n", "\\n"), + ("\\\r", "\\r") ) - .foreach { notEscapeChar => - it should s"contains a not escape sequence ($notEscapeChar)" in { - - eval(s""" "a $notEscapeChar b" """) should be( - ValString( - s"""a $notEscapeChar b""" - ) - ) - } + + it should "contains a regex character" in { + forEvery(regexCharacters) { (character, _) => + val expectedString = s"a $character b" + + evaluateExpression(s" \"a $character b\" ") should returnResult(expectedString) + evaluateExpression("char", Map("char" -> expectedString)) should returnResult(expectedString) } + } }