Introduce withAssignmentDisabled() option for SimpleEvaluationContext
To support additional use cases, this commit introduces a withAssignmentDisabled() method in the Builder for SimpleEvaluationContext. Closes gh-33319
This commit is contained in:
@@ -187,6 +187,59 @@ class SimpleEvaluationContextTests {
|
||||
assertIncrementAndDecrementWritesForIndexedStructures(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
void forPropertyAccessorsWithAssignmentDisabled() {
|
||||
SimpleEvaluationContext context = SimpleEvaluationContext
|
||||
.forPropertyAccessors(new CompilableMapAccessor(), DataBindingPropertyAccessor.forReadOnlyAccess())
|
||||
.withAssignmentDisabled()
|
||||
.build();
|
||||
|
||||
assertCommonReadOnlyModeBehavior(context);
|
||||
|
||||
// Map -- with key as property name supported by CompilableMapAccessor
|
||||
|
||||
Expression expression;
|
||||
expression = parser.parseExpression("map.yellow");
|
||||
// setValue() is supported even though assignment is not.
|
||||
expression.setValue(context, model, "pineapple");
|
||||
assertThat(expression.getValue(context, model, String.class)).isEqualTo("pineapple");
|
||||
|
||||
// WRITE -- via assignment operator
|
||||
|
||||
// Variable
|
||||
assertAssignmentDisabled(context, "#myVar = 'rejected'");
|
||||
|
||||
// Property
|
||||
assertAssignmentDisabled(context, "name = 'rejected'");
|
||||
assertAssignmentDisabled(context, "map.yellow = 'rejected'");
|
||||
assertIncrementDisabled(context, "count++");
|
||||
assertIncrementDisabled(context, "++count");
|
||||
assertDecrementDisabled(context, "count--");
|
||||
assertDecrementDisabled(context, "--count");
|
||||
|
||||
// Array Index
|
||||
assertAssignmentDisabled(context, "array[0] = 'rejected'");
|
||||
assertIncrementDisabled(context, "numbers[0]++");
|
||||
assertIncrementDisabled(context, "++numbers[0]");
|
||||
assertDecrementDisabled(context, "numbers[0]--");
|
||||
assertDecrementDisabled(context, "--numbers[0]");
|
||||
|
||||
// List Index
|
||||
assertAssignmentDisabled(context, "list[0] = 'rejected'");
|
||||
|
||||
// Map Index -- key as String
|
||||
assertAssignmentDisabled(context, "map['red'] = 'rejected'");
|
||||
|
||||
// Map Index -- key as pseudo property name
|
||||
assertAssignmentDisabled(context, "map[yellow] = 'rejected'");
|
||||
|
||||
// String Index
|
||||
assertAssignmentDisabled(context, "name[0] = 'rejected'");
|
||||
|
||||
// Object Index
|
||||
assertAssignmentDisabled(context, "['name'] = 'rejected'");
|
||||
}
|
||||
|
||||
|
||||
private void assertReadWriteMode(SimpleEvaluationContext context) {
|
||||
// Variables can always be set programmatically within an EvaluationContext.
|
||||
|
||||
Reference in New Issue
Block a user