Skip to content

Commit

Permalink
Polish SpelParserTests and TemplateExpressionParsingTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Apr 25, 2023
1 parent ec5f764 commit 5afd94f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,10 +34,11 @@
/**
* @author Andy Clement
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class TemplateExpressionParsingTests extends AbstractExpressionTests {
class TemplateExpressionParsingTests extends AbstractExpressionTests {

public static final ParserContext DEFAULT_TEMPLATE_PARSER_CONTEXT = new ParserContext() {
static final ParserContext DEFAULT_TEMPLATE_PARSER_CONTEXT = new ParserContext() {
@Override
public String getExpressionPrefix() {
return "${";
Expand All @@ -52,7 +53,7 @@ public boolean isTemplate() {
}
};

public static final ParserContext HASH_DELIMITED_PARSER_CONTEXT = new ParserContext() {
static final ParserContext HASH_DELIMITED_PARSER_CONTEXT = new ParserContext() {
@Override
public String getExpressionPrefix() {
return "#{";
Expand All @@ -68,34 +69,33 @@ public boolean isTemplate() {
};


private final SpelExpressionParser parser = new SpelExpressionParser();


@Test
public void testParsingSimpleTemplateExpression01() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
void parsingSimpleTemplateExpression01() {
Expression expr = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
Object o = expr.getValue();
assertThat(o.toString()).isEqualTo("hello world");
}

@Test
public void testParsingSimpleTemplateExpression02() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
void parsingSimpleTemplateExpression02() {
Expression expr = parser.parseExpression("hello ${'to'} you", DEFAULT_TEMPLATE_PARSER_CONTEXT);
Object o = expr.getValue();
assertThat(o.toString()).isEqualTo("hello to you");
}

@Test
public void testParsingSimpleTemplateExpression03() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
void parsingSimpleTemplateExpression03() {
Expression expr = parser.parseExpression("The quick ${'brown'} fox jumped over the ${'lazy'} dog",
DEFAULT_TEMPLATE_PARSER_CONTEXT);
Object o = expr.getValue();
assertThat(o.toString()).isEqualTo("The quick brown fox jumped over the lazy dog");
}

@Test
public void testParsingSimpleTemplateExpression04() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
void parsingSimpleTemplateExpression04() {
Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
Object o = expr.getValue();
assertThat(o.toString()).isEqualTo("hello world");
Expand All @@ -114,8 +114,7 @@ public void testParsingSimpleTemplateExpression04() throws Exception {
}

@Test
public void testCompositeStringExpression() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
void compositeStringExpression() {
Expression ex = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
assertThat(ex.getValue()).isInstanceOf(String.class).isEqualTo("hello world");
assertThat(ex.getValue(String.class)).isInstanceOf(String.class).isEqualTo("hello world");
Expand Down Expand Up @@ -154,8 +153,7 @@ public void testCompositeStringExpression() throws Exception {
static class Rooty {}

@Test
public void testNestedExpressions() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
void nestedExpressions() {
// 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);
Expand Down Expand Up @@ -185,8 +183,7 @@ public void testNestedExpressions() throws Exception {
}

@Test

public void testClashingWithSuffixes() throws Exception {
void clashingWithSuffixes() {
// Just wanting to use the prefix or suffix within the template:
Expression ex = parser.parseExpression("hello ${3+4} world",DEFAULT_TEMPLATE_PARSER_CONTEXT);
String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class);
Expand All @@ -202,13 +199,13 @@ public void testClashingWithSuffixes() throws Exception {
}

@Test
public void testParsingNormalExpressionThroughTemplateParser() throws Exception {
void parsingNormalExpressionThroughTemplateParser() {
Expression expr = parser.parseExpression("1+2+3");
assertThat(expr.getValue()).isEqualTo(6);
}

@Test
public void testErrorCases() throws Exception {
void errorCases() {
assertThatExceptionOfType(ParseException.class).isThrownBy(() ->
parser.parseExpression("hello ${'world'", DEFAULT_TEMPLATE_PARSER_CONTEXT))
.satisfies(pex -> {
Expand All @@ -224,7 +221,7 @@ public void testErrorCases() throws Exception {
}

@Test
public void testTemplateParserContext() {
void testTemplateParserContext() {
TemplateParserContext tpc = new TemplateParserContext("abc","def");
assertThat(tpc.getExpressionPrefix()).isEqualTo("abc");
assertThat(tpc.getExpressionSuffix()).isEqualTo("def");
Expand Down
Loading

0 comments on commit 5afd94f

Please sign in to comment.