From 0d9fdbd23f0100c1d62b97d42b2fa03175285c89 Mon Sep 17 00:00:00 2001 From: gulevsky Date: Thu, 14 Nov 2024 18:42:22 +0300 Subject: [PATCH] fix string template tokenization commit_hash:9654114d963d26cc03fdbb07e810caa6881959b3 --- .../com/yandex/div/evaluable/Evaluable.kt | 2 +- .../div/evaluable/internal/Tokenizer.kt | 4 ++-- .../local/SetVariableForLocalVariablesTest.kt | 15 +++++++------- .../string_templates.json | 20 +++++++++++++++++++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/Evaluable.kt b/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/Evaluable.kt index 78aef0e63..4d74f5fb1 100644 --- a/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/Evaluable.kt +++ b/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/Evaluable.kt @@ -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().map { it.name } } override fun evalImpl(evaluator: Evaluator): Any { if (!this::expression.isInitialized) { diff --git a/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/internal/Tokenizer.kt b/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/internal/Tokenizer.kt index defa27e96..3bed970a6 100644 --- a/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/internal/Tokenizer.kt +++ b/client/android/div-evaluable/src/main/java/com/yandex/div/evaluable/internal/Tokenizer.kt @@ -31,7 +31,7 @@ internal object Tokenizer { } val stringTemplateTokens = mutableListOf() - 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() processExpression(state, expressionTokens) - val stringAfterExpression = processString(state) + val stringAfterExpression = processString(state, isLiteral = isPartOfExpression) val isSpecialCaseWithExpressionInStringTemplate = !isPartOfExpression && stringTemplateTokens.isEmpty() diff --git a/client/android/div/src/test/java/com/yandex/div/core/view2/local/SetVariableForLocalVariablesTest.kt b/client/android/div/src/test/java/com/yandex/div/core/view2/local/SetVariableForLocalVariablesTest.kt index df82fd54c..b9882f094 100644 --- a/client/android/div/src/test/java/com/yandex/div/core/view2/local/SetVariableForLocalVariablesTest.kt +++ b/client/android/div/src/test/java/com/yandex/div/core/view2/local/SetVariableForLocalVariablesTest.kt @@ -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) } } } diff --git a/test_data/expression_test_data/string_templates.json b/test_data/expression_test_data/string_templates.json index da25cfcba..2d3bcffb8 100644 --- a/test_data/expression_test_data/string_templates.json +++ b/test_data/expression_test_data/string_templates.json @@ -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" + ] } ] }