Search in sources :

Example 6 with EvaluationContext

use of cn.taketoday.expression.EvaluationContext 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 7 with EvaluationContext

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

the class SelectionAndProjectionTests method selectFirstItemInArray.

@Test
void selectFirstItemInArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
    Object value = expression.getValue(context);
    assertThat(value).isInstanceOf(Integer.class);
    assertThat(value).isEqualTo(0);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 8 with EvaluationContext

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

the class SelectionAndProjectionTests method projectionWithSet.

@Test
@SuppressWarnings("unchecked")
void projectionWithSet() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testList", IntegerTestBean.createSet());
    Object value = expression.getValue(context);
    assertThat(value).isInstanceOf(List.class);
    List<Integer> list = (List<Integer>) value;
    assertThat(list).containsExactly(5, 6, 7);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 9 with EvaluationContext

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

the class SelectionAndProjectionTests method projectionWithList.

@Test
@SuppressWarnings("unchecked")
void projectionWithList() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testList", IntegerTestBean.createList());
    Object value = expression.getValue(context);
    assertThat(value).isInstanceOf(List.class);
    List<Integer> list = (List<Integer>) value;
    assertThat(list).containsExactly(5, 6, 7);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 10 with EvaluationContext

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

the class SelectionAndProjectionTests method selectionWithArray.

@Test
void selectionWithArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
    Object value = expression.getValue(context);
    assertThat(value.getClass().isArray()).isTrue();
    TypedValue typedValue = new TypedValue(value);
    assertThat(typedValue.getTypeDescriptor().getElementDescriptor().getType()).isEqualTo(Integer.class);
    Integer[] array = (Integer[]) value;
    assertThat(array).containsExactly(0, 1, 2, 3, 4);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Aggregations

EvaluationContext (cn.taketoday.expression.EvaluationContext)126 Test (org.junit.jupiter.api.Test)120 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)110 Expression (cn.taketoday.expression.Expression)68 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)62 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)28 ArrayList (java.util.ArrayList)24 SimpleEvaluationContext (cn.taketoday.expression.spel.support.SimpleEvaluationContext)20 List (java.util.List)18 ExpressionParser (cn.taketoday.expression.ExpressionParser)16 TypedValue (cn.taketoday.expression.TypedValue)14 RecordPerson (cn.taketoday.expression.spel.testresources.RecordPerson)14 Map (java.util.Map)12 Person (cn.taketoday.expression.spel.testresources.Person)11 TypeDescriptor (cn.taketoday.core.TypeDescriptor)10 MethodExecutor (cn.taketoday.expression.MethodExecutor)10 ReflectiveMethodResolver (cn.taketoday.expression.spel.support.ReflectiveMethodResolver)10 Method (java.lang.reflect.Method)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 EvaluationException (cn.taketoday.expression.EvaluationException)6