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"));
}
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"));
}
Aggregations