fix string template tokenization

commit_hash:9654114d963d26cc03fdbb07e810caa6881959b3
This commit is contained in:
gulevsky
2024-11-14 18:42:22 +03:00
parent cf1aaa3fbb
commit 0d9fdbd23f
4 changed files with 30 additions and 11 deletions
@@ -39,7 +39,7 @@ abstract class Evaluable(val rawExpr: String) {
get() = if (this::expression.isInitialized) {
expression.variables
} else {
tokens.filterIsInstance(Token.Operand.Variable::class.java).map { it.name }
tokens.filterIsInstance<Token.Operand.Variable>().map { it.name }
}
override fun evalImpl(evaluator: Evaluator): Any {
if (!this::expression.isInitialized) {
@@ -31,7 +31,7 @@ internal object Tokenizer {
}
val stringTemplateTokens = mutableListOf<Token>()
val stringLiteral = processString(state, isPartOfExpression)
val stringLiteral = processString(state, isLiteral = isPartOfExpression)
if (state.currentChar().isAtEnd()) {
if (isPartOfExpression) {
@@ -56,7 +56,7 @@ internal object Tokenizer {
val expressionTokens = mutableListOf<Token>()
processExpression(state, expressionTokens)
val stringAfterExpression = processString(state)
val stringAfterExpression = processString(state, isLiteral = isPartOfExpression)
val isSpecialCaseWithExpressionInStringTemplate = !isPartOfExpression
&& stringTemplateTokens.isEmpty()
@@ -11,8 +11,6 @@ import com.yandex.div.core.view2.divs.widgets.DivLineHeightTextView
import com.yandex.div.core.view2.divs.widgets.DivLinearLayout
import com.yandex.div.data.DivParsingEnvironment
import com.yandex.div2.DivData
import java.io.File
import java.lang.AssertionError
import org.json.JSONObject
import org.junit.Assert
import org.junit.Before
@@ -21,6 +19,7 @@ import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import java.io.File
@RunWith(RobolectricTestRunner::class)
class SetVariableForLocalVariablesTest {
@@ -55,7 +54,7 @@ class SetVariableForLocalVariablesTest {
fun `variable with card variable shows and updates card variable`() {
globalText.performClick()
Assert.assertEquals("global string_var = 'new value", globalText.text)
Assert.assertEquals("global string_var = 'new value'", globalText.text)
assertOtherViewsNotChanged(globalText)
}
@@ -63,7 +62,7 @@ class SetVariableForLocalVariablesTest {
fun `variable with local variable shows and updates local variable`() {
localText.performClick()
Assert.assertEquals("local string_var = 'new value", localText.text)
Assert.assertEquals("local string_var = 'new value'", localText.text)
assertOtherViewsNotChanged(localText)
}
@@ -71,19 +70,19 @@ class SetVariableForLocalVariablesTest {
fun `variable with parent local variable shows and updates parent local variable`() {
parentLocalText.performClick()
Assert.assertEquals("parent local string_var = 'new value", parentLocalText.text)
Assert.assertEquals("parent local string_var = 'new value'", parentLocalText.text)
assertOtherViewsNotChanged(parentLocalText)
}
private fun assertOtherViewsNotChanged(view: View?) {
if (view != globalText) {
Assert.assertEquals("global string_var = 'global value", globalText.text)
Assert.assertEquals("global string_var = 'global value'", globalText.text)
}
if (view != localText) {
Assert.assertEquals("local string_var = 'local value", localText.text)
Assert.assertEquals("local string_var = 'local value'", localText.text)
}
if (view != parentLocalText) {
Assert.assertEquals("parent local string_var = 'local value", parentLocalText.text)
Assert.assertEquals("parent local string_var = 'local value'", parentLocalText.text)
}
}
}
@@ -432,6 +432,26 @@
"ios",
"web"
]
},
{
"name": "string template with expression surrounded by apostrophes",
"expression": "Set '@{engine_url}' as your homepage?",
"expected": {
"type": "string",
"value": "Set 'https://ya.ru' as your homepage?"
},
"variables": [
{
"type": "url",
"value": "https://ya.ru",
"name": "engine_url"
}
],
"platforms": [
"android",
"ios",
"web"
]
}
]
}