Search in sources :

Example 1 with CompositeStringExpression

use of cn.taketoday.expression.common.CompositeStringExpression in project today-infrastructure by TAKETODAY.

the class TemplateExpressionParsingTests method testNestedExpressions.

@Test
public void testNestedExpressions() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    // treat the nested ${..} as a part of the expression
    Expression ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertThat(s).isEqualTo("hello 4 world");
    // not a useful expression but tests nested expression syntax that clashes with template prefix/suffix
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    assertThat(ex.getClass()).isEqualTo(CompositeStringExpression.class);
    CompositeStringExpression cse = (CompositeStringExpression) ex;
    Expression[] exprs = cse.getExpressions();
    assertThat(exprs.length).isEqualTo(3);
    assertThat(exprs[1].getExpressionString()).isEqualTo("listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]");
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertThat(s).isEqualTo("hello  world");
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertThat(s).isEqualTo("hello 4 10 world");
    assertThatExceptionOfType(ParseException.class).isThrownBy(() -> parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world", DEFAULT_TEMPLATE_PARSER_CONTEXT)).satisfies(pex -> assertThat(pex.getSimpleMessage()).isEqualTo("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world"));
    assertThatExceptionOfType(ParseException.class).isThrownBy(() -> parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT)).satisfies(pex -> assertThat(pex.getSimpleMessage()).isEqualTo("Found closing '}' at position 74 but most recent opening is '[' at position 30"));
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) Test(org.junit.jupiter.api.Test)

Example 2 with CompositeStringExpression

use of cn.taketoday.expression.common.CompositeStringExpression in project today-framework by TAKETODAY.

the class TemplateExpressionParsingTests method testNestedExpressions.

@Test
public void testNestedExpressions() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    // treat the nested ${..} as a part of the expression
    Expression ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertThat(s).isEqualTo("hello 4 world");
    // not a useful expression but tests nested expression syntax that clashes with template prefix/suffix
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    assertThat(ex.getClass()).isEqualTo(CompositeStringExpression.class);
    CompositeStringExpression cse = (CompositeStringExpression) ex;
    Expression[] exprs = cse.getExpressions();
    assertThat(exprs.length).isEqualTo(3);
    assertThat(exprs[1].getExpressionString()).isEqualTo("listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]");
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertThat(s).isEqualTo("hello  world");
    ex = parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(), String.class);
    assertThat(s).isEqualTo("hello 4 10 world");
    assertThatExceptionOfType(ParseException.class).isThrownBy(() -> parser.parseExpression("hello ${listOfNumbersUpToTen.$[#this<5]} ${listOfNumbersUpToTen.$[#this>5] world", DEFAULT_TEMPLATE_PARSER_CONTEXT)).satisfies(pex -> assertThat(pex.getSimpleMessage()).isEqualTo("No ending suffix '}' for expression starting at character 41: ${listOfNumbersUpToTen.$[#this>5] world"));
    assertThatExceptionOfType(ParseException.class).isThrownBy(() -> parser.parseExpression("hello ${listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT)).satisfies(pex -> assertThat(pex.getSimpleMessage()).isEqualTo("Found closing '}' at position 74 but most recent opening is '[' at position 30"));
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) Test(org.junit.jupiter.api.Test)

Aggregations

Expression (cn.taketoday.expression.Expression)2 CompositeStringExpression (cn.taketoday.expression.common.CompositeStringExpression)2 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)2 Test (org.junit.jupiter.api.Test)2