|
20 | 20 | import com.google.api.expr.v1alpha1.Constant; |
21 | 21 | import com.google.api.expr.v1alpha1.Expr; |
22 | 22 | import com.google.api.expr.v1alpha1.Type.PrimitiveType; |
| 23 | +import com.google.common.collect.ImmutableList; |
23 | 24 | import com.google.common.collect.ImmutableMap; |
24 | 25 | import com.google.protobuf.Any; |
25 | 26 | import com.google.protobuf.BoolValue; |
26 | 27 | import com.google.protobuf.ByteString; |
27 | 28 | import com.google.protobuf.DescriptorProtos.FileDescriptorSet; |
28 | 29 | import com.google.protobuf.DynamicMessage; |
29 | 30 | import com.google.rpc.context.AttributeContext; |
| 31 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 32 | +import com.google.testing.junit.testparameterinjector.TestParameters; |
30 | 33 | import dev.cel.bundle.Cel; |
31 | 34 | import dev.cel.bundle.CelFactory; |
32 | 35 | import dev.cel.common.CelAbstractSyntaxTree; |
|
50 | 53 | import java.util.concurrent.atomic.AtomicReference; |
51 | 54 | import org.junit.Test; |
52 | 55 | import org.junit.runner.RunWith; |
53 | | -import org.junit.runners.JUnit4; |
54 | 56 |
|
55 | | -@RunWith(JUnit4.class) |
| 57 | +@RunWith(TestParameterInjector.class) |
56 | 58 | public class CelRuntimeTest { |
57 | 59 |
|
58 | 60 | @Test |
@@ -400,6 +402,141 @@ public void trace_withVariableResolver() throws Exception { |
400 | 402 | assertThat(result).isEqualTo("hello"); |
401 | 403 | } |
402 | 404 |
|
| 405 | + @Test |
| 406 | + public void trace_shortCircuitingDisabled_logicalAndAllBranchesVisited() throws Exception { |
| 407 | + ImmutableList.Builder<Boolean> branchResults = ImmutableList.builder(); |
| 408 | + CelEvaluationListener listener = |
| 409 | + (expr, res) -> { |
| 410 | + if (expr.constantOrDefault().getKind().equals(CelConstant.Kind.BOOLEAN_VALUE)) { |
| 411 | + branchResults.add((Boolean) res); |
| 412 | + } |
| 413 | + }; |
| 414 | + Cel cel = |
| 415 | + CelFactory.standardCelBuilder() |
| 416 | + .setOptions(CelOptions.current().enableShortCircuiting(false).build()) |
| 417 | + .build(); |
| 418 | + CelAbstractSyntaxTree ast = cel.compile("false && true && false").getAst(); |
| 419 | + |
| 420 | + boolean result = (boolean) cel.createProgram(ast).trace(listener); |
| 421 | + |
| 422 | + assertThat(result).isFalse(); |
| 423 | + assertThat(branchResults.build()).containsExactly(false, true, false); |
| 424 | + } |
| 425 | + |
| 426 | + @Test |
| 427 | + public void trace_shortCircuitingDisabled_logicalAndWithUnknowns() throws Exception { |
| 428 | + ImmutableList.Builder<Object> branchResults = ImmutableList.builder(); |
| 429 | + CelEvaluationListener listener = |
| 430 | + (expr, res) -> { |
| 431 | + if (expr.constantOrDefault().getKind().equals(CelConstant.Kind.BOOLEAN_VALUE) |
| 432 | + || expr.identOrDefault().name().equals("x")) { |
| 433 | + branchResults.add(res); |
| 434 | + } |
| 435 | + }; |
| 436 | + Cel cel = |
| 437 | + CelFactory.standardCelBuilder() |
| 438 | + .addVar("x", SimpleType.BOOL) |
| 439 | + .setOptions(CelOptions.current().enableShortCircuiting(false).build()) |
| 440 | + .build(); |
| 441 | + CelAbstractSyntaxTree ast = cel.compile("false && false && x").getAst(); |
| 442 | + |
| 443 | + Object unknownResult = cel.createProgram(ast).trace(listener); |
| 444 | + |
| 445 | + assertThat(InterpreterUtil.isUnknown(unknownResult)).isTrue(); |
| 446 | + assertThat(branchResults.build()).containsExactly(false, false, unknownResult); |
| 447 | + } |
| 448 | + |
| 449 | + @Test |
| 450 | + public void trace_shortCircuitingDisabled_logicalOrAllBranchesVisited() throws Exception { |
| 451 | + ImmutableList.Builder<Boolean> branchResults = ImmutableList.builder(); |
| 452 | + CelEvaluationListener listener = |
| 453 | + (expr, res) -> { |
| 454 | + if (expr.constantOrDefault().getKind().equals(CelConstant.Kind.BOOLEAN_VALUE)) { |
| 455 | + branchResults.add((Boolean) res); |
| 456 | + } |
| 457 | + }; |
| 458 | + Cel cel = |
| 459 | + CelFactory.standardCelBuilder() |
| 460 | + .setOptions(CelOptions.current().enableShortCircuiting(false).build()) |
| 461 | + .build(); |
| 462 | + CelAbstractSyntaxTree ast = cel.compile("true || false || true").getAst(); |
| 463 | + |
| 464 | + boolean result = (boolean) cel.createProgram(ast).trace(listener); |
| 465 | + |
| 466 | + assertThat(result).isTrue(); |
| 467 | + assertThat(branchResults.build()).containsExactly(true, false, true); |
| 468 | + } |
| 469 | + |
| 470 | + @Test |
| 471 | + public void trace_shortCircuitingDisabled_logicalOrWithUnknowns() throws Exception { |
| 472 | + ImmutableList.Builder<Object> branchResults = ImmutableList.builder(); |
| 473 | + CelEvaluationListener listener = |
| 474 | + (expr, res) -> { |
| 475 | + if (expr.constantOrDefault().getKind().equals(CelConstant.Kind.BOOLEAN_VALUE) |
| 476 | + || expr.identOrDefault().name().equals("x")) { |
| 477 | + branchResults.add(res); |
| 478 | + } |
| 479 | + }; |
| 480 | + Cel cel = |
| 481 | + CelFactory.standardCelBuilder() |
| 482 | + .addVar("x", SimpleType.BOOL) |
| 483 | + .setOptions(CelOptions.current().enableShortCircuiting(false).build()) |
| 484 | + .build(); |
| 485 | + CelAbstractSyntaxTree ast = cel.compile("false || false || x").getAst(); |
| 486 | + |
| 487 | + Object unknownResult = cel.createProgram(ast).trace(listener); |
| 488 | + |
| 489 | + assertThat(InterpreterUtil.isUnknown(unknownResult)).isTrue(); |
| 490 | + assertThat(branchResults.build()).containsExactly(false, false, unknownResult); |
| 491 | + } |
| 492 | + |
| 493 | + @Test |
| 494 | + public void trace_shortCircuitingDisabled_ternaryAllBranchesVisited() throws Exception { |
| 495 | + ImmutableList.Builder<Boolean> branchResults = ImmutableList.builder(); |
| 496 | + CelEvaluationListener listener = |
| 497 | + (expr, res) -> { |
| 498 | + if (expr.constantOrDefault().getKind().equals(CelConstant.Kind.BOOLEAN_VALUE)) { |
| 499 | + branchResults.add((Boolean) res); |
| 500 | + } |
| 501 | + }; |
| 502 | + Cel cel = |
| 503 | + CelFactory.standardCelBuilder() |
| 504 | + .setOptions(CelOptions.current().enableShortCircuiting(false).build()) |
| 505 | + .build(); |
| 506 | + CelAbstractSyntaxTree ast = cel.compile("true ? false : true").getAst(); |
| 507 | + |
| 508 | + boolean result = (boolean) cel.createProgram(ast).trace(listener); |
| 509 | + |
| 510 | + assertThat(result).isFalse(); |
| 511 | + assertThat(branchResults.build()).containsExactly(true, false, true); |
| 512 | + } |
| 513 | + |
| 514 | + @Test |
| 515 | + @TestParameters("{source: 'false ? true : x'}") |
| 516 | + @TestParameters("{source: 'true ? x : false'}") |
| 517 | + @TestParameters("{source: 'x ? true : false'}") |
| 518 | + public void trace_shortCircuitingDisabled_ternaryWithUnknowns(String source) throws Exception { |
| 519 | + ImmutableList.Builder<Object> branchResults = ImmutableList.builder(); |
| 520 | + CelEvaluationListener listener = |
| 521 | + (expr, res) -> { |
| 522 | + if (expr.constantOrDefault().getKind().equals(CelConstant.Kind.BOOLEAN_VALUE) |
| 523 | + || expr.identOrDefault().name().equals("x")) { |
| 524 | + branchResults.add(res); |
| 525 | + } |
| 526 | + }; |
| 527 | + Cel cel = |
| 528 | + CelFactory.standardCelBuilder() |
| 529 | + .addVar("x", SimpleType.BOOL) |
| 530 | + .setOptions(CelOptions.current().enableShortCircuiting(false).build()) |
| 531 | + .build(); |
| 532 | + CelAbstractSyntaxTree ast = cel.compile(source).getAst(); |
| 533 | + |
| 534 | + Object unknownResult = cel.createProgram(ast).trace(listener); |
| 535 | + |
| 536 | + assertThat(InterpreterUtil.isUnknown(unknownResult)).isTrue(); |
| 537 | + assertThat(branchResults.build()).containsExactly(false, unknownResult, true); |
| 538 | + } |
| 539 | + |
403 | 540 | @Test |
404 | 541 | public void standardEnvironmentDisabledForRuntime_throws() throws Exception { |
405 | 542 | CelCompiler celCompiler = |
|
0 commit comments