Search in sources :

Example 16 with Expression

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

the class PropertyAccessTests method propertyAccessWithArrayIndexOutOfBounds.

@Test
void propertyAccessWithArrayIndexOutOfBounds() {
    EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
    Expression expression = parser.parseExpression("stringArrayOfThreeItems[3]");
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expression.getValue(context, new Inventor())).extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS);
}
Also used : Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) SimpleEvaluationContext(cn.taketoday.expression.spel.support.SimpleEvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Inventor(cn.taketoday.expression.spel.testresources.Inventor) Test(org.junit.jupiter.api.Test)

Example 17 with Expression

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

the class PropertyAccessTests method propertyReadOnlyWithRecordStyle.

@Test
void propertyReadOnlyWithRecordStyle() {
    EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
    Expression expr = parser.parseExpression("name");
    RecordPerson target1 = new RecordPerson("p1");
    assertThat(expr.getValue(context, target1)).isEqualTo("p1");
    RecordPerson target2 = new RecordPerson("p2");
    assertThat(expr.getValue(context, target2)).isEqualTo("p2");
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target2));
}
Also used : RecordPerson(cn.taketoday.expression.spel.testresources.RecordPerson) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) SimpleEvaluationContext(cn.taketoday.expression.spel.support.SimpleEvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 18 with Expression

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

the class PropertyAccessTests method shouldAlwaysUsePropertyAccessorFromEvaluationContext.

@Test
void shouldAlwaysUsePropertyAccessorFromEvaluationContext() {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expression = parser.parseExpression("name");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Ollie")));
    assertThat(expression.getValue(context)).isEqualTo("Ollie");
    context = new StandardEvaluationContext();
    context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Jens")));
    assertThat(expression.getValue(context)).isEqualTo("Jens");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 19 with Expression

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

the class SelectionAndProjectionTests method selectLastItemInArray.

@Test
void selectLastItemInArray() 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(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) Test(org.junit.jupiter.api.Test)

Example 20 with Expression

use of cn.taketoday.expression.Expression 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)

Aggregations

Expression (cn.taketoday.expression.Expression)418 Test (org.junit.jupiter.api.Test)394 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)302 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)268 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)206 ExpressionParser (cn.taketoday.expression.ExpressionParser)112 EvaluationContext (cn.taketoday.expression.EvaluationContext)70 ArrayList (java.util.ArrayList)58 HashMap (java.util.HashMap)38 CompoundExpression (cn.taketoday.expression.spel.ast.CompoundExpression)36 EvaluationException (cn.taketoday.expression.EvaluationException)28 List (java.util.List)24 Map (java.util.Map)20 CompositeStringExpression (cn.taketoday.expression.common.CompositeStringExpression)16 LinkedHashMap (java.util.LinkedHashMap)14 TypedValue (cn.taketoday.expression.TypedValue)12 SimpleEvaluationContext (cn.taketoday.expression.spel.support.SimpleEvaluationContext)10 Method (java.lang.reflect.Method)10 AccessException (cn.taketoday.expression.AccessException)8 MethodResolver (cn.taketoday.expression.MethodResolver)8