Skip to content

Commit

Permalink
ExpressionEvaluationContext.enterScope was context
Browse files Browse the repository at this point in the history
  • Loading branch information
mP1 committed Jan 12, 2025
1 parent e220997 commit ff81a9d
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Object evaluateExpression(final Expression expression) {
// functions........................................................................................................

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> resolver) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> resolver) {
return ExpressionEvaluationContexts.scoped(
resolver,
this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public boolean isPure(final ExpressionFunctionName name) {
}

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> resolver) {
return this.context.context(resolver);
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> resolver) {
return this.context.enterScope(resolver);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface ExpressionEvaluationContext extends Context,
/**
* Factory that returns a {@link ExpressionEvaluationContext} of the same type with the given scoped variables.
*/
ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped);
ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped);

/**
* If the value is a reference or expression resolve or evaluate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/**
* Delegates all {@link ExpressionEvaluationContext} except for {@link #reference(ExpressionReference)} and
* {@link #context(Function)}.
* {@link #addReferences(Function)}.
*/
public interface ExpressionEvaluationContextDelegator extends ExpressionEvaluationContext,
ConverterContextDelegator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
Expand All @@ -38,6 +39,31 @@ public interface ExpressionEvaluationContextTesting<C extends ExpressionEvaluati
ExpressionPurityContextTesting<C>,
TreePrintableTesting {

// enterScope.......................................................................................................

@Test
default void testEnterScopeWithNullFails() {
assertThrows(
NullPointerException.class,
() -> this.createContext()
.enterScope(null)
);
}

@Test
default void testEnterScopeGivesDifferentInstance() {
final C context = this.createContext();

assertNotSame(
context,
context.enterScope(
(n) -> {
throw new UnsupportedOperationException();
}
)
);
}

// evaluateExpression...............................................................................................

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CaseSensitivity stringEqualsCaseSensitivity() {
}

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private ScopedExpressionEvaluationContext(final Function<ExpressionReference, Op
// ExpressionEvaluationContext......................................................................................

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
return ScopedExpressionEvaluationContext.with(
scoped,
this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public T apply(final List<Object> values,
this.checkParameterCount(values);

return context.convertOrFail(
context.context(
context.enterScope(
ExpressionFunctionLambdaExpressionEvaluationContextFunction.with(
this.parameters,
values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public N node() {
// namedFunction.........................................................................................................

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
Objects.requireNonNull(scoped, "scoped");

throw new UnsupportedOperationException("Scoped variables not supported");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,13 @@ public void testReferenceNotFound() {
);
}

// context..........................................................................................................
// enterScope.......................................................................................................

@Test
public void testContextWithGlobalReference() {
public void testEnterScopeWithGlobalReference() {
this.referenceAndCheck(
this.createContext()
.context(
.enterScope(
r -> Optional.empty() // no locals
),
REFERENCE,
Expand All @@ -518,13 +518,13 @@ public void testContextWithGlobalReference() {
}

@Test
public void testContextWithLocalReference() {
public void testEnterScopeWithLocalReference() {
final ExpressionReference reference = new FakeExpressionReference();
final String value = "*reference value*";

this.referenceAndCheck(
this.createContext()
.context(
.enterScope(
r -> r.equals(reference) ?
Optional.of(
Optional.of(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void toExpressionNumberLambdaExpressionNumber() {
new FakeExpressionEvaluationContext() {

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
return new FakeExpressionEvaluationContext() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -355,6 +356,11 @@ public <T> Either<T, String> convert(final Object value, final Class<T> target)
);
}

@Override
public void testEnterScopeGivesDifferentInstance() {
throw new UnsupportedOperationException();
}

@Override
public CycleDetectingExpressionEvaluationContext createContext() {
return this.createContext(true);
Expand Down Expand Up @@ -388,6 +394,13 @@ public boolean isPure(final ExpressionFunctionName name) {
return pure;
}

@Override
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
Objects.requireNonNull(scoped, "scoped");

throw new UnsupportedOperationException();
}

// DecimalNumberContext............................................................................

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public void testEvaluateExpressionUnknownFunctionNameFails() {
throw new UnsupportedOperationException();
}

@Override
public void testEnterScopeGivesDifferentInstance() {
throw new UnsupportedOperationException();
}

@Override
public TestExpressionEvaluationContextDelegator createContext() {
return new TestExpressionEvaluationContextDelegator();
Expand Down Expand Up @@ -125,7 +130,9 @@ public Object evaluateFunction(final ExpressionFunction<?, ? extends ExpressionE
}

@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
Objects.requireNonNull(scoped, "scoped");

throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private void lambdaFunctionFails(final List<ExpressionFunctionParameter<?>> para
public void testLambdaFunction() {
final FakeExpressionEvaluationContext context = new FakeExpressionEvaluationContext() {
@Override
public ExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public ExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
return new FakeExpressionEvaluationContext() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void testReferenceWithDoubleLocalReference() {

this.referenceAndCheck(
this.createContext()
.context(r ->
.enterScope(r ->
Optional.of(
Optional.ofNullable(
r.equals(reference) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public FakeExpressionEvaluationContext createContext() {
return new FakeExpressionEvaluationContext() {

@Override
public FakeExpressionEvaluationContext context(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
public FakeExpressionEvaluationContext enterScope(final Function<ExpressionReference, Optional<Optional<Object>>> scoped) {
return new FakeExpressionEvaluationContext() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public void testReference() {
);
}

@Override
public void testEnterScopeGivesDifferentInstance() {
throw new UnsupportedOperationException();
}

@Override
public BasicNodeSelectorExpressionEvaluationContext<TestNode, StringName, StringName, Object> createContext() {
return this.createContext(TestNode.with("test-node-123"));
Expand Down

0 comments on commit ff81a9d

Please sign in to comment.