Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,13 @@ public void evaluate(
if (left instanceof Term.Bool && right instanceof Closure) {
if (((Term.Bool) left).value()) {
Closure closure = (Closure) right;
Term result = closure.call(variables, temporarySymbolTable, steps);
if (result instanceof Term.Bool) {
stack.push(result);
}
stack.push(closure.call(variables, temporarySymbolTable, steps));
} else {
stack.push(new Term.Bool(false));
}
} else {
throw new Error.Execution(
Error.Execution.Kind.InvalidType, "invalid operands for lazy and op");
}
break;
case LazyOr:
Expand All @@ -556,11 +556,11 @@ public void evaluate(
stack.push(new Term.Bool(true));
} else {
Closure closure = (Closure) right;
Term result = closure.call(variables, temporarySymbolTable, steps);
if (result instanceof Term.Bool) {
stack.push(result);
}
stack.push(closure.call(variables, temporarySymbolTable, steps));
}
} else {
throw new Error.Execution(
Error.Execution.Kind.InvalidType, "invalid operands for lazy or op");
}
break;
case Intersection:
Expand Down Expand Up @@ -735,6 +735,9 @@ public void evaluate(
if (e instanceof Error.TooManySteps) throw e;
stack.push(right);
}
} else {
throw new Error.Execution(
Error.Execution.Kind.InvalidType, "left operand of try_or op is not a closure");
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.eclipse.biscuit.datalog;

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

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -148,4 +149,50 @@ public void testIntersectionAndContains() throws Error {
new TemporarySymbolTable(symbolTable),
new StepCounter(new RunLimits().getMaxSteps())));
}

private static Term evaluate(Op... ops) throws Error {
return new Expression(new ArrayList<>(Arrays.asList(ops)))
.evaluate(
new HashMap<>(),
new TemporarySymbolTable(new SymbolTable()),
new StepCounter(new RunLimits().getMaxSteps()));
}

private static Op.Closure closure(Op... ops) {
return new Op.Closure(new ArrayList<>(), new ArrayList<>(Arrays.asList(ops)));
}

@Test
public void lazyOpsReturnTheClosureResult() throws Error {
assertEquals(
new Term.Integer(1),
evaluate(
new Term.Bool(true), closure(new Term.Integer(1)), new Op.Binary(Op.BinaryOp.LazyAnd)));
assertEquals(
new Term.Integer(1),
evaluate(
new Term.Bool(false), closure(new Term.Integer(1)), new Op.Binary(Op.BinaryOp.LazyOr)));
}

@Test
public void lazyOpsRejectInvalidOperands() {
assertThrows(
Error.Execution.class,
() ->
evaluate(new Term.Bool(true), new Term.Bool(true), new Op.Binary(Op.BinaryOp.LazyAnd)));
assertThrows(
Error.Execution.class,
() ->
evaluate(
new Term.Integer(1),
closure(new Term.Bool(true)),
new Op.Binary(Op.BinaryOp.LazyOr)));
}

@Test
public void tryOrRejectsANonClosureOperand() {
assertThrows(
Error.Execution.class,
() -> evaluate(new Term.Integer(1), new Term.Integer(2), new Op.Binary(Op.BinaryOp.TryOr)));
}
}