Search in sources :

Example 1 with TypedValue

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

the class SelectionAndProjectionTests method selectionWithPrimitiveArray.

@Test
void selectionWithPrimitiveArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("ints.?[#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)

Example 2 with TypedValue

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

Example 3 with TypedValue

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

the class ExpressionStateTests method testActiveContextObject.

@Test
public void testActiveContextObject() {
    ExpressionState state = getState();
    assertThat(state.getActiveContextObject().getValue()).isEqualTo(state.getRootContextObject().getValue());
    assertThatIllegalStateException().isThrownBy(state::popActiveContextObject);
    state.pushActiveContextObject(new TypedValue(34));
    assertThat(state.getActiveContextObject().getValue()).isEqualTo(34);
    state.pushActiveContextObject(new TypedValue("hello"));
    assertThat(state.getActiveContextObject().getValue()).isEqualTo("hello");
    state.popActiveContextObject();
    assertThat(state.getActiveContextObject().getValue()).isEqualTo(34);
    state.popActiveContextObject();
    assertThat(state.getActiveContextObject().getValue()).isEqualTo(state.getRootContextObject().getValue());
    state = new ExpressionState(new StandardEvaluationContext());
    assertThat(state.getActiveContextObject()).isEqualTo(TypedValue.NULL);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 4 with TypedValue

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

the class ExpressionStateTests method testVariables.

@Test
public void testVariables() {
    ExpressionState state = getState();
    TypedValue typedValue = state.lookupVariable("foo");
    assertThat(typedValue).isEqualTo(TypedValue.NULL);
    state.setVariable("foo", 34);
    typedValue = state.lookupVariable("foo");
    assertThat(typedValue.getValue()).isEqualTo(34);
    assertThat(typedValue.getTypeDescriptor().getType()).isEqualTo(Integer.class);
    state.setVariable("foo", "abc");
    typedValue = state.lookupVariable("foo");
    assertThat(typedValue.getValue()).isEqualTo("abc");
    assertThat(typedValue.getTypeDescriptor().getType()).isEqualTo(String.class);
}
Also used : TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 5 with TypedValue

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

the class ExpressionStateTests method testTypeConversion.

@Test
public void testTypeConversion() throws EvaluationException {
    ExpressionState state = getState();
    String s = (String) state.convertValue(34, TypeDescriptor.valueOf(String.class));
    assertThat(s).isEqualTo("34");
    s = (String) state.convertValue(new TypedValue(34), TypeDescriptor.valueOf(String.class));
    assertThat(s).isEqualTo("34");
}
Also used : TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Aggregations

TypedValue (cn.taketoday.expression.TypedValue)64 Test (org.junit.jupiter.api.Test)34 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)28 ExpressionState (cn.taketoday.expression.spel.ExpressionState)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)14 EvaluationContext (cn.taketoday.expression.EvaluationContext)12 Expression (cn.taketoday.expression.Expression)8 CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)8 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)8 Nullable (cn.taketoday.lang.Nullable)8 MethodParameter (cn.taketoday.core.MethodParameter)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 AccessException (cn.taketoday.expression.AccessException)6 EvaluationException (cn.taketoday.expression.EvaluationException)4 ReflectivePropertyAccessor (cn.taketoday.expression.spel.support.ReflectivePropertyAccessor)4 Time (java.sql.Time)4 Date (java.util.Date)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 ExpressionException (cn.taketoday.expression.ExpressionException)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2