Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: parse expression with index path (#851)
Browse files Browse the repository at this point in the history
* fix: parse expression with index path

* resolved detekt warning
  • Loading branch information
matheusribeirozup authored Aug 28, 2020
1 parent f69e0e5 commit 7be428d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -101,6 +101,8 @@ internal class TokenInterpreter(value: String) {
}
}

private fun isExpressionCharacter() = lastChar == '.' || lastChar == '[' || lastChar == ']'

private fun resetLastChar() {
lastChar = Char.MIN_VALUE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7be428d

Please sign in to comment.