Search in sources :

Example 26 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-framework by TAKETODAY.

the class OpPlusTests method test_binaryPlusWithRightStringOperand.

@Test
public void test_binaryPlusWithRightStringOperand() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    LongLiteral n1 = new LongLiteral("123", -1, -1, 123);
    StringLiteral n2 = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\"");
    OpPlus o = new OpPlus(-1, -1, n1, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
    assertThat(value.getValue()).isEqualTo("123 is a number");
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 27 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-framework by TAKETODAY.

the class OpPlusTests method test_binaryPlusWithTimeConverted.

@Test
public void test_binaryPlusWithTimeConverted() {
    SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
    GenericConversionService conversionService = new GenericConversionService();
    conversionService.addConverter(Time.class, String.class, format::format);
    StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
    evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
    ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
    Time time = new Time(new Date().getTime());
    VariableReference var = new VariableReference("timeVar", -1, -1);
    var.setValue(expressionState, time);
    StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
    OpPlus o = new OpPlus(-1, -1, var, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
    assertThat(value.getValue()).isEqualTo((format.format(time) + " is now"));
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) ExpressionState(cn.taketoday.expression.spel.ExpressionState) Time(java.sql.Time) SimpleDateFormat(java.text.SimpleDateFormat) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Date(java.util.Date) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 28 with ExpressionState

use of cn.taketoday.expression.spel.ExpressionState in project today-framework by TAKETODAY.

the class OpPlusTests method test_binaryPlusWithLeftStringOperand.

@Test
public void test_binaryPlusWithLeftStringOperand() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    StringLiteral n1 = new StringLiteral("\"number is \"", -1, -1, "\"number is \"");
    LongLiteral n2 = new LongLiteral("123", -1, -1, 123);
    OpPlus o = new OpPlus(-1, -1, n1, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
    assertThat(value.getValue()).isEqualTo("number is 123");
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 29 with ExpressionState

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

the class OpPlusTests method test_binaryPlusWithRightStringOperand.

@Test
public void test_binaryPlusWithRightStringOperand() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    LongLiteral n1 = new LongLiteral("123", -1, -1, 123);
    StringLiteral n2 = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\"");
    OpPlus o = new OpPlus(-1, -1, n1, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
    assertThat(value.getValue()).isEqualTo("123 is a number");
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 30 with ExpressionState

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

the class OpPlusTests method test_binaryPlusWithTime_ToString.

@Test
public void test_binaryPlusWithTime_ToString() {
    ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
    Time time = new Time(new Date().getTime());
    VariableReference var = new VariableReference("timeVar", -1, -1);
    var.setValue(expressionState, time);
    StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\"");
    OpPlus o = new OpPlus(-1, -1, var, n2);
    TypedValue value = o.getValueInternal(expressionState);
    assertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.class);
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(String.class);
    assertThat(value.getValue()).isEqualTo((time + " is now"));
}
Also used : ExpressionState(cn.taketoday.expression.spel.ExpressionState) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Time(java.sql.Time) Date(java.util.Date) TypedValue(cn.taketoday.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Aggregations

ExpressionState (cn.taketoday.expression.spel.ExpressionState)38 Nullable (cn.taketoday.lang.Nullable)24 TypedValue (cn.taketoday.expression.TypedValue)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)18 CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)16 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)16 Test (org.junit.jupiter.api.Test)12 TypeDescriptor (cn.taketoday.core.TypeDescriptor)4 EvaluationContext (cn.taketoday.expression.EvaluationContext)4 Time (java.sql.Time)4 Date (java.util.Date)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 StandardTypeConverter (cn.taketoday.expression.spel.support.StandardTypeConverter)2 SimpleDateFormat (java.text.SimpleDateFormat)2