Search in sources :

Example 96 with Expression

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

the class SpelReproTests method SPR10417.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void SPR10417() {
    List list1 = new ArrayList();
    list1.add("a");
    list1.add("b");
    list1.add("x");
    List list2 = new ArrayList();
    list2.add("c");
    list2.add("x");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("list1", list1);
    context.setVariable("list2", list2);
    // #this should be the element from list1
    Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
    Object result = ex.getValue(context);
    assertThat(result.toString()).isEqualTo("[x]");
    // toString() should be called on the element from list1
    ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
    result = ex.getValue(context);
    assertThat(result.toString()).isEqualTo("[x]");
    List list3 = new ArrayList();
    list3.add(1);
    list3.add(2);
    list3.add(3);
    list3.add(4);
    context = new StandardEvaluationContext();
    context.setVariable("list3", list3);
    ex = parser.parseExpression("#list3.?[#this > 2]");
    result = ex.getValue(context);
    assertThat(result.toString()).isEqualTo("[3, 4]");
    ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
    result = ex.getValue(context);
    assertThat(result.toString()).isEqualTo("[1, 2, 3, 4]");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 97 with Expression

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

the class SpelReproTests method SPR9486_addFloatWithFloat.

@Test
void SPR9486_addFloatWithFloat() {
    Number expectedNumber = 10.21f + 10.2f;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("10.21f + 10.2f");
    Number result = expression.getValue(context, null, Number.class);
    assertThat(result).isEqualTo(expectedNumber);
}
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) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 98 with Expression

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

the class SpelReproTests method SPR9486_floatPowerDouble.

@Test
void SPR9486_floatPowerDouble() {
    Number expectedResult = Math.pow(10.21f, 10.2);
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("10.21f ^ 10.2");
    Number result = expression.getValue(context, null, Number.class);
    assertThat(result).isEqualTo(expectedResult);
}
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) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 99 with Expression

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

the class TemplateExpressionParsingTests method testParsingSimpleTemplateExpression04.

@Test
public void testParsingSimpleTemplateExpression04() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    Object o = expr.getValue();
    assertThat(o.toString()).isEqualTo("hello world");
    expr = parser.parseExpression("", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    o = expr.getValue();
    assertThat(o.toString()).isEqualTo("");
    expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    o = expr.getValue();
    assertThat(o.toString()).isEqualTo("abc");
    expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    o = expr.getValue((Object) null);
    assertThat(o.toString()).isEqualTo("abc");
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) Test(org.junit.jupiter.api.Test)

Example 100 with Expression

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

the class TemplateExpressionParsingTests method testParsingNormalExpressionThroughTemplateParser.

@Test
public void testParsingNormalExpressionThroughTemplateParser() throws Exception {
    Expression expr = parser.parseExpression("1+2+3");
    assertThat(expr.getValue()).isEqualTo(6);
}
Also used : Expression(cn.taketoday.expression.Expression) CompositeStringExpression(cn.taketoday.expression.common.CompositeStringExpression) 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