Search in sources :

Example 1 with ExpressionParser

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

the class SelectionAndProjectionTests method selectLastItemInMap.

@Test
@SuppressWarnings("unchecked")
void selectLastItemInMap() {
    EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("colors.$[key.startsWith('b')]");
    Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
    assertThat(colorsMap.size()).isEqualTo(1);
    assertThat(colorsMap.keySet().iterator().next()).isEqualTo("brown");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with ExpressionParser

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

the class SelectionAndProjectionTests method selectFirstItemInMap.

@Test
@SuppressWarnings("unchecked")
void selectFirstItemInMap() {
    EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("colors.^[key.startsWith('b')]");
    Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
    assertThat(colorsMap.size()).isEqualTo(1);
    assertThat(colorsMap.keySet().iterator().next()).isEqualTo("beige");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with ExpressionParser

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

the class ComparatorTests method customComparatorWorksWithEquality.

@Test
public void customComparatorWorksWithEquality() {
    final StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setTypeComparator(customComparator);
    ExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("'1' == 1");
    assertThat(expr.getValue(ctx, Boolean.class)).isTrue();
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 4 with ExpressionParser

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

the class MapAccessTests method testGetValueFromRootMap.

@Test
public void testGetValueFromRootMap() {
    Map<String, String> map = new HashMap<>();
    map.put("key", "value");
    ExpressionParser spelExpressionParser = new SpelExpressionParser();
    Expression expr = spelExpressionParser.parseExpression("#root['key']");
    assertThat(expr.getValue(map)).isEqualTo("value");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 5 with ExpressionParser

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

the class MapAccessTests method testVariableMapAccess.

@Test
public void testVariableMapAccess() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
    ctx.setVariable("day", "saturday");
    Expression expr = parser.parseExpression("testMap[#day]");
    Object value = expr.getValue(ctx, String.class);
    assertThat(value).isEqualTo("samstag");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Aggregations

ExpressionParser (cn.taketoday.expression.ExpressionParser)132 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)132 Test (org.junit.jupiter.api.Test)132 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)112 Expression (cn.taketoday.expression.Expression)110 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)72 EvaluationContext (cn.taketoday.expression.EvaluationContext)16 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)10 Map (java.util.Map)10 EvaluationException (cn.taketoday.expression.EvaluationException)6 MethodResolver (cn.taketoday.expression.MethodResolver)6 ReflectiveMethodResolver (cn.taketoday.expression.spel.support.ReflectiveMethodResolver)6 Method (java.lang.reflect.Method)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 TreeMap (java.util.TreeMap)6 MethodParameter (cn.taketoday.core.MethodParameter)4 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4 AccessException (cn.taketoday.expression.AccessException)4