From 7be428d4173e3c050e47530b295ee2e52e819e21 Mon Sep 17 00:00:00 2001 From: Matheus Ribeiro Lima <57918064+matheusribeirozup@users.noreply.github.com> Date: Fri, 28 Aug 2020 19:56:44 -0300 Subject: [PATCH] fix: parse expression with index path (#851) * fix: parse expression with index path * resolved detekt warning --- .../android/context/tokenizer/TokenInterpreter.kt | 14 ++++++++------ .../android/context/tokenizer/TokenParserTest.kt | 13 +++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/android/beagle/src/main/java/br/com/zup/beagle/android/context/tokenizer/TokenInterpreter.kt b/android/beagle/src/main/java/br/com/zup/beagle/android/context/tokenizer/TokenInterpreter.kt index f989985be0..e9a8f3325d 100644 --- a/android/beagle/src/main/java/br/com/zup/beagle/android/context/tokenizer/TokenInterpreter.kt +++ b/android/beagle/src/main/java/br/com/zup/beagle/android/context/tokenizer/TokenInterpreter.kt @@ -30,17 +30,17 @@ internal class TokenInterpreter(value: String) { } private fun readToken(): Token? { - return when { - lastChar == '\'' -> readString() - lastChar == '(' -> { + return when (lastChar) { + '\'' -> readString() + '(' -> { resetLastChar() tokenOpenBracket() } - lastChar == ')' -> { + ')' -> { resetLastChar() tokenOfCloseBracket() } - lastChar == ',' -> { + ',' -> { resetLastChar() tokenOfComma() } @@ -79,7 +79,7 @@ internal class TokenInterpreter(value: String) { private fun readVariableOrFunction(): Token? { val sb = StringBuilder() - while (lastChar.isJavaIdentifierPart() || lastChar == '.') { + while (lastChar.isJavaIdentifierPart() || isExpressionCharacter()) { sb.append(lastChar) lastChar = reader.read().toChar() } @@ -101,6 +101,8 @@ internal class TokenInterpreter(value: String) { } } + private fun isExpressionCharacter() = lastChar == '.' || lastChar == '[' || lastChar == ']' + private fun resetLastChar() { lastChar = Char.MIN_VALUE } diff --git a/android/beagle/src/test/java/br/com/zup/beagle/android/context/tokenizer/TokenParserTest.kt b/android/beagle/src/test/java/br/com/zup/beagle/android/context/tokenizer/TokenParserTest.kt index 1c046069f0..693789a256 100644 --- a/android/beagle/src/test/java/br/com/zup/beagle/android/context/tokenizer/TokenParserTest.kt +++ b/android/beagle/src/test/java/br/com/zup/beagle/android/context/tokenizer/TokenParserTest.kt @@ -127,6 +127,19 @@ class TokenParserTest { assertEquals(expression, result.token.value) } + @Test + fun parse_should_return_binding_with_path_list() { + // Given + val expression = "bindingId.bindingValue[0]" + + // When + val result = tokenParser.parse(expression) + + // Then + assertTrue { result.token is TokenBinding } + assertEquals(expression, result.token.value) + } + @Test fun parse_should_return_binding_with_number() { // Given