Search in sources :

Example 1 with Person

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

the class PropertyAccessTests method propertyAccessWithInstanceMethodResolverAndTypedRootObject.

@Test
void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
    Person rootObject = new Person("p1");
    EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().withTypedRootObject(rootObject, TypeDescriptor.valueOf(Object.class)).build();
    assertThat(parser.parseExpression("name.substring(1)").getValue(context)).isEqualTo("1");
    assertThat(context.getRootObject().getValue()).isSameAs(rootObject);
    assertThat(context.getRootObject().getTypeDescriptor().getType()).isSameAs(Object.class);
}
Also used : SimpleEvaluationContext(cn.taketoday.expression.spel.support.SimpleEvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Person(cn.taketoday.expression.spel.testresources.Person) RecordPerson(cn.taketoday.expression.spel.testresources.RecordPerson) Test(org.junit.jupiter.api.Test)

Example 2 with Person

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

the class PropertyAccessTests method propertyAccessWithInstanceMethodResolverAndTypedRootObject.

@Test
void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
    Person rootObject = new Person("p1");
    EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().withTypedRootObject(rootObject, TypeDescriptor.valueOf(Object.class)).build();
    assertThat(parser.parseExpression("name.substring(1)").getValue(context)).isEqualTo("1");
    assertThat(context.getRootObject().getValue()).isSameAs(rootObject);
    assertThat(context.getRootObject().getTypeDescriptor().getType()).isSameAs(Object.class);
}
Also used : SimpleEvaluationContext(cn.taketoday.expression.spel.support.SimpleEvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Person(cn.taketoday.expression.spel.testresources.Person) RecordPerson(cn.taketoday.expression.spel.testresources.RecordPerson) Test(org.junit.jupiter.api.Test)

Example 3 with Person

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

the class PropertyAccessTests method propertyAccessWithInstanceMethodResolver.

@Test
void propertyAccessWithInstanceMethodResolver() {
    EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build();
    Person target = new Person("p1");
    assertThat(parser.parseExpression("name.substring(1)").getValue(context, target)).isEqualTo("1");
}
Also used : SimpleEvaluationContext(cn.taketoday.expression.spel.support.SimpleEvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Person(cn.taketoday.expression.spel.testresources.Person) RecordPerson(cn.taketoday.expression.spel.testresources.RecordPerson) Test(org.junit.jupiter.api.Test)

Example 4 with Person

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

the class PropertyAccessTests method propertyReadOnly.

@Test
void propertyReadOnly() {
    EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
    Expression expr = parser.parseExpression("name");
    Person target = new Person("p1");
    assertThat(expr.getValue(context, target)).isEqualTo("p1");
    target.setName("p2");
    assertThat(expr.getValue(context, target)).isEqualTo("p2");
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target));
}
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) Person(cn.taketoday.expression.spel.testresources.Person) RecordPerson(cn.taketoday.expression.spel.testresources.RecordPerson) Test(org.junit.jupiter.api.Test)

Example 5 with Person

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

the class PropertyAccessTests method propertyReadWrite.

@Test
void propertyReadWrite() {
    EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
    Expression expr = parser.parseExpression("name");
    Person target = new Person("p1");
    assertThat(expr.getValue(context, target)).isEqualTo("p1");
    target.setName("p2");
    assertThat(expr.getValue(context, target)).isEqualTo("p2");
    parser.parseExpression("name='p3'").getValue(context, target);
    assertThat(target.getName()).isEqualTo("p3");
    assertThat(expr.getValue(context, target)).isEqualTo("p3");
    expr.setValue(context, target, "p4");
    assertThat(target.getName()).isEqualTo("p4");
    assertThat(expr.getValue(context, target)).isEqualTo("p4");
}
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) Person(cn.taketoday.expression.spel.testresources.Person) RecordPerson(cn.taketoday.expression.spel.testresources.RecordPerson) Test(org.junit.jupiter.api.Test)

Aggregations

EvaluationContext (cn.taketoday.expression.EvaluationContext)12 SimpleEvaluationContext (cn.taketoday.expression.spel.support.SimpleEvaluationContext)12 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)12 Person (cn.taketoday.expression.spel.testresources.Person)12 RecordPerson (cn.taketoday.expression.spel.testresources.RecordPerson)12 Test (org.junit.jupiter.api.Test)12 Expression (cn.taketoday.expression.Expression)6 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)6