From 6d460048da3832efa0d84966966a286abc3fe266 Mon Sep 17 00:00:00 2001 From: Sean Huh Date: Fri, 2 Oct 2026 12:43:47 -0700 Subject: [PATCH] Migrate internal tests to use planner PiperOrigin-RevId: 992481438 --- .../src/test/java/dev/cel/bundle/BUILD.bazel | 9 +- .../bundle/CelEnvironmentExporterTest.java | 20 +- .../dev/cel/bundle/CelEnvironmentTest.java | 29 +- .../bundle/CelEnvironmentYamlParserTest.java | 77 ++-- .../java/dev/cel/bundle/CelFactoryTest.java | 49 --- .../test/java/dev/cel/bundle/CelImplTest.java | 378 ++++++++++-------- .../compiler/tools/CelCompilerToolTest.java | 4 +- .../policy/PolicyConformanceTest.java | 2 +- .../dev/cel/maven/RuntimeArtifactTest.java | 7 +- .../dev/cel/extensions/CelExtensionsTest.java | 21 +- .../cel/extensions/CelMathExtensionsTest.java | 116 +----- .../cel/extensions/CelSetsExtensionsTest.java | 4 +- .../extensions/CelStringExtensionsTest.java | 4 +- .../test/java/dev/cel/optimizer/BUILD.bazel | 8 +- .../optimizer/CelOptimizerFactoryTest.java | 61 --- .../cel/optimizer/CelOptimizerImplTest.java | 22 +- .../optimizers/InliningOptimizerTest.java | 8 +- .../SubexpressionOptimizerTest.java | 52 +-- .../src/test/java/dev/cel/parser/BUILD.bazel | 1 - .../dev/cel/parser/CelParserFactoryTest.java | 30 -- .../src/test/java/dev/cel/policy/BUILD.bazel | 2 - .../policy/CelPolicyCompilerFactoryTest.java | 47 --- .../src/test/java/dev/cel/runtime/BUILD.bazel | 4 +- .../CelLiteRuntimeVersionSkewTest.java | 4 +- .../cel/runtime/CelRuntimeFactoryTest.java | 30 -- .../cel/runtime/CelRuntimeLegacyImplTest.java | 88 +++- .../java/dev/cel/runtime/CelRuntimeTest.java | 104 +---- .../cel/runtime/CelStandardFunctionsTest.java | 11 +- .../runtime/DescriptorTypeResolverTest.java | 41 +- .../async/CelAsyncRuntimeImplTest.java | 21 +- .../planner/OptimizedSelectPlannerTest.java | 5 +- .../testing/testrunner/CelTestContext.java | 4 +- .../testing/testrunner/TestRunnerLibrary.java | 2 +- .../dev/cel/testing/testrunner/BUILD.bazel | 1 + .../testrunner/CelCoverageIndexTest.java | 10 +- .../testing/testrunner/ContextPbUserTest.java | 2 +- .../cel/testing/testrunner/CoverageTest.java | 2 +- .../CustomVariableBindingUserTest.java | 2 +- .../testrunner/DefaultResultMatcherTest.java | 8 +- .../testing/testrunner/EnvConfigUserTest.java | 2 +- .../LateFunctionBindingUserTest.java | 2 + .../testrunner/TestRunnerLibraryTest.java | 10 +- .../dev/cel/testing/testrunner/UserTest.java | 2 +- .../test/java/dev/cel/validator/BUILD.bazel | 7 +- .../validator/CelValidatorFactoryTest.java | 61 --- .../cel/validator/CelValidatorImplTest.java | 16 +- .../AstDepthLimitValidatorTest.java | 2 +- .../dev/cel/validator/validators/BUILD.bazel | 3 +- ...omprehensionNestingLimitValidatorTest.java | 2 +- .../DurationLiteralValidatorTest.java | 16 +- .../HomogeneousLiteralValidatorTest.java | 10 +- .../validators/RegexLiteralValidatorTest.java | 13 +- .../TimestampLiteralValidatorTest.java | 23 +- 53 files changed, 565 insertions(+), 894 deletions(-) delete mode 100644 bundle/src/test/java/dev/cel/bundle/CelFactoryTest.java delete mode 100644 optimizer/src/test/java/dev/cel/optimizer/CelOptimizerFactoryTest.java delete mode 100644 parser/src/test/java/dev/cel/parser/CelParserFactoryTest.java delete mode 100644 policy/src/test/java/dev/cel/policy/CelPolicyCompilerFactoryTest.java delete mode 100644 runtime/src/test/java/dev/cel/runtime/CelRuntimeFactoryTest.java delete mode 100644 validator/src/test/java/dev/cel/validator/CelValidatorFactoryTest.java diff --git a/bundle/src/test/java/dev/cel/bundle/BUILD.bazel b/bundle/src/test/java/dev/cel/bundle/BUILD.bazel index ddd2e7285..4f9e51c62 100644 --- a/bundle/src/test/java/dev/cel/bundle/BUILD.bazel +++ b/bundle/src/test/java/dev/cel/bundle/BUILD.bazel @@ -25,13 +25,18 @@ java_library( "//bundle:environment_exporter", "//bundle:environment_yaml_parser", "//checker", - "//checker:checker_legacy_environment", "//checker:proto_type_mask", "//checker:standard_decl", + "//checker:type_provider_legacy", "//common:cel_ast", "//common:cel_descriptor_util", + "//common:cel_function_decl", + "//common:cel_issue", + "//common:cel_overload_decl", "//common:cel_source", - "//common:compiler_common", + "//common:cel_validation_exception", + "//common:cel_validation_result", + "//common:cel_var_decl", "//common:container", "//common:error_codes", "//common:options", diff --git a/bundle/src/test/java/dev/cel/bundle/CelEnvironmentExporterTest.java b/bundle/src/test/java/dev/cel/bundle/CelEnvironmentExporterTest.java index 7560a12aa..461453587 100644 --- a/bundle/src/test/java/dev/cel/bundle/CelEnvironmentExporterTest.java +++ b/bundle/src/test/java/dev/cel/bundle/CelEnvironmentExporterTest.java @@ -50,13 +50,13 @@ @RunWith(TestParameterInjector.class) public class CelEnvironmentExporterTest { - public static final CelOptions CEL_OPTIONS = - CelOptions.newBuilder().enableHeterogeneousNumericComparisons(true).build(); + private static final CelOptions CEL_OPTIONS = + CelOptions.current().enableHeterogeneousNumericComparisons(true).build(); @Test public void extensions_latest() { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math(CEL_OPTIONS, 2)) .build(); @@ -70,7 +70,7 @@ public void extensions_latest() { @Test public void extensions_earlierVersion() { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math(CEL_OPTIONS, 1)) .build(); @@ -88,7 +88,7 @@ public void standardLibrarySubset_favorExclusion() throws Exception { CelEnvironment environment = CelEnvironmentYamlParser.newInstance().parse(yamlFileContent); Cel standardCel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math(CEL_OPTIONS, 2)) .build(); Cel extendedCel = environment.extend(standardCel, CEL_OPTIONS); @@ -123,7 +123,7 @@ public void standardLibrarySubset_favorInclusion() throws Exception { CelEnvironment environment = CelEnvironmentYamlParser.newInstance().parse(yamlFileContent); Cel standardCel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math(CEL_OPTIONS, 2)) .build(); Cel extendedCel = environment.extend(standardCel, CEL_OPTIONS); @@ -171,7 +171,7 @@ public void standardLibrarySubset_favorInclusion() throws Exception { @Test public void customFunctions() { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math(CEL_OPTIONS, 1)) .addFunctionDeclarations( CelFunctionDecl.newFunctionDeclaration( @@ -296,7 +296,7 @@ public void customFunctions() { @Test public void customVariables() { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVarDeclarations( CelVarDecl.newVarDeclaration("x", SimpleType.INT), CelVarDecl.newVarDeclaration("y", OpaqueType.create("foo.Bar"))) @@ -321,7 +321,7 @@ public void customVariables() { @Test public void container() { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setContainer( CelContainer.newBuilder() .setName("cntnr") @@ -342,7 +342,7 @@ public void container() { @Test public void options() { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setOptions( CelOptions.current() .maxExpressionCodePointSize(100) diff --git a/bundle/src/test/java/dev/cel/bundle/CelEnvironmentTest.java b/bundle/src/test/java/dev/cel/bundle/CelEnvironmentTest.java index a48ea0ff8..7ff605995 100644 --- a/bundle/src/test/java/dev/cel/bundle/CelEnvironmentTest.java +++ b/bundle/src/test/java/dev/cel/bundle/CelEnvironmentTest.java @@ -41,6 +41,9 @@ @RunWith(TestParameterInjector.class) public class CelEnvironmentTest { + private static final CelOptions CEL_OPTIONS = + CelOptions.current().enableHeterogeneousNumericComparisons(true).build(); + @Test public void newBuilder_defaults() { CelEnvironment environment = CelEnvironment.newBuilder().build(); @@ -90,7 +93,7 @@ public void extend_allExtensions() throws Exception { CelEnvironment environment = CelEnvironment.newBuilder().addExtensions(extensionConfigs).build(); - Cel cel = environment.extend(CelFactory.standardCelBuilder().build(), CelOptions.DEFAULT); + Cel cel = environment.extend(CelFactory.plannerCelBuilder().build(), CEL_OPTIONS); CelAbstractSyntaxTree ast = cel.compile( "cel.bind(x, 10, math.greatest([1,x])) < int(' 11 '.trim()) &&" @@ -116,7 +119,7 @@ public void extend_allFeatureFlags() throws Exception { Cel cel = environment.extend( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .build(), CelOptions.DEFAULT); @@ -140,10 +143,10 @@ public void extend_allLimits() throws Exception { Cel cel = environment.extend( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .build(), - CelOptions.DEFAULT); + CEL_OPTIONS); CelOptions checkerOptions = cel.toCheckerBuilder().options(); assertThat(checkerOptions.maxExpressionCodePointSize()).isEqualTo(20); assertThat(checkerOptions.maxParseErrorRecoveryLimit()).isEqualTo(10); @@ -169,10 +172,10 @@ public void extend_expressionNodeCountLimit() throws Exception { Cel cel = environment.extend( - CelFactory.legacyCelBuilder() + CelFactory.plannerCelBuilder() .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .build(), - CelOptions.DEFAULT); + CEL_OPTIONS); CelOptions checkerOptions = cel.toCheckerBuilder().options(); assertThat(checkerOptions.maxParseExpressionNodeCount()).isEqualTo(2); @@ -193,10 +196,10 @@ public void extend_unsupportedFeatureFlag_throws() throws Exception { IllegalArgumentException.class, () -> environment.extend( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .build(), - CelOptions.DEFAULT)); + CEL_OPTIONS)); assertThat(e).hasMessageThat().contains("Unknown feature flag: unknown.feature"); } @@ -212,10 +215,10 @@ public void extend_unsupportedLimit_throws() throws Exception { IllegalArgumentException.class, () -> environment.extend( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .build(), - CelOptions.DEFAULT)); + CEL_OPTIONS)); assertThat(e).hasMessageThat().contains("Unknown limit: unknown.limit"); } @@ -224,7 +227,7 @@ public void extensionVersion_specific() throws Exception { CelEnvironment environment = CelEnvironment.newBuilder().addExtensions(ExtensionConfig.of("math", 1)).build(); - Cel cel = environment.extend(CelFactory.standardCelBuilder().build(), CelOptions.DEFAULT); + Cel cel = environment.extend(CelFactory.plannerCelBuilder().build(), CEL_OPTIONS); CelAbstractSyntaxTree ast1 = cel.compile("math.abs(-4)").getAst(); assertThat(cel.createProgram(ast1).eval()).isEqualTo(4); @@ -244,7 +247,7 @@ public void extensionVersion_latest() throws Exception { CelEnvironment environment = CelEnvironment.newBuilder().addExtensions(ExtensionConfig.latest("math")).build(); - Cel cel = environment.extend(CelFactory.standardCelBuilder().build(), CelOptions.DEFAULT); + Cel cel = environment.extend(CelFactory.plannerCelBuilder().build(), CEL_OPTIONS); CelAbstractSyntaxTree ast = cel.compile("math.sqrt(4)").getAst(); double result = (double) cel.createProgram(ast).eval(); assertThat(result).isEqualTo(2.0); @@ -259,7 +262,7 @@ public void extensionVersion_unsupportedVersion_throws() { assertThrows( CelEnvironmentException.class, () -> { - environment.extend(CelFactory.standardCelBuilder().build(), CelOptions.DEFAULT); + environment.extend(CelFactory.plannerCelBuilder().build(), CEL_OPTIONS); })) .hasMessageThat() .contains("Unsupported 'math' extension version -5"); diff --git a/bundle/src/test/java/dev/cel/bundle/CelEnvironmentYamlParserTest.java b/bundle/src/test/java/dev/cel/bundle/CelEnvironmentYamlParserTest.java index 9a07a854d..acc89ff18 100644 --- a/bundle/src/test/java/dev/cel/bundle/CelEnvironmentYamlParserTest.java +++ b/bundle/src/test/java/dev/cel/bundle/CelEnvironmentYamlParserTest.java @@ -54,8 +54,11 @@ @RunWith(TestParameterInjector.class) public final class CelEnvironmentYamlParserTest { + private static final CelOptions CEL_OPTIONS = + CelOptions.current().enableHeterogeneousNumericComparisons(true).build(); + private static final Cel CEL_WITH_MESSAGE_TYPES = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addMessageTypes(AttributeContext.Request.getDescriptor()) .build(); @@ -170,7 +173,7 @@ public void environment_setExtensions() throws Exception { ExtensionConfig.of("sets"), ExtensionConfig.of("strings", 1))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -188,7 +191,7 @@ public void environment_setExtensionVersionToLatest() throws Exception { .setSource(environment.source().get()) .addExtensions(ImmutableSet.of(ExtensionConfig.of("bindings", Integer.MAX_VALUE))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -299,7 +302,7 @@ public void environment_setFunctions() throws Exception { .build()) .build())))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -354,7 +357,7 @@ public void environment_setMapVariable() throws Exception { .addParams(TypeDecl.create("string"), TypeDecl.create("dyn")) .build()))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -377,7 +380,7 @@ public void environment_setMessageVariable() throws Exception { "request", TypeDecl.create("google.rpc.context.AttributeContext.Request")))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -402,7 +405,7 @@ public void environment_setListVariable_shorthand() throws Exception { .addParams(TypeDecl.create("string")) .build()))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -427,37 +430,7 @@ public void environment_setMapVariable_shorthand() throws Exception { .addParams(TypeDecl.create("string"), TypeDecl.create("dyn")) .build()))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); - } - - @Test - public void environment_withTypeSpecifiersEnabled_handlesStructuredMapTypeDecl() - throws Exception { - String yamlConfig = - "variables:\n" // - + "- name: 'request'\n" // - + " type:\n" // - + " type_name: 'map'\n" // - + " params:\n" // - + " - type_name: 'string'\n" // - + " - type_name: 'dyn'"; - - CelEnvironment environment = ENVIRONMENT_PARSER.parse(yamlConfig); - - assertThat(environment) - .isEqualTo( - CelEnvironment.newBuilder() - .setSource(environment.source().get()) - .setVariables( - ImmutableSet.of( - VariableDecl.create( - "request", - TypeDecl.newBuilder() - .setName("map") - .addParams(TypeDecl.create("string"), TypeDecl.create("dyn")) - .build()))) - .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -484,7 +457,7 @@ public void environment_withTypeSpecifiersEnabled_handlesBlockScalarTextTypeDecl .addParams(TypeDecl.create("string")) .build()))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -506,7 +479,7 @@ public void environment_setMessageVariable_shorthand() throws Exception { "request", TypeDecl.create("google.rpc.context.AttributeContext.Request")))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -524,7 +497,7 @@ public void environment_setContextVariable_type() throws Exception { .setContextVariable( ContextVariable.create("google.rpc.context.AttributeContext.Request")) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -582,7 +555,7 @@ public void environment_setFunctions_shorthand() throws Exception { .setReturnType(TypeDecl.ofTypeParam("V")) .build())))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -619,7 +592,7 @@ public void environment_evaluatesShorthandVariable() throws Exception { + " type: 'list'"; CelEnvironment environment = ENVIRONMENT_PARSER.parse(yamlConfig); - Cel cel = environment.extend(CelFactory.standardCelBuilder().build(), CelOptions.DEFAULT); + Cel cel = environment.extend(CelFactory.plannerCelBuilder().build(), CEL_OPTIONS); CelAbstractSyntaxTree ast = cel.compile("values.size() == 2 && values[0] == 'hello'").getAst(); boolean result = @@ -652,7 +625,7 @@ public void environment_setContainer() throws Exception { "request", TypeDecl.create("google.rpc.context.AttributeContext.Request")))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -715,7 +688,7 @@ public void environment_withInlinedVariableDecl() throws Exception { .addParams(TypeDecl.create("string"), TypeDecl.create("string")) .build()))) .build()); - assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)).isNotNull(); + assertThat(environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)).isNotNull(); } @Test @@ -734,7 +707,7 @@ public void environment_extendErrors(@TestParameter EnvironmentExtendErrorTestCa CelEnvironmentException e = assertThrows( CelEnvironmentException.class, - () -> environment.extend(CEL_WITH_MESSAGE_TYPES, CelOptions.DEFAULT)); + () -> environment.extend(CEL_WITH_MESSAGE_TYPES, CEL_OPTIONS)); assertThat(e).hasMessageThat().isEqualTo(testCase.expectedErrorMessage); } @@ -1254,13 +1227,16 @@ public void lateBoundFunction_evaluate_callExpr() throws Exception { + " return:\n" + " type_name: 'bool'"; CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource); + // TODO: CelEnvironment.extendRuntime does not automatically register + // YAML-declared functions without bindings as late-bound. Cel celDetails = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("a", SimpleType.INT) .addVar("b", SimpleType.INT) .addVar("c", SimpleType.INT) + .addLateBoundFunctions("test") .build(); - Cel cel = celEnvironment.extend(celDetails, CelOptions.DEFAULT); + Cel cel = celEnvironment.extend(celDetails, CEL_OPTIONS); CelAbstractSyntaxTree ast = cel.compile("a < 0 && b < 0 && c < 0 && test(a<0)").getAst(); CelLateFunctionBindings bindings = CelLateFunctionBindings.from( @@ -1295,12 +1271,13 @@ public void lateBoundFunction_trace_callExpr_identifyFalseBranch() throws Except CelEnvironment celEnvironment = ENVIRONMENT_PARSER.parse(configSource); Cel celDetails = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("a", SimpleType.INT) .addVar("b", SimpleType.INT) .addVar("c", SimpleType.INT) + .addLateBoundFunctions("test") .build(); - Cel cel = celEnvironment.extend(celDetails, CelOptions.DEFAULT); + Cel cel = celEnvironment.extend(celDetails, CEL_OPTIONS); CelAbstractSyntaxTree ast = cel.compile("a < 0 && b < 0 && c < 0 && test(a<0)").getAst(); CelLateFunctionBindings bindings = CelLateFunctionBindings.from( diff --git a/bundle/src/test/java/dev/cel/bundle/CelFactoryTest.java b/bundle/src/test/java/dev/cel/bundle/CelFactoryTest.java deleted file mode 100644 index 456ea628c..000000000 --- a/bundle/src/test/java/dev/cel/bundle/CelFactoryTest.java +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.bundle; - -import static com.google.common.truth.Truth.assertThat; - -import dev.cel.common.ast.CelExpr; -import dev.cel.parser.CelMacro; -import java.util.Optional; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public final class CelFactoryTest { - - @Test - public void legacyCelBuilder() { - assertThat(CelFactory.standardCelBuilder().build()).isNotNull(); - } - - @Test - public void standardCelBuilder() { - assertThat(CelFactory.standardCelBuilder().build()).isNotNull(); - } - - @Test - public void addCustomMacros_currentParser_success() { - assertThat( - CelFactory.standardCelBuilder() - .addMacros( - CelMacro.newReceiverMacro( - "dummy", 0, (a, b, c) -> Optional.of(CelExpr.newBuilder().build()))) - .build()) - .isNotNull(); - } -} diff --git a/bundle/src/test/java/dev/cel/bundle/CelImplTest.java b/bundle/src/test/java/dev/cel/bundle/CelImplTest.java index 4f82411a3..8665e4e9a 100644 --- a/bundle/src/test/java/dev/cel/bundle/CelImplTest.java +++ b/bundle/src/test/java/dev/cel/bundle/CelImplTest.java @@ -109,8 +109,8 @@ import dev.cel.runtime.CelFunctionBinding; import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelRuntime.Program; +import dev.cel.runtime.CelRuntimeBuilder; import dev.cel.runtime.CelRuntimeFactory; -import dev.cel.runtime.CelRuntimeLegacyImpl; import dev.cel.runtime.CelStandardFunctions; import dev.cel.runtime.CelUnknownSet; import dev.cel.runtime.CelVariableResolver; @@ -197,17 +197,13 @@ public final class CelImplTest { .setTestOnly(true))) .build(); - private CelBuilder standardCelBuilderWithMacros() { - return CelFactory.standardCelBuilder().setStandardMacros(CelStandardMacro.STANDARD_MACROS); - } - @Test public void build_badFileDescriptorSet() { IllegalArgumentException e = Assert.assertThrows( IllegalArgumentException.class, () -> - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setContainer(CelContainer.ofName("cel.expr.conformance.proto2")) .addFileTypes( FileDescriptorSet.newBuilder() @@ -219,13 +215,13 @@ public void build_badFileDescriptorSet() { @Test public void parse() throws Exception { - Cel cel = standardCelBuilderWithMacros().build(); + Cel cel = plannerCelBuilderWithMacros().build(); assertValidationResult(cel.parse("true && !false"), PARSED_EXPR); } @Test public void check() throws Exception { - Cel cel = standardCelBuilderWithMacros().setResultType(SimpleType.BOOL).build(); + Cel cel = plannerCelBuilderWithMacros().setResultType(SimpleType.BOOL).build(); CelValidationResult parseResult = cel.parse("true && !false"); assertValidationResult(parseResult, PARSED_EXPR); CelValidationResult checkResult = cel.check(parseResult.getAst()); @@ -234,7 +230,7 @@ public void check() throws Exception { @Test public void compile(@TestParameter boolean useProtoResultType) throws Exception { - CelBuilder celBuilder = standardCelBuilderWithMacros(); + CelBuilder celBuilder = plannerCelBuilderWithMacros(); if (useProtoResultType) { celBuilder.setProtoResultType(CelProtoTypes.BOOL); } else { @@ -246,7 +242,7 @@ public void compile(@TestParameter boolean useProtoResultType) throws Exception @Test public void compile_resultTypeCheckFailure(@TestParameter boolean useProtoResultType) { - CelBuilder celBuilder = standardCelBuilderWithMacros(); + CelBuilder celBuilder = plannerCelBuilderWithMacros(); if (useProtoResultType) { celBuilder.setProtoResultType(CelProtoTypes.STRING); } else { @@ -265,7 +261,7 @@ public void compile_combinedTypeProvider() { ProtoMessageTypeProvider celTypeProvider = new ProtoMessageTypeProvider(ImmutableList.of(AttributeContext.getDescriptor())); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setContainer(CelContainer.ofName("google")) .setTypeProvider(celTypeProvider) .addMessageTypes(com.google.type.Expr.getDescriptor()) @@ -286,7 +282,7 @@ public void compile_customTypeProvider() { ImmutableList.of( AttributeContext.getDescriptor(), com.google.type.Expr.getDescriptor())); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setContainer(CelContainer.ofName("google")) .setTypeProvider(celTypeProvider) .addVar("condition", StructTypeReference.create("google.type.Expr")) @@ -299,7 +295,6 @@ public void compile_customTypeProvider() { @Test public void compile_customTypesWithAliasingCombinedProviders() throws Exception { - // The custom type provider sets up an alias from "Condition" to "google.type.Expr". // However, the first type resolution from the alias to the qualified type name won't be // sufficient as future checks will expect the resolved alias to also be a type. @@ -314,7 +309,7 @@ public void compile_customTypesWithAliasingCombinedProviders() throws Exception // The custom type factory is then necessary to ensure that the Condition type listed // in the AST can be resolved to the appropriate message builder instance. Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setTypeProvider(customTypeProvider) .addMessageTypes(com.google.type.Expr.getDescriptor()) .setTypeFactory( @@ -331,7 +326,6 @@ public void compile_customTypesWithAliasingCombinedProviders() throws Exception @Test public void compile_customTypesWithAliasingSelfContainedProvider() throws Exception { - // The custom type provider sets up an alias from "Condition" to "google.type.Expr". TypeProvider customTypeProvider = aliasingProvider( @@ -348,7 +342,7 @@ public void compile_customTypesWithAliasingSelfContainedProvider() throws Except // The custom type factory is then necessary to ensure that the Condition type listed // in the AST can be resolved to the appropriate message builder instance. Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setTypeProvider(customTypeProvider) .setTypeFactory( (typeName) -> @@ -373,7 +367,7 @@ public void program_setTypeFactoryOnAnyPackedMessage_fieldSelectionSuccess() thr .build(); CelAbstractSyntaxTree ast = celCompiler.compile("input.expression").getAst(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() // CEL-Internal-2 .setTypeFactory( (typeName) -> @@ -403,7 +397,7 @@ public void program_setTypeFactoryOnAnyPackedMessage_messageConstructionSucceeds CelAbstractSyntaxTree ast = celCompiler.compile("input").getAst(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() // CEL-Internal-2 .setTypeFactory( (typeName) -> @@ -429,7 +423,7 @@ public void program_concurrentMessageConstruction_succeeds( // Arrange int threadCount = 10; Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setContainer(CelContainer.ofName("google.rpc.context.AttributeContext")) .addFileTypes( Any.getDescriptor().getFile(), @@ -462,7 +456,7 @@ public void program_concurrentMessageConstruction_succeeds( @Test public void compile_syntaxFailure() throws Exception { - Cel cel = standardCelBuilderWithMacros().build(); + Cel cel = plannerCelBuilderWithMacros().build(); CelValidationResult result = cel.compile("|| false"); assertThat(result.hasError()).isTrue(); assertThat(result.getErrors()) @@ -483,7 +477,7 @@ public void compile_syntaxFailure() throws Exception { @Test public void compile_typeCheckFailure() { - Cel cel = standardCelBuilderWithMacros().build(); + Cel cel = plannerCelBuilderWithMacros().build(); CelValidationResult syntaxErrorResult = cel.compile("variable"); assertThat(syntaxErrorResult.hasError()).isTrue(); assertThat(syntaxErrorResult.getErrors()) @@ -502,8 +496,12 @@ public void compile_typeCheckFailure() { @Test public void compile_withOptionalTypes() throws Exception { Cel cel = - CelFactory.standardCelBuilder() - .setOptions(CelOptions.current().enableOptionalSyntax(true).build()) + CelFactory.plannerCelBuilder() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .enableOptionalSyntax(true) + .build()) .addVar("a", OptionalType.create(SimpleType.STRING)) .build(); @@ -517,7 +515,7 @@ public void compile_withOptionalTypes() throws Exception { @Test public void compile_overlappingVarsFailure() { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addDeclarations( Decl.newBuilder() .setName("variable") @@ -540,7 +538,7 @@ public void compile_overlappingVarsFailure() { @Test public void program() throws Exception { - Cel cel = standardCelBuilderWithMacros().setResultType(SimpleType.BOOL).build(); + Cel cel = plannerCelBuilderWithMacros().setResultType(SimpleType.BOOL).build(); CelRuntime.Program program = cel.createProgram(cel.compile("true && !false").getAst()); assertThat(program.eval()).isEqualTo(true); } @@ -548,7 +546,7 @@ public void program() throws Exception { @Test public void program_withVars() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addDeclarations( Decl.newBuilder() .setName("variable") @@ -564,7 +562,7 @@ public void program_withVars() throws Exception { @Test public void program_withProtoVars() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(AttributeContext.getDescriptor()) .addProtoTypeMasks( ProtoTypeMask.of( @@ -590,7 +588,7 @@ public void program_withProtoVars() throws Exception { @Test public void program_withAllFieldsHidden_emptyMessageConstructionSuccess() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(AttributeContext.getDescriptor()) .setContainer(CelContainer.ofName("google.rpc.context.AttributeContext")) .addProtoTypeMasks( @@ -604,7 +602,7 @@ public void program_withAllFieldsHidden_emptyMessageConstructionSuccess() throws @Test public void compile_withAllFieldsHidden_selectHiddenField_throws() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(AttributeContext.getDescriptor()) .setContainer(CelContainer.ofName("google.rpc.context.AttributeContext")) .addProtoTypeMasks( @@ -621,7 +619,7 @@ public void compile_withAllFieldsHidden_selectHiddenField_throws() throws Except @Test public void compile_withAllFieldsHidden_selectHiddenFieldOnVar_throws() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(AttributeContext.getDescriptor()) .setContainer(CelContainer.ofName("google.rpc.context.AttributeContext")) .addProtoTypeMasks( @@ -637,7 +635,7 @@ public void compile_withAllFieldsHidden_selectHiddenFieldOnVar_throws() throws E @Test public void program_withNestedRestrictedProtoVars() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(AttributeContext.getDescriptor()) .addProtoTypeMasks( ProtoTypeMask.of( @@ -656,7 +654,7 @@ public void program_withNestedRestrictedProtoVars() throws Exception { @Test public void program_withFunctions() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addDeclarations( ImmutableList.of( Decl.newBuilder() @@ -713,7 +711,7 @@ public void program_withFunctions() throws Exception { @Test public void program_withThrowingFunction() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addDeclarations( Decl.newBuilder() .setName("throws") @@ -741,7 +739,7 @@ public void program_withThrowingFunction() throws Exception { @Test public void program_withThrowingFunctionShortcircuited() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addDeclarations( Decl.newBuilder() .setName("throws") @@ -775,7 +773,7 @@ public void program_simpleStructTypeReference() throws Exception { .addMessageTypes(Expr.getDescriptor()) .setResultType(SimpleType.BOOL) .build(); - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime celRuntime = CelRuntimeFactory.plannerRuntimeBuilder().build(); CelRuntime.Program program = celRuntime.createProgram(celCompiler.compile("test.id == 2").getAst()); @@ -788,11 +786,10 @@ public void program_simpleStructTypeReference() throws Exception { @Test public void program_messageConstruction() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setContainer(CelContainer.ofName("google.type")) .addMessageTypes(com.google.type.Expr.getDescriptor()) .setResultType(StructTypeReference.create("google.type.Expr")) - .setStandardEnvironmentEnabled(false) .build(); CelRuntime.Program program = cel.createProgram(cel.compile("type.Expr{expression: \"'hello'\"}").getAst()); @@ -803,7 +800,7 @@ public void program_messageConstruction() throws Exception { @Test public void program_duplicateTypeDescriptor() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(Timestamp.getDescriptor()) .addMessageTypes(ImmutableList.of(Timestamp.getDescriptor())) .setContainer(CelContainer.ofName("google")) @@ -818,7 +815,7 @@ public void program_duplicateTypeDescriptor() throws Exception { @Test public void program_hermeticDescriptors_wellKnownProtobuf() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() // CEL-Internal-2 .addMessageTypes(Timestamp.getDescriptor()) .setContainer(CelContainer.ofName("google")) @@ -833,7 +830,7 @@ public void program_hermeticDescriptors_wellKnownProtobuf() throws Exception { public void program_partialMessageTypes() throws Exception { String packageName = CheckedExpr.getDescriptor().getFile().getPackage(); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addFileTypes(CheckedExpr.getDescriptor().getFile()) // Disabling the resolution of type dependencies can be risky as message types which // are expected to be available in an imported file may not be present if the type @@ -842,7 +839,11 @@ public void program_partialMessageTypes() throws Exception { // In this test 'Expr' is defined in syntax.proto, but the descriptor provided is // defined in checked.proto. Because the `Expr` type is referenced within a message // field of the CheckedExpr, it is available for use. - .setOptions(CelOptions.current().resolveTypeDependencies(false).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(false) + .build()) .setContainer(CelContainer.ofName(packageName)) .setResultType(StructTypeReference.create(packageName + ".Expr")) .build(); @@ -854,12 +855,16 @@ public void program_partialMessageTypes() throws Exception { public void program_partialMessageTypeFailure() { String packageName = CheckedExpr.getDescriptor().getFile().getPackage(); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addFileTypes(CheckedExpr.getDescriptor().getFile()) // In this test 'ParsedExpr' is defined in syntax.proto, but the descriptor provided is // defined in checked.proto. Because the `ParsedExpr` type is not referenced, it is not // available for use within CEL when deep type resolution is disabled. - .setOptions(CelOptions.current().resolveTypeDependencies(false).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(false) + .build()) .setContainer(CelContainer.ofName(packageName)) .setResultType(StructTypeReference.create(packageName + ".ParsedExpr")) .build(); @@ -873,12 +878,16 @@ public void program_partialMessageTypeFailure() { public void program_deepTypeResolution() throws Exception { String packageName = CheckedExpr.getDescriptor().getFile().getPackage(); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addFileTypes(CheckedExpr.getDescriptor().getFile()) // In this test 'ParsedExpr' is defined in syntax.proto, but the descriptor provided is // defined in checked.proto. Because deep type dependency resolution is enabled, the // `ParsedExpr` may be used within CEL. - .setOptions(CelOptions.current().resolveTypeDependencies(true).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(true) + .build()) .setContainer(CelContainer.ofName(packageName)) .setResultType(StructTypeReference.create(packageName + ".ParsedExpr")) .build(); @@ -899,9 +908,13 @@ public void program_deepTypeResolutionEnabledForRuntime_success() throws Excepti CelAbstractSyntaxTree ast = celCompiler.compile("ParsedExpr{}").getAst(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFileTypes(CheckedExpr.getDescriptor().getFile()) - .setOptions(CelOptions.current().resolveTypeDependencies(true).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(true) + .build()) // CEL-Internal-2 .build(); CelRuntime.Program program = celRuntime.createProgram(ast); @@ -927,8 +940,9 @@ public void program_deepTypeResolutionDisabledForRuntime_fails() throws Exceptio // 'ParsedExpr' is transitively available for use because deep type resolution is enabled. CelAbstractSyntaxTree ast = celCompiler.compile("ParsedExpr{}").getAst(); + // TODO: Planner runtime ignores CelOptions.resolveTypeDependencies(false). CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .addFileTypes(CheckedExpr.getDescriptor().getFile()) .setOptions(CelOptions.current().resolveTypeDependencies(false).build()) // CEL-Internal-2 @@ -949,7 +963,7 @@ public void program_deepTypeResolutionDisabledForRuntime_fails() throws Exceptio @SuppressWarnings("deprecation") // Test for existing deprecated method setTypeProvider public void program_typeProvider() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setTypeProvider( new DescriptorTypeProvider(ImmutableList.of(Timestamp.getDescriptor()))) .setContainer(CelContainer.ofName("google")) @@ -963,7 +977,7 @@ public void program_typeProvider() throws Exception { @Test public void program_protoActivation() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(AttributeContext.getDescriptor()) .addDeclarations( Decl.newBuilder() @@ -989,10 +1003,13 @@ public void program_protoActivation() throws Exception { public void program_enumTypeDirectResolution(@TestParameter boolean resolveTypeDependencies) throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addFileTypes(StandaloneGlobalEnum.getDescriptor().getFile()) .setOptions( - CelOptions.current().resolveTypeDependencies(resolveTypeDependencies).build()) + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(resolveTypeDependencies) + .build()) .setContainer( CelContainer.ofName("dev.cel.testing.testdata.proto3.StandaloneGlobalEnum")) .setResultType(SimpleType.BOOL) @@ -1011,9 +1028,12 @@ public void program_enumTypeDirectResolution(@TestParameter boolean resolveTypeD public void program_enumTypeReferenceResolution(@TestParameter boolean resolveTypeDependencies) throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setOptions( - CelOptions.current().resolveTypeDependencies(resolveTypeDependencies).build()) + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(resolveTypeDependencies) + .build()) .addMessageTypes(Struct.getDescriptor()) .setResultType(StructTypeReference.create("google.protobuf.NullValue")) .setContainer(CelContainer.ofName("google.protobuf")) @@ -1030,8 +1050,12 @@ public void program_enumTypeReferenceResolution(@TestParameter boolean resolveTy @Test public void program_enumTypeTransitiveResolution() throws Exception { Cel cel = - standardCelBuilderWithMacros() - .setOptions(CelOptions.current().resolveTypeDependencies(true).build()) + plannerCelBuilderWithMacros() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(true) + .build()) .addMessageTypes(Proto2ExtensionScopedMessage.getDescriptor()) .setResultType(StructTypeReference.create("google.protobuf.NullValue")) .setContainer(CelContainer.ofName("google.protobuf")) @@ -1049,7 +1073,7 @@ public void program_enumTypeTransitiveResolution() throws Exception { @Test public void compile_enumTypeIsEquivalentToInt() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addFileTypes(StandaloneGlobalEnum.getDescriptor().getFile()) .addVar("enumVar", EnumType.create("enum", ImmutableMap.of("FOO", 0, "BAR", 1))) .setResultType(SimpleType.BOOL) @@ -1063,8 +1087,12 @@ public void compile_enumTypeIsEquivalentToInt() throws Exception { @Test public void compile_enumTypeTransitiveResolutionFailure() { Cel cel = - standardCelBuilderWithMacros() - .setOptions(CelOptions.current().resolveTypeDependencies(false).build()) + plannerCelBuilderWithMacros() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .resolveTypeDependencies(false) + .build()) .addMessageTypes(Proto2ExtensionScopedMessage.getDescriptor()) .setResultType(StructTypeReference.create("google.protobuf.NullValue")) .setContainer(CelContainer.ofName("google.protobuf")) @@ -1099,7 +1127,7 @@ public void compile_multipleInstancesOfEnumDescriptor_dedupedByFullName() throws FileDescriptor enumFileDescriptor = FileDescriptor.buildFrom(enumFileDescriptorProto, new FileDescriptor[] {}); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .setContainer(CelContainer.ofName("dev.cel.testing.testdata")) .addFileTypes(enumFileDescriptor) .addFileTypes(StandaloneGlobalEnum.getDescriptor().getFile()) @@ -1112,7 +1140,7 @@ public void compile_multipleInstancesOfEnumDescriptor_dedupedByFullName() throws @Test public void program_customVarResolver() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addDeclarations( Decl.newBuilder() .setName("variable") @@ -1130,18 +1158,18 @@ public void program_customVarResolver() throws Exception { @Test public void program_wrongTypeComprehensionThrows() throws Exception { - Cel cel = standardCelBuilderWithMacros().setResultType(SimpleType.BOOL).build(); + Cel cel = plannerCelBuilderWithMacros().setResultType(SimpleType.BOOL).build(); CelRuntime.Program program = cel.createProgram(cel.compile("dyn(42).exists(x, x != 'foo')").getAst()); CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval); - assertThat(e).hasMessageThat().contains("expected a list or a map"); + assertThat(e).hasMessageThat().contains("Unexpected iter_range type: class java.lang.Long"); } @Test public void program_stringFormatInjection_throwsEvaluationException() throws Exception { - Cel cel = standardCelBuilderWithMacros().build(); + Cel cel = plannerCelBuilderWithMacros().build(); CelRuntime.Program program = cel.createProgram(cel.compile("{}['%2000222222s']").getAst()); CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval); @@ -1150,7 +1178,7 @@ public void program_stringFormatInjection_throwsEvaluationException() throws Exc @Test public void program_emptyTypeProviderConfig() throws Exception { - Cel cel = standardCelBuilderWithMacros().build(); + Cel cel = plannerCelBuilderWithMacros().build(); assertThat(cel.createProgram(cel.compile("true && !false").getAst()).eval()).isEqualTo(true); } @@ -1158,7 +1186,7 @@ public void program_emptyTypeProviderConfig() throws Exception { public void program_messageTypeAddedAsVarWithoutDescriptor_throwsHumanReadableError() { String packageName = CheckedExpr.getDescriptor().getFile().getPackage(); Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addVar("parsedExprVar", CelProtoMessageTypes.createMessage(ParsedExpr.getDescriptor())) .build(); CelValidationException exception = @@ -1176,7 +1204,7 @@ public void program_messageTypeAddedAsVarWithoutDescriptor_throwsHumanReadableEr @Test public void setOptions() throws Exception { - Cel cel = standardCelBuilderWithMacros().build(); + Cel cel = plannerCelBuilderWithMacros().build(); CelValidationResult result = cel.parse("!!!true"); assertThat(result.hasError()).isFalse(); assertThat(CelProtoAbstractSyntaxTree.fromCelAst(result.getAst()).toParsedExpr().getExpr()) @@ -1184,8 +1212,12 @@ public void setOptions() throws Exception { .isEqualTo(NOT_EXPR); cel = - standardCelBuilderWithMacros() - .setOptions(CelOptions.newBuilder().retainRepeatedUnaryOperators(true).build()) + plannerCelBuilderWithMacros() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .retainRepeatedUnaryOperators(true) + .build()) .build(); result = cel.parse("!!!true"); assertThat(result.hasError()).isFalse(); @@ -1196,7 +1228,7 @@ public void setOptions() throws Exception { @Test public void setStandardMacros() throws Exception { - Cel cel = standardCelBuilderWithMacros().setStandardMacros(CelStandardMacro.HAS).build(); + Cel cel = plannerCelBuilderWithMacros().setStandardMacros(CelStandardMacro.HAS).build(); assertValidationResult(cel.parse("has(a.b)"), PARSED_HAS_EXPR); } @@ -1223,14 +1255,10 @@ private void assertValidationResult(CelValidationResult result, CheckedExpr chec .isEqualTo(checkedExpr); } - private CelVariableResolver fromMap(ImmutableMap m) { - return (String s) -> Optional.ofNullable(m.get(s)); - } - @Test public void programAdvanceEvaluation_unknownsBasic() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("a", SimpleType.BOOL) .addVar("b", SimpleType.BOOL) @@ -1258,7 +1286,7 @@ public void programAdvanceEvaluation_unknownsBasic() throws Exception { @Test public void programAdvanceEvaluation_attributesIgnoredIfDisabled() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(false).build()) .addVar("a", SimpleType.BOOL) .addVar("b", SimpleType.BOOL) @@ -1281,7 +1309,7 @@ public void programAdvanceEvaluation_attributesIgnoredIfDisabled() throws Except @Test public void programAdvanceEvaluation_logicOperatorTypeMismatchThrows() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("a", SimpleType.BOOL) .addFunctionBindings() @@ -1303,7 +1331,7 @@ public void programAdvanceEvaluation_logicOperatorTypeMismatchThrows() throws Ex @Test public void programAdvanceEvaluation_unknownsCollection() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("a", SimpleType.BOOL) .addVar("b", SimpleType.BOOL) @@ -1334,7 +1362,7 @@ public void programAdvanceEvaluation_unknownsCollection() throws Exception { @Test public void programAdvanceEvaluation_unknownsNamespaceSupport() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("com.google.a", SimpleType.BOOL) .addVar("com.google.b", SimpleType.BOOL) @@ -1363,7 +1391,7 @@ public void programAdvanceEvaluation_unknownsNamespaceSupport() throws Exception @Test public void programAdvanceEvaluation_unknownsIterativeEvalExample() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("com.google.a", SimpleType.BOOL) .addVar("com.google.b", SimpleType.BOOL) @@ -1390,7 +1418,7 @@ public void programAdvanceEvaluation_unknownsIterativeEvalExample() throws Excep @Test public void programAdvanceEvaluation_nestedSelect() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("com", MapType.create(SimpleType.STRING, SimpleType.DYN)) .addFunctionBindings() @@ -1410,7 +1438,7 @@ public void programAdvanceEvaluation_nestedSelect() throws Exception { @Test public void programAdvanceEvaluation_argumentMergeErrorPriority() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk", SimpleType.BOOL) .addDeclarations( @@ -1451,7 +1479,7 @@ public void programAdvanceEvaluation_argumentMergeErrorPriority() throws Excepti @Test public void programAdvanceEvaluation_argumentMergeUnknowns() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk.a", SimpleType.BOOL) .addVar("unk.b", SimpleType.BOOL) @@ -1494,7 +1522,7 @@ public void programAdvanceEvaluation_argumentMergeUnknowns() throws Exception { @Test public void programAdvanceEvaluation_mapSelectUnknowns() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk", MapType.create(SimpleType.STRING, SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1520,7 +1548,7 @@ public void programAdvanceEvaluation_mapSelectUnknowns() throws Exception { @Test public void programAdvanceEvaluation_mapIndexUnknowns() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk", MapType.create(SimpleType.STRING, SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1549,7 +1577,7 @@ public void programAdvanceEvaluation_mapIndexUnknowns() throws Exception { @Test public void programAdvanceEvaluation_listIndexUnknowns() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk", ListType.create(SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1578,7 +1606,7 @@ public void programAdvanceEvaluation_listIndexUnknowns() throws Exception { @Test public void programAdvanceEvaluation_indexOnUnknownContainer() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk", ListType.create(SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1600,7 +1628,7 @@ public void programAdvanceEvaluation_indexOnUnknownContainer() throws Exception @Test public void programAdvanceEvaluation_unsupportedIndexIgnored() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("unk", MapType.create(SimpleType.STRING, SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1637,7 +1665,7 @@ public void programAdvanceEvaluation_unsupportedIndexIgnored() throws Exception @Test public void programAdvanceEvaluation_listIndexMacroTracking() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("testList", ListType.create(SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1670,7 +1698,7 @@ public void programAdvanceEvaluation_listIndexMacroTracking() throws Exception { @Test public void programAdvanceEvaluation_mapIndexMacroTracking() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("testMap", MapType.create(SimpleType.STRING, SimpleType.BOOL)) .setContainer(CelContainer.ofName("")) @@ -1718,7 +1746,7 @@ public void programAdvanceEvaluation_mapIndexMacroTracking() throws Exception { @Test public void programAdvanceEvaluation_boolOperatorMergeUnknownPriority() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVarDeclarations( CelVarDecl.newVarDeclaration("unk", SimpleType.BOOL), @@ -1742,7 +1770,7 @@ public void programAdvanceEvaluation_boolOperatorMergeUnknownPriority() throws E @Test public void programAdvanceEvaluation_partialUnknownMapEntryPropagates() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVarDeclarations( ImmutableList.of( @@ -1775,7 +1803,7 @@ public void programAdvanceEvaluation_partialUnknownMapEntryPropagates() throws E @Test public void programAdvanceEvaluation_partialUnknownListElementPropagates() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addVar("partialList1", ListType.create(SimpleType.INT)) .addVar("partialList2", ListType.create(SimpleType.INT)) @@ -1804,7 +1832,7 @@ public void programAdvanceEvaluation_partialUnknownListElementPropagates() throw @Test public void programAdvanceEvaluation_partialUnknownMessageFieldPropagates() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar( @@ -1869,7 +1897,7 @@ public boolean isAssignableFrom(CelType other) { } }; Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("x", SimpleType.INT) .addFunctionDeclarations( newFunctionDeclaration( @@ -1892,7 +1920,7 @@ public boolean isAssignableFrom(CelType other) { @SuppressWarnings("unchecked") // test only public void program_functionParamWithWellKnownType() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionDeclarations( newFunctionDeclaration( "hasStringValue", @@ -1921,7 +1949,7 @@ public void program_functionParamWithWellKnownType() throws Exception { @Test public void program_nativeTypeUnknownsEnabled_asIdentifiers() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .addVar("x", SimpleType.BOOL) .addVar("y", SimpleType.BOOL) .setOptions(CelOptions.current().build()) @@ -1937,7 +1965,7 @@ public void program_nativeTypeUnknownsEnabled_asIdentifiers() throws Exception { @Test public void program_nativeTypeUnknownsEnabled_asCallArguments() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .addVar("x", SimpleType.BOOL) .addFunctionDeclarations( newFunctionDeclaration( @@ -1954,8 +1982,9 @@ public void program_nativeTypeUnknownsEnabled_asCallArguments() throws Exception @Test public void program_comprehensionDisabled_throws() throws Exception { + // TODO: Planner ExecutionFrame ignores CelOptions.enableComprehension(false). Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().enableComprehension(false).build()) .build(); CelAbstractSyntaxTree ast = cel.compile("['foo', 'bar'].map(x, x)").getAst(); @@ -1969,8 +1998,12 @@ public void program_comprehensionDisabled_throws() throws Exception { @Test public void program_regexProgramSizeUnderLimit_success() throws Exception { Cel cel = - standardCelBuilderWithMacros() - .setOptions(CelOptions.current().maxRegexProgramSize(7).build()) + plannerCelBuilderWithMacros() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .maxRegexProgramSize(7) + .build()) .build(); // See // https://github.com/google/re2j/blob/84237cbbd0fbd637c6eb6856717c1e248daae729/javatests/com/google/re2j/PatternTest.java#L175 for program size @@ -1982,8 +2015,12 @@ public void program_regexProgramSizeUnderLimit_success() throws Exception { @Test public void program_regexProgramSizeExceedsLimit_throws() throws Exception { Cel cel = - standardCelBuilderWithMacros() - .setOptions(CelOptions.current().maxRegexProgramSize(6).build()) + plannerCelBuilderWithMacros() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .maxRegexProgramSize(6) + .build()) .build(); // See // https://github.com/google/re2j/blob/84237cbbd0fbd637c6eb6856717c1e248daae729/javatests/com/google/re2j/PatternTest.java#L175 for program size @@ -2004,7 +2041,7 @@ public void program_regexProgramSizeExceedsLimit_throws() throws Exception { public void program_evaluateCanonicalTypesToNativeTypesDisabled_producesProtoValues() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .setOptions(CelOptions.current().evaluateCanonicalTypesToNativeValues(false).build()) .build(); CelAbstractSyntaxTree ast = cel.compile("[null, {b'abc': null}]").getAst(); @@ -2020,7 +2057,7 @@ public void program_evaluateCanonicalTypesToNativeTypesDisabled_producesProtoVal public void program_evaluateCanonicalTypesToNativeTypesDisabled_producesBytesProto() throws Exception { Cel cel = - standardCelBuilderWithMacros() + legacyCelBuilderWithMacros() .addMessageTypes(TestAllTypes.getDescriptor()) .setContainer(CelContainer.ofName("cel.expr.conformance.proto3")) .setOptions(CelOptions.current().evaluateCanonicalTypesToNativeValues(false).build()) @@ -2067,7 +2104,7 @@ public void program_fdsContainsWktDependency_descriptorInstancesMatch() throws E ExtensionRegistry.getEmptyRegistry()); // Setup CEL environment with the same descriptors obtained from FDS Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addMessageTypes(descriptors) // CEL-Internal-2 .setContainer(CelContainer.ofName("cel.expr.conformance.proto3")) @@ -2083,7 +2120,7 @@ public void program_fdsContainsWktDependency_descriptorInstancesMatch() throws E @Test public void toBuilder_isImmutable() { - CelBuilder celBuilder = CelFactory.standardCelBuilder(); + CelBuilder celBuilder = CelFactory.plannerCelBuilder(); CelImpl celImpl = (CelImpl) celBuilder.build(); CelImpl.Builder newCelBuilder = (CelImpl.Builder) celImpl.toCelBuilder(); @@ -2092,8 +2129,7 @@ public void toBuilder_isImmutable() { (CelCheckerLegacyImpl.Builder) celImpl.toCheckerBuilder(); CelCompilerImpl.Builder newCompilerBuilder = (CelCompilerImpl.Builder) celImpl.toCompilerBuilder(); - CelRuntimeLegacyImpl.Builder newRuntimeBuilder = - (CelRuntimeLegacyImpl.Builder) celImpl.toRuntimeBuilder(); + CelRuntimeBuilder newRuntimeBuilder = celImpl.toRuntimeBuilder(); assertThat(newCelBuilder).isNotEqualTo(celBuilder); assertThat(newParserBuilder).isNotEqualTo(celImpl.toParserBuilder()); @@ -2198,12 +2234,20 @@ public void eval_withJsonFieldName_runtimeOptionDisabled_throws() throws Excepti CelCompilerFactory.standardCelCompilerBuilder() .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) .addMessageTypes(SingleFile.getDescriptor()) - .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .enableJsonFieldNames(true) + .build()) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(SingleFile.getDescriptor()) - .setOptions(CelOptions.current().enableJsonFieldNames(false).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .enableJsonFieldNames(false) + .build()) .build(); CelAbstractSyntaxTree ast = celCompiler.compile("file.int64CamelCaseJsonName").getAst(); @@ -2224,10 +2268,14 @@ public void eval_withJsonFieldName_runtimeOptionDisabled_throws() throws Excepti @Test public void compile_withJsonFieldName_astTagged() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) .addMessageTypes(SingleFile.getDescriptor()) - .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .enableJsonFieldNames(true) + .build()) .build(); CelAbstractSyntaxTree ast = cel.compile("file.int64CamelCaseJsonName").getAst(); @@ -2240,10 +2288,14 @@ public void compile_withJsonFieldName_astTagged() throws Exception { @Test public void compile_withJsonFieldName_protoFieldNameComparison_throws() throws Exception { Cel cel = - standardCelBuilderWithMacros() + plannerCelBuilderWithMacros() .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) .addMessageTypes(SingleFile.getDescriptor()) - .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .enableJsonFieldNames(true) + .build()) .build(); CelValidationException e = @@ -2253,30 +2305,39 @@ public void compile_withJsonFieldName_protoFieldNameComparison_throws() throws E assertThat(e).hasMessageThat().contains("undefined field 'snake_cased'"); } - private static TypeProvider aliasingProvider(ImmutableMap typeAliases) { - return new TypeProvider() { - @Override - public @Nullable Type lookupType(String typeName) { - Type alias = typeAliases.get(typeName); - if (alias != null) { - return CelProtoTypes.create(alias); - } - return null; - } + @Test + public void plannerCelBuilder_setStandardDeclarationsAndFunctions_subsetsEnvironment() + throws Exception { + Cel cel = + CelFactory.plannerCelBuilder() + .setStandardDeclarations( + CelStandardDeclarations.newBuilder() + .includeFunctions(CelStandardDeclarations.StandardFunction.ADD) + .build()) + .setStandardFunctions( + CelStandardFunctions.newBuilder() + .includeFunctions(CelStandardFunctions.StandardFunction.ADD) + .build()) + .build(); - @Override - public @Nullable Integer lookupEnumValue(String enumName) { - return null; - } + CelAbstractSyntaxTree ast = cel.compile("1 + 1").getAst(); + assertThat(cel.createProgram(ast).eval()).isEqualTo(2L); - @Override - public TypeProvider.@Nullable FieldType lookupFieldType(Type type, String fieldName) { - if (typeAliases.containsKey(type.getMessageType())) { - return TypeProvider.FieldType.of(CelProtoTypes.STRING); - } - return null; - } - }; + CelValidationException validationException = + assertThrows(CelValidationException.class, () -> cel.compile("1 - 1").getAst()); + assertThat(validationException).hasMessageThat().contains("undeclared reference to '_-_'"); + } + + private static CelBuilder plannerCelBuilderWithMacros() { + return CelFactory.plannerCelBuilder().setStandardMacros(CelStandardMacro.STANDARD_MACROS); + } + + private static CelBuilder legacyCelBuilderWithMacros() { + return CelFactory.legacyCelBuilder().setStandardMacros(CelStandardMacro.STANDARD_MACROS); + } + + private static CelVariableResolver fromMap(ImmutableMap m) { + return (String s) -> Optional.ofNullable(m.get(s)); } private static Cel setupEnv(CelBuilder celBuilder) { @@ -2297,26 +2358,29 @@ private static Cel setupEnv(CelBuilder celBuilder) { .build(); } - @Test - public void plannerCelBuilder_setStandardDeclarationsAndFunctions_subsetsEnvironment() - throws Exception { - Cel cel = - CelFactory.plannerCelBuilder() - .setStandardDeclarations( - CelStandardDeclarations.newBuilder() - .includeFunctions(CelStandardDeclarations.StandardFunction.ADD) - .build()) - .setStandardFunctions( - CelStandardFunctions.newBuilder() - .includeFunctions(CelStandardFunctions.StandardFunction.ADD) - .build()) - .build(); + private static TypeProvider aliasingProvider(ImmutableMap typeAliases) { + return new TypeProvider() { + @Override + public @Nullable Type lookupType(String typeName) { + Type alias = typeAliases.get(typeName); + if (alias != null) { + return CelProtoTypes.create(alias); + } + return null; + } - CelAbstractSyntaxTree ast = cel.compile("1 + 1").getAst(); - assertThat(cel.createProgram(ast).eval()).isEqualTo(2L); + @Override + public @Nullable Integer lookupEnumValue(String enumName) { + return null; + } - CelValidationException validationException = - assertThrows(CelValidationException.class, () -> cel.compile("1 - 1").getAst()); - assertThat(validationException).hasMessageThat().contains("undeclared reference to '_-_'"); + @Override + public TypeProvider.@Nullable FieldType lookupFieldType(Type type, String fieldName) { + if (typeAliases.containsKey(type.getMessageType())) { + return TypeProvider.FieldType.of(CelProtoTypes.STRING); + } + return null; + } + }; } } diff --git a/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java b/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java index ed3d8b473..2345ca5fb 100644 --- a/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java +++ b/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java @@ -24,8 +24,8 @@ import dev.cel.expr.conformance.proto3.TestAllTypes; import dev.cel.extensions.CelExtensions; import dev.cel.extensions.CelOptionalLibrary; -import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelFunctionBinding; +import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelRuntimeFactory; import java.util.List; import org.junit.Test; @@ -35,7 +35,7 @@ @RunWith(JUnit4.class) public class CelCompilerToolTest { private static final CelRuntime CEL_RUNTIME = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from("wrapper_string_isEmpty", String.class, String::isEmpty)) .addLibraries( diff --git a/conformance/src/test/java/dev/cel/conformance/policy/PolicyConformanceTest.java b/conformance/src/test/java/dev/cel/conformance/policy/PolicyConformanceTest.java index 5727eb5ee..d34a2ba5f 100644 --- a/conformance/src/test/java/dev/cel/conformance/policy/PolicyConformanceTest.java +++ b/conformance/src/test/java/dev/cel/conformance/policy/PolicyConformanceTest.java @@ -39,7 +39,7 @@ public final class PolicyConformanceTest extends Statement { private static final Cel CEL = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionBindings( CelFunctionBinding.from( "locationCode_string", diff --git a/conformance/src/test/java/dev/cel/maven/RuntimeArtifactTest.java b/conformance/src/test/java/dev/cel/maven/RuntimeArtifactTest.java index e129b85d3..206fbbf1e 100644 --- a/conformance/src/test/java/dev/cel/maven/RuntimeArtifactTest.java +++ b/conformance/src/test/java/dev/cel/maven/RuntimeArtifactTest.java @@ -22,7 +22,6 @@ import com.google.protobuf.TextFormat; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import dev.cel.common.CelAbstractSyntaxTree; -import dev.cel.common.CelOptions; import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.expr.conformance.proto3.TestAllTypes; import dev.cel.runtime.CelEvaluationException; @@ -373,9 +372,7 @@ public class RuntimeArtifactTest { @Test public void eval() throws Exception { CelRuntime runtime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .setOptions(CelOptions.DEFAULT) - .setStandardEnvironmentEnabled(false) + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.fromOverloads( "getThree", @@ -400,7 +397,7 @@ public void eval() throws Exception { @Test public void eval_error() throws Exception { CelRuntime runtime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addMessageTypes(TestAllTypes.getDescriptor()) .build(); CheckedExpr checkedExpr = TextFormat.parse(CHECKED_EXPR, CheckedExpr.class); diff --git a/extensions/src/test/java/dev/cel/extensions/CelExtensionsTest.java b/extensions/src/test/java/dev/cel/extensions/CelExtensionsTest.java index 883f00ef5..06a6ba142 100644 --- a/extensions/src/test/java/dev/cel/extensions/CelExtensionsTest.java +++ b/extensions/src/test/java/dev/cel/extensions/CelExtensionsTest.java @@ -19,11 +19,11 @@ import dev.cel.bundle.Cel; import dev.cel.bundle.CelFactory; +import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelOptions; import dev.cel.common.CelValidationException; import dev.cel.extensions.CelStringExtensions.Function; import dev.cel.runtime.CelEvaluationException; -import dev.cel.runtime.CelRuntime; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -34,7 +34,7 @@ public class CelExtensionsTest { @Test public void addAllStringExtensions_success() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.strings()) .addRuntimeLibraries(CelExtensions.strings()) .build(); @@ -55,7 +55,7 @@ public void addAllStringExtensions_success() throws Exception { public void addSubsetOfStringExtensions_success() throws Exception { CelStringExtensions extensions = CelExtensions.strings(Function.SUBSTRING, Function.CHAR_AT); Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(extensions) .addRuntimeLibraries(extensions) .build(); @@ -71,7 +71,7 @@ public void addSubsetOfStringExtensions_success() throws Exception { @Test public void addStringExtensionsForRuntimeOnly_throwsValidationException() { - Cel cel = CelFactory.standardCelBuilder().addRuntimeLibraries(CelExtensions.strings()).build(); + Cel cel = CelFactory.plannerCelBuilder().addRuntimeLibraries(CelExtensions.strings()).build(); CelValidationException exception = assertThrows( @@ -82,10 +82,11 @@ public void addStringExtensionsForRuntimeOnly_throwsValidationException() { @Test public void addStringExtensionsForCompilerOnly_throwsEvaluationException() throws Exception { - Cel cel = CelFactory.standardCelBuilder().addCompilerLibraries(CelExtensions.strings()).build(); + Cel cel = CelFactory.plannerCelBuilder().addCompilerLibraries(CelExtensions.strings()).build(); + CelAbstractSyntaxTree ast = cel.compile("'abcd'.substring(1, 3)").getAst(); - CelRuntime.Program program = cel.createProgram(cel.compile("'abcd'.substring(1, 3)").getAst()); - CelEvaluationException exception = assertThrows(CelEvaluationException.class, program::eval); + CelEvaluationException exception = + assertThrows(CelEvaluationException.class, () -> cel.createProgram(ast)); assertThat(exception) .hasMessageThat() @@ -97,7 +98,7 @@ public void addStringExtensionsForCompilerOnly_throwsEvaluationException() throw @Test public void addAllMathExtensions_success() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math()) .addRuntimeLibraries(CelExtensions.math()) .build(); @@ -112,7 +113,7 @@ public void addAllMathExtensions_success() throws Exception { @Test public void addSubsetOfMathExtensions_success() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.math(CelMathExtensions.Function.MAX)) .addRuntimeLibraries(CelExtensions.math(CelMathExtensions.Function.MAX)) .build(); @@ -127,7 +128,7 @@ public void addSubsetOfMathExtensions_success() throws Exception { @Test public void addEncoderExtension_success() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelExtensions.encoders(CelOptions.DEFAULT)) .addRuntimeLibraries(CelExtensions.encoders(CelOptions.DEFAULT)) .build(); diff --git a/extensions/src/test/java/dev/cel/extensions/CelMathExtensionsTest.java b/extensions/src/test/java/dev/cel/extensions/CelMathExtensionsTest.java index 6fa34de30..0dae9c846 100644 --- a/extensions/src/test/java/dev/cel/extensions/CelMathExtensionsTest.java +++ b/extensions/src/test/java/dev/cel/extensions/CelMathExtensionsTest.java @@ -122,6 +122,8 @@ public void greatest_intResult_success(String expr, long expectedResult) throws @TestParameters("{expr: 'math.greatest(14u, 15.0)', expectedResult: 15.0}") @TestParameters("{expr: 'math.greatest(1, 1.797693e308)', expectedResult: 1.797693e308}") @TestParameters("{expr: 'math.greatest(1.797693e308, 1)', expectedResult: 1.797693e308}") + @TestParameters("{expr: 'math.greatest(1u, 1.797693e308)', expectedResult: 1.797693e308}") + @TestParameters("{expr: 'math.greatest(1.797693e308, 1u)', expectedResult: 1.797693e308}") @TestParameters("{expr: 'math.greatest(-1.797693e308, 1.0)', expectedResult: 1.0}") @TestParameters("{expr: 'math.greatest(1.0, -1.797693e308)', expectedResult: 1.0}") @TestParameters("{expr: 'math.greatest(1.0, 1.0, 1.0)', expectedResult: 1.0}") @@ -130,6 +132,7 @@ public void greatest_intResult_success(String expr, long expectedResult) throws @TestParameters("{expr: 'math.greatest(-1.0, 1.0, 0)', expectedResult: 1.0}") @TestParameters("{expr: 'math.greatest(dyn(1.0), 1.0, 1.0)', expectedResult: 1}") @TestParameters("{expr: 'math.greatest(5.0, 1.0, 3u)', expectedResult: 5.0}") + @TestParameters("{expr: 'math.greatest(5.0, 1.0, dyn(3u))', expectedResult: 5.0}") @TestParameters("{expr: 'math.greatest(5.4, 10.0, 3u, -5.0, 3.5)', expectedResult: 10}") @TestParameters( "{expr: 'math.greatest(5.4, 10, 3u, -5.0, 1.797693e308)', expectedResult:" + " 1.797693e308}") @@ -145,44 +148,6 @@ public void greatest_doubleResult_success(String expr, double expectedResult) th assertThat(result).isEqualTo(expectedResult); } - @Test - @TestParameters("{expr: 'math.greatest(1.0, 1u)', expectedResult: '1.0'}") - @TestParameters("{expr: 'math.greatest(15.0, 14u)', expectedResult: '15.0'}") - @TestParameters("{expr: 'math.greatest(14u, 15.0)', expectedResult: '15.0'}") - @TestParameters("{expr: 'math.greatest(1u, 1.797693e308)', expectedResult: '1.797693e308'}") - @TestParameters("{expr: 'math.greatest(1.797693e308, 1u)', expectedResult: '1.797693e308'}") - @TestParameters("{expr: 'math.greatest(-1.797693e308, 1.0)', expectedResult: '1.0'}") - @TestParameters("{expr: 'math.greatest(5.0, 1.0, 3u)', expectedResult: '5.0'}") - @TestParameters("{expr: 'math.greatest(5.0, 1.0, dyn(3u))', expectedResult: '5.0'}") - @TestParameters("{expr: 'math.greatest(5.4, 10.0, 3u, -5.0, 3.5)', expectedResult: '10'}") - @TestParameters( - "{expr: 'math.greatest(5.4, 10, 3u, -5.0, 1.797693e308)', expectedResult: '1.797693e308'}") - @TestParameters( - "{expr: 'math.greatest(1.797693e308, 10, 3u, -5.0, 0)', expectedResult: '1.797693e308'}") - @TestParameters("{expr: 'math.greatest([5.4, 10.0, 3u, -5.0, 3.5])', expectedResult: '10'}") - @TestParameters( - "{expr: 'math.greatest([dyn(5.4), dyn(10.0), dyn(3u), dyn(-5.0), dyn(3.5)])', expectedResult:" - + " '10.0'}") - public void greatest_doubleResult_withUnsignedLongsEnabled_success( - String expr, double expectedResult) throws Exception { - CelOptions celOptions = CelOptions.DEFAULT; - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - - CelAbstractSyntaxTree ast = celCompiler.compile(expr).getAst(); - double result = (double) celRuntime.createProgram(ast).eval(); - - assertThat(result).isEqualTo(expectedResult); - } - @Test @TestParameters("{expr: 'math.greatest(5u)', expectedResult: '5'}") @TestParameters( @@ -216,20 +181,7 @@ public void greatest_doubleResult_withUnsignedLongsEnabled_success( + " '10'}") public void greatest_unsignedLongResult_withUnsignedLongType_success( String expr, String expectedResult) throws Exception { - CelOptions celOptions = CelOptions.DEFAULT; - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - - CelAbstractSyntaxTree ast = celCompiler.compile(expr).getAst(); - UnsignedLong result = (UnsignedLong) celRuntime.createProgram(ast).eval(); + UnsignedLong result = (UnsignedLong) eval(expr); assertThat(result).isEqualTo(UnsignedLong.valueOf(expectedResult)); } @@ -386,6 +338,8 @@ public void least_intResult_success(String expr, long expectedResult) throws Exc @TestParameters("{expr: 'math.least(14u, 13.0)', expectedResult: 13.0}") @TestParameters("{expr: 'math.least(1, -1.797693e308)', expectedResult: -1.797693e308}") @TestParameters("{expr: 'math.least(-1.797693e308, 1)', expectedResult: -1.797693e308}") + @TestParameters("{expr: 'math.least(1u, -1.797693e308)', expectedResult: -1.797693e308}") + @TestParameters("{expr: 'math.least(-1.797693e308, 1u)', expectedResult: -1.797693e308}") @TestParameters("{expr: 'math.least(1.797693e308, 1.0)', expectedResult: 1.0}") @TestParameters("{expr: 'math.least(1.0, 1.797693e308)', expectedResult: 1.0}") @TestParameters("{expr: 'math.least(1.0, 1.0, 1.0)', expectedResult: 1.0}") @@ -394,6 +348,7 @@ public void least_intResult_success(String expr, long expectedResult) throws Exc @TestParameters("{expr: 'math.least(-1.0, 1.0, 0)', expectedResult: -1.0}") @TestParameters("{expr: 'math.least(dyn(1.0), 1.0, 1.0)', expectedResult: 1.0}") @TestParameters("{expr: 'math.least(5.0, 1.0, 3u)', expectedResult: 1.0}") + @TestParameters("{expr: 'math.least(5.0, 1.0, dyn(3u))', expectedResult: 1.0}") @TestParameters("{expr: 'math.least(5.4, 10.0, 3u, -5.0, 3.5)', expectedResult: -5.0}") @TestParameters( "{expr: 'math.least(5.4, 10, 3u, -5.0, -1.797693e308)', expectedResult: -1.797693e308}") @@ -409,44 +364,6 @@ public void least_doubleResult_success(String expr, double expectedResult) throw assertThat(result).isEqualTo(expectedResult); } - @Test - @TestParameters("{expr: 'math.least(1.0, 1u)', expectedResult: '1.0'}") - @TestParameters("{expr: 'math.least(13.0, 14u)', expectedResult: '13.0'}") - @TestParameters("{expr: 'math.least(14u, 13.0)', expectedResult: '13.0'}") - @TestParameters("{expr: 'math.least(1u, -1.797693e308)', expectedResult: '-1.797693e308'}") - @TestParameters("{expr: 'math.least(-1.797693e308, 1u)', expectedResult: '-1.797693e308'}") - @TestParameters("{expr: 'math.least(1.797693e308, 1.0)', expectedResult: '1.0'}") - @TestParameters("{expr: 'math.least(5.0, 1.0, 3u)', expectedResult: '1.0'}") - @TestParameters("{expr: 'math.least(5.0, 1.0, dyn(3u))', expectedResult: '1.0'}") - @TestParameters("{expr: 'math.least(5.4, 10.0, 3u, -5.0, 3.5)', expectedResult: '-5.0'}") - @TestParameters( - "{expr: 'math.least(5.4, 10, 3u, -5.0, -1.797693e308)', expectedResult: '-1.797693e308'}") - @TestParameters( - "{expr: 'math.least(-1.797693e308, 10, 3u, -5.0, 0)', expectedResult: '-1.797693e308'}") - @TestParameters("{expr: 'math.least([5.4, 10.0, 3u, -5.0, 3.5])', expectedResult: '-5.0'}") - @TestParameters( - "{expr: 'math.least([dyn(5.4), dyn(10.0), dyn(3u), dyn(-5.0), dyn(3.5)])', expectedResult:" - + " '-5.0'}") - public void least_doubleResult_withUnsignedLongsEnabled_success( - String expr, double expectedResult) throws Exception { - CelOptions celOptions = CelOptions.current().enableUnsignedLongs(true).build(); - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - - CelAbstractSyntaxTree ast = celCompiler.compile(expr).getAst(); - double result = (double) celRuntime.createProgram(ast).eval(); - - assertThat(result).isEqualTo(expectedResult); - } - @Test @TestParameters("{expr: 'math.least(9, 1u, 0u)', expectedResult: '0'}") @TestParameters("{expr: 'math.least(dyn(1u), 1, 1.0)', expectedResult: '1'}") @@ -485,20 +402,7 @@ public void least_doubleResult_withUnsignedLongsEnabled_success( + " '3'}") public void least_unsignedLongResult_withUnsignedLongType_success( String expr, String expectedResult) throws Exception { - CelOptions celOptions = CelOptions.current().build(); - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .setOptions(celOptions) - .addLibraries(CelExtensions.math()) - .build(); - - CelAbstractSyntaxTree ast = celCompiler.compile(expr).getAst(); - UnsignedLong result = (UnsignedLong) celRuntime.createProgram(ast).eval(); + UnsignedLong result = (UnsignedLong) eval(expr); assertThat(result).isEqualTo(UnsignedLong.valueOf(expectedResult)); } @@ -1132,7 +1036,7 @@ public void separateLibraryAndRuntime_versioned_success() throws Exception { .addLibraries(CelMathCompilerLibrary.math(0)) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings(CelMathRuntimeLibrary.math(0).newFunctionBindings()) .build(); @@ -1151,7 +1055,7 @@ public void separateLibraryAndRuntime_subsetOfFunctions_success() throws Excepti .addLibraries(CelMathCompilerLibrary.math(CelMathCompilerLibrary.Function.MAX)) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelMathRuntimeLibrary.math(CelMathRuntimeLibrary.Function.MAX) .newFunctionBindings()) diff --git a/extensions/src/test/java/dev/cel/extensions/CelSetsExtensionsTest.java b/extensions/src/test/java/dev/cel/extensions/CelSetsExtensionsTest.java index fdeb45a37..94673e812 100644 --- a/extensions/src/test/java/dev/cel/extensions/CelSetsExtensionsTest.java +++ b/extensions/src/test/java/dev/cel/extensions/CelSetsExtensionsTest.java @@ -485,7 +485,7 @@ public void separateLibraryAndRuntime_versioned_success() throws Exception { .addLibraries(CelSetsCompilerLibrary.sets(0)) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings(CelSetsRuntimeLibrary.sets(0).newFunctionBindings()) .build(); @@ -502,7 +502,7 @@ public void separateLibraryAndRuntime_subsetOfFunctions_success() throws Excepti .addLibraries(CelSetsCompilerLibrary.sets(CelSetsCompilerLibrary.Function.CONTAINS)) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelSetsRuntimeLibrary.sets(CelSetsRuntimeLibrary.Function.CONTAINS) .newFunctionBindings()) diff --git a/extensions/src/test/java/dev/cel/extensions/CelStringExtensionsTest.java b/extensions/src/test/java/dev/cel/extensions/CelStringExtensionsTest.java index 609269874..6e295a353 100644 --- a/extensions/src/test/java/dev/cel/extensions/CelStringExtensionsTest.java +++ b/extensions/src/test/java/dev/cel/extensions/CelStringExtensionsTest.java @@ -1927,7 +1927,7 @@ public void separateLibraryAndRuntime_versioned_success() throws Exception { .addLibraries(CelStringCompilerLibrary.strings(0)) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings(CelStringRuntimeLibrary.strings(0).newFunctionBindings()) .build(); @@ -1945,7 +1945,7 @@ public void separateLibraryAndRuntime_subsetOfFunctions_success() throws Excepti CelStringCompilerLibrary.strings(CelStringCompilerLibrary.Function.LOWER_ASCII)) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.plannerRuntimeBuilder() .addFunctionBindings( CelStringRuntimeLibrary.strings(CelStringRuntimeLibrary.Function.LOWER_ASCII) .newFunctionBindings()) diff --git a/optimizer/src/test/java/dev/cel/optimizer/BUILD.bazel b/optimizer/src/test/java/dev/cel/optimizer/BUILD.bazel index 539f7d341..fb14fd628 100644 --- a/optimizer/src/test/java/dev/cel/optimizer/BUILD.bazel +++ b/optimizer/src/test/java/dev/cel/optimizer/BUILD.bazel @@ -13,8 +13,10 @@ java_library( "//:java_truth", "//bundle:cel", "//common:cel_ast", + "//common:cel_function_decl", + "//common:cel_overload_decl", "//common:cel_source", - "//common:compiler_common", + "//common:cel_validation_exception", "//common:container", "//common:mutable_ast", "//common:options", @@ -23,10 +25,8 @@ java_library( "//common/navigation", "//common/navigation:common", "//common/types", - "//compiler", "//extensions", "//extensions:optional_library", - "//optimizer", "//optimizer:ast_optimizer", "//optimizer:mutable_ast", "//optimizer:optimization_exception", @@ -34,9 +34,7 @@ java_library( "//optimizer:optimizer_impl", "//optimizer:optimizer_listener", "//parser:macro", - "//parser:parser_factory", "//parser:unparser", - "//runtime", "@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto", "@maven//:com_google_guava_guava", "@maven//:com_google_testparameterinjector_test_parameter_injector", diff --git a/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerFactoryTest.java b/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerFactoryTest.java deleted file mode 100644 index 41c7ecd74..000000000 --- a/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerFactoryTest.java +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2023 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.optimizer; - -import static com.google.common.truth.Truth.assertThat; - -import dev.cel.bundle.CelFactory; -import dev.cel.compiler.CelCompilerFactory; -import dev.cel.parser.CelParserFactory; -import dev.cel.runtime.CelRuntimeFactory; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class CelOptimizerFactoryTest { - - @Test - public void standardCelOptimizerBuilder_withParserCheckerAndRuntime() { - CelOptimizerBuilder builder = - CelOptimizerFactory.standardCelOptimizerBuilder( - CelParserFactory.standardCelParserBuilder().build(), - CelCompilerFactory.standardCelCheckerBuilder().build(), - CelRuntimeFactory.standardCelRuntimeBuilder().build()); - - assertThat(builder).isNotNull(); - assertThat(builder.build()).isNotNull(); - } - - @Test - public void standardCelOptimizerBuilder_withCompilerAndRuntime() { - CelOptimizerBuilder builder = - CelOptimizerFactory.standardCelOptimizerBuilder( - CelCompilerFactory.standardCelCompilerBuilder().build(), - CelRuntimeFactory.standardCelRuntimeBuilder().build()); - - assertThat(builder).isNotNull(); - assertThat(builder.build()).isNotNull(); - } - - @Test - public void standardCelOptimizerBuilder_withCel() { - CelOptimizerBuilder builder = - CelOptimizerFactory.standardCelOptimizerBuilder(CelFactory.standardCelBuilder().build()); - - assertThat(builder).isNotNull(); - assertThat(builder.build()).isNotNull(); - } -} diff --git a/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerImplTest.java b/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerImplTest.java index 0867ac0e0..6743bf0f7 100644 --- a/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerImplTest.java +++ b/optimizer/src/test/java/dev/cel/optimizer/CelOptimizerImplTest.java @@ -39,8 +39,12 @@ public class CelOptimizerImplTest { private static final Cel CEL = - CelFactory.standardCelBuilder() - .setOptions(CelOptions.current().populateMacroCalls(true).build()) + CelFactory.plannerCelBuilder() + .setOptions( + CelOptions.current() + .populateMacroCalls(true) + .enableHeterogeneousNumericComparisons(true) + .build()) .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .build(); @@ -79,20 +83,6 @@ public void onPassFailure( } }; - @Test - public void constructCelOptimizer_success() { - CelOptimizer celOptimizer = - CelOptimizerImpl.newBuilder(CEL) - .addAstOptimizers( - (ast, cel) -> - // no-op - OptimizationResult.create(ast)) - .build(); - - assertThat(celOptimizer).isNotNull(); - assertThat(celOptimizer).isInstanceOf(CelOptimizerImpl.class); - } - @Test public void astOptimizers_invokedInOrder() throws Exception { List list = new ArrayList<>(); diff --git a/optimizer/src/test/java/dev/cel/optimizer/optimizers/InliningOptimizerTest.java b/optimizer/src/test/java/dev/cel/optimizer/optimizers/InliningOptimizerTest.java index da2e9b745..8712cbebc 100644 --- a/optimizer/src/test/java/dev/cel/optimizer/optimizers/InliningOptimizerTest.java +++ b/optimizer/src/test/java/dev/cel/optimizer/optimizers/InliningOptimizerTest.java @@ -45,7 +45,7 @@ public class InliningOptimizerTest { private static final Cel CEL = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .setContainer(CelContainer.ofName("google.expr.proto3.test")) .addFileTypes(TestAllTypes.getDescriptor().getFile()) @@ -58,7 +58,11 @@ public class InliningOptimizerTest { "child", StructTypeReference.create(TestAllTypes.NestedMessage.getDescriptor().getFullName())) .addVar("shadowed_ident", SimpleType.INT) - .setOptions(CelOptions.current().populateMacroCalls(true).build()) + .setOptions( + CelOptions.current() + .populateMacroCalls(true) + .enableHeterogeneousNumericComparisons(true) + .build()) .build(); @Test diff --git a/optimizer/src/test/java/dev/cel/optimizer/optimizers/SubexpressionOptimizerTest.java b/optimizer/src/test/java/dev/cel/optimizer/optimizers/SubexpressionOptimizerTest.java index c3e705f84..52bc8db92 100644 --- a/optimizer/src/test/java/dev/cel/optimizer/optimizers/SubexpressionOptimizerTest.java +++ b/optimizer/src/test/java/dev/cel/optimizer/optimizers/SubexpressionOptimizerTest.java @@ -25,7 +25,6 @@ import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.bundle.Cel; import dev.cel.bundle.CelBuilder; -import dev.cel.bundle.CelFactory; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelFunctionDecl; import dev.cel.common.CelMutableAst; @@ -55,7 +54,6 @@ import dev.cel.runtime.CelEvaluationException; import dev.cel.runtime.CelFunctionBinding; import dev.cel.runtime.CelRuntime; -import dev.cel.runtime.CelRuntimeFactory; import dev.cel.runtime.CelUnknownSet; import dev.cel.runtime.PartialVars; import dev.cel.runtime.Program; @@ -137,20 +135,6 @@ public void setUp() { private static final CelUnparser CEL_UNPARSER = CelUnparserFactory.newUnparser(); - private static CelBuilder newCelBuilder() { - return CelFactory.standardCelBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) - .setStandardMacros(CelStandardMacro.STANDARD_MACROS) - .setOptions(CelOptions.current().populateMacroCalls(true).build()) - .addCompilerLibraries(CelExtensions.bindings()) - .addFunctionDeclarations( - CelFunctionDecl.newFunctionDeclaration( - "non_pure_custom_func", - newGlobalOverload("non_pure_custom_func_overload", SimpleType.INT, SimpleType.INT))) - .addVar("x", SimpleType.DYN) - .addVar("msg", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); - } - private CelOptimizer newCseOptimizer(SubexpressionOptimizerOptions options) { return CelOptimizerFactory.standardCelOptimizerBuilder(cel) .addAstOptimizers(SubexpressionOptimizer.newInstance(options)) @@ -159,7 +143,7 @@ private CelOptimizer newCseOptimizer(SubexpressionOptimizerOptions options) { @Test public void cse_resultTypeSet_celBlockOptimizationSuccess() throws Exception { - Cel cel = newCelBuilder().setResultType(SimpleType.BOOL).build(); + Cel cel = this.cel.toCelBuilder().setResultType(SimpleType.BOOL).build(); CelOptimizer celOptimizer = CelOptimizerFactory.standardCelOptimizerBuilder(cel) .addAstOptimizers( @@ -423,8 +407,8 @@ public void block_success_parsedOnly(@TestParameter BlockTestCase testCase) thro public void lazyEval_blockIndexNeverReferenced() throws Exception { AtomicInteger invocation = new AtomicInteger(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) + celForEvaluatingBlock + .toRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "get_true_overload", @@ -453,8 +437,8 @@ public void lazyEval_blockIndexNeverReferenced() throws Exception { public void lazyEval_blockIndexEvaluatedOnlyOnce() throws Exception { AtomicInteger invocation = new AtomicInteger(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) + celForEvaluatingBlock + .toRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "get_true_overload", @@ -478,8 +462,8 @@ public void lazyEval_blockIndexEvaluatedOnlyOnce() throws Exception { public void lazyEval_withinComprehension_blockIndexEvaluatedOnlyOnce() throws Exception { AtomicInteger invocation = new AtomicInteger(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) + celForEvaluatingBlock + .toRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "get_true_overload", @@ -503,8 +487,8 @@ public void lazyEval_withinComprehension_blockIndexEvaluatedOnlyOnce() throws Ex public void lazyEval_multipleBlockIndices_inResultExpr() throws Exception { AtomicInteger invocation = new AtomicInteger(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) + celForEvaluatingBlock + .toRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "get_true_overload", @@ -530,8 +514,8 @@ public void lazyEval_multipleBlockIndices_inResultExpr() throws Exception { public void lazyEval_multipleBlockIndices_cascaded() throws Exception { AtomicInteger invocation = new AtomicInteger(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) + celForEvaluatingBlock + .toRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "get_true_overload", @@ -555,8 +539,8 @@ public void lazyEval_multipleBlockIndices_cascaded() throws Exception { public void lazyEval_nestedComprehension_indexReferencedInNestedScopes() throws Exception { AtomicInteger invocation = new AtomicInteger(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .addMessageTypes(TestAllTypes.getDescriptor()) + celForEvaluatingBlock + .toRuntimeBuilder() .addFunctionBindings( CelFunctionBinding.from( "get_true_overload", @@ -570,8 +554,8 @@ public void lazyEval_nestedComprehension_indexReferencedInNestedScopes() throws CelAbstractSyntaxTree ast = compileUsingInternalFunctions( "cel.block([true, false, get_true()], [index2, false, index2].map(c0, [c0].map(c1, [c0," - + " c1, index2]))) == [[[true, true, true]], [[false, false, true]], [[true, true," - + " true]]]"); + + " c1, index2])) == [[[true, true, true]], [[false, false, true]], [[true, true," + + " true]]])"); boolean result = (boolean) celRuntime.createProgram(ast).eval(); @@ -709,7 +693,11 @@ public void cse_nestedMacro_noOp_assertAstIdCorrectness() throws Exception { .builder() .addVar("x", SimpleType.DYN) .setStandardMacros(CelStandardMacro.STANDARD_MACROS) - .setOptions(CelOptions.current().populateMacroCalls(true).build()) + .setOptions( + CelOptions.current() + .populateMacroCalls(true) + .enableHeterogeneousNumericComparisons(true) + .build()) .addCompilerLibraries(CelExtensions.comprehensions()) .addRuntimeLibraries(CelExtensions.comprehensions()) .build(); diff --git a/parser/src/test/java/dev/cel/parser/BUILD.bazel b/parser/src/test/java/dev/cel/parser/BUILD.bazel index db5a647b7..d5de43d06 100644 --- a/parser/src/test/java/dev/cel/parser/BUILD.bazel +++ b/parser/src/test/java/dev/cel/parser/BUILD.bazel @@ -39,7 +39,6 @@ java_library( "//parser:lite_parser_factory", "//parser:macro", "//parser:parser_builder", - "//parser:parser_factory", "//parser:pratt_parser", "//parser:unparser", "//parser:unparser_visitor", diff --git a/parser/src/test/java/dev/cel/parser/CelParserFactoryTest.java b/parser/src/test/java/dev/cel/parser/CelParserFactoryTest.java deleted file mode 100644 index f6efaa68a..000000000 --- a/parser/src/test/java/dev/cel/parser/CelParserFactoryTest.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.parser; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public final class CelParserFactoryTest { - - @Test - public void standardCelParserBuilder() { - assertThat(CelParserFactory.standardCelParserBuilder().build()).isNotNull(); - } -} diff --git a/policy/src/test/java/dev/cel/policy/BUILD.bazel b/policy/src/test/java/dev/cel/policy/BUILD.bazel index 6a76cf3b0..4b8d757db 100644 --- a/policy/src/test/java/dev/cel/policy/BUILD.bazel +++ b/policy/src/test/java/dev/cel/policy/BUILD.bazel @@ -26,10 +26,8 @@ java_library( "//common/internal", "//common/resources/testdata/proto3:standalone_global_enum_java_proto", "//common/types", - "//compiler", "//extensions:optional_library", "//parser:macro", - "//parser:parser_factory", "//parser:unparser", "//policy", "//policy:compiled_rule", diff --git a/policy/src/test/java/dev/cel/policy/CelPolicyCompilerFactoryTest.java b/policy/src/test/java/dev/cel/policy/CelPolicyCompilerFactoryTest.java deleted file mode 100644 index 1f323256b..000000000 --- a/policy/src/test/java/dev/cel/policy/CelPolicyCompilerFactoryTest.java +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2024 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.policy; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.testing.junit.testparameterinjector.TestParameterInjector; -import dev.cel.compiler.CelCompilerFactory; -import dev.cel.parser.CelParserFactory; -import dev.cel.runtime.CelRuntimeFactory; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(TestParameterInjector.class) -public final class CelPolicyCompilerFactoryTest { - - @Test - public void newPolicyCompiler_compilerRuntimeCombined() { - assertThat( - CelPolicyCompilerFactory.newPolicyCompiler( - CelCompilerFactory.standardCelCompilerBuilder().build(), - CelRuntimeFactory.standardCelRuntimeBuilder().build())) - .isNotNull(); - } - - @Test - public void newPolicyCompiler_parserCheckerRuntimeCombined() { - assertThat( - CelPolicyCompilerFactory.newPolicyCompiler( - CelParserFactory.standardCelParserBuilder().build(), - CelCompilerFactory.standardCelCheckerBuilder().build(), - CelRuntimeFactory.standardCelRuntimeBuilder().build())) - .isNotNull(); - } -} diff --git a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel index c6a771f4e..3146e5a8b 100644 --- a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel @@ -33,6 +33,8 @@ java_library( "//common:cel_descriptor_util", "//common:cel_descriptors", "//common:cel_exception", + "//common:cel_function_decl", + "//common:cel_overload_decl", "//common:cel_source", "//common:compiler_common", "//common:container", @@ -168,7 +170,7 @@ java_library( deps = [ ":abstract_planner_interpreter_test", "//common:cel_ast", - "//common:compiler_common", + "//common:cel_validation_exception", "//common:container", "//common:options", "//common/types:type_providers", diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java index cb5bb24f7..a112bde6a 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java @@ -257,7 +257,7 @@ public final class CelLiteRuntimeVersionSkewTest { public void setUp() { // Server (schema V2): compiles and optimizes expressions against the newest descriptors. serverCompiler = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setOptions(CEL_OPTIONS) .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .addCompilerLibraries(CelExtensions.bindings()) @@ -2150,7 +2150,7 @@ private static Object evalProto2( String expression, dev.cel.expr.conformance.proto2.TestAllTypes proto2Message) throws Exception { Cel proto2Cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setOptions(CEL_OPTIONS) .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .addMessageTypes(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor()) diff --git a/runtime/src/test/java/dev/cel/runtime/CelRuntimeFactoryTest.java b/runtime/src/test/java/dev/cel/runtime/CelRuntimeFactoryTest.java deleted file mode 100644 index 28676ebcb..000000000 --- a/runtime/src/test/java/dev/cel/runtime/CelRuntimeFactoryTest.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.runtime; - -import static com.google.common.truth.Truth.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public final class CelRuntimeFactoryTest { - - @Test - public void standardCelRuntimeBuilder() { - assertThat(CelRuntimeFactory.standardCelRuntimeBuilder().build()).isNotNull(); - } -} diff --git a/runtime/src/test/java/dev/cel/runtime/CelRuntimeLegacyImplTest.java b/runtime/src/test/java/dev/cel/runtime/CelRuntimeLegacyImplTest.java index 5bef0c61e..7efb8d929 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelRuntimeLegacyImplTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelRuntimeLegacyImplTest.java @@ -20,7 +20,12 @@ import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.protobuf.Any; +import com.google.protobuf.BoolValue; +import com.google.protobuf.DescriptorProtos.FileDescriptorSet; +import com.google.protobuf.DynamicMessage; import com.google.protobuf.Message; +import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelException; import dev.cel.common.exceptions.CelDivideByZeroException; import dev.cel.compiler.CelCompiler; @@ -39,15 +44,82 @@ public final class CelRuntimeLegacyImplTest { @Test public void evalException() throws CelException { CelCompiler compiler = CelCompilerFactory.standardCelCompilerBuilder().build(); - CelRuntime runtime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime runtime = CelRuntimeFactory.legacyCelRuntimeBuilder().build(); CelRuntime.Program program = runtime.createProgram(compiler.compile("1/0").getAst()); CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval); assertThat(e).hasCauseThat().isInstanceOf(CelDivideByZeroException.class); } + @Test + public void newWellKnownTypeMessage_inDynamicMessage_withSetTypeFactory() throws Exception { + CelCompiler celCompiler = + CelCompilerFactory.standardCelCompilerBuilder() + .addMessageTypes(BoolValue.getDescriptor()) + .build(); + CelRuntime celRuntime = + CelRuntimeFactory.legacyCelRuntimeBuilder() + // CEL-Internal-2 + .setTypeFactory( + (typeName) -> + typeName.equals("google.protobuf.BoolValue") + ? DynamicMessage.newBuilder(BoolValue.getDescriptor()) + : null) + .build(); + + CelAbstractSyntaxTree ast = + celCompiler.compile("google.protobuf.BoolValue{value: false}").getAst(); + + assertThat(celRuntime.createProgram(ast).eval()).isEqualTo(false); + } + + @Test + public void newWellKnownTypeMessage_inAnyMessage_withSetTypeFactory() throws Exception { + FileDescriptorSet fds = + FileDescriptorSet.newBuilder() + // Copy the WKT descriptors + .addFile(Any.getDescriptor().getFile().toProto()) + .addFile(BoolValue.getDescriptor().getFile().toProto()) + .build(); + CelCompiler celCompiler = + CelCompilerFactory.standardCelCompilerBuilder().addFileTypes(fds).build(); + CelRuntime celRuntime = + CelRuntimeFactory.legacyCelRuntimeBuilder() + // CEL-Internal-2 + .addFileTypes(fds) + .setTypeFactory( + (typeName) -> + typeName.equals("google.protobuf.Any") + ? Any.newBuilder().setTypeUrl("google.protobuf.DoubleValue") + : null) + .build(); + + CelAbstractSyntaxTree ast = + celCompiler + .compile( + "google.protobuf.Any{type_url: 'types.googleapis.com/google.protobuf.DoubleValue'}") + .getAst(); + + assertThat(celRuntime.createProgram(ast).eval()).isEqualTo(0.0d); + } + + @Test + public void standardEnvironmentDisabledForRuntime_throws() throws Exception { + CelCompiler celCompiler = + CelCompilerFactory.standardCelCompilerBuilder().setStandardEnvironmentEnabled(true).build(); + CelRuntime celRuntime = + CelRuntimeFactory.legacyCelRuntimeBuilder().setStandardEnvironmentEnabled(false).build(); + CelAbstractSyntaxTree ast = celCompiler.compile("size('hello')").getAst(); + CelRuntime.Program program = celRuntime.createProgram(ast); + + CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval); + assertThat(e) + .hasMessageThat() + .contains("No matching overload for function 'size'. Overload candidates: size_string"); + } + @Test public void toRuntimeBuilder_isNewInstance() { - CelRuntimeBuilder celRuntimeBuilder = CelRuntimeFactory.standardCelRuntimeBuilder(); + CelRuntimeBuilder celRuntimeBuilder = CelRuntimeFactory.legacyCelRuntimeBuilder(); CelRuntimeLegacyImpl celRuntime = (CelRuntimeLegacyImpl) celRuntimeBuilder.build(); CelRuntimeLegacyImpl.Builder newRuntimeBuilder = @@ -58,7 +130,7 @@ public void toRuntimeBuilder_isNewInstance() { @Test public void toRuntimeBuilder_isImmutable() { - CelRuntimeBuilder originalRuntimeBuilder = CelRuntimeFactory.standardCelRuntimeBuilder(); + CelRuntimeBuilder originalRuntimeBuilder = CelRuntimeFactory.legacyCelRuntimeBuilder(); CelRuntimeLegacyImpl celRuntime = (CelRuntimeLegacyImpl) originalRuntimeBuilder.build(); originalRuntimeBuilder.addLibraries(runtimeBuilder -> {}); @@ -70,7 +142,7 @@ public void toRuntimeBuilder_isImmutable() { @Test public void toRuntimeBuilder_collectionProperties_copied() { - CelRuntimeBuilder celRuntimeBuilder = CelRuntimeFactory.standardCelRuntimeBuilder(); + CelRuntimeBuilder celRuntimeBuilder = CelRuntimeFactory.legacyCelRuntimeBuilder(); celRuntimeBuilder.addMessageTypes(TestAllTypes.getDescriptor()); celRuntimeBuilder.addFileTypes(TestAllTypes.getDescriptor().getFile()); celRuntimeBuilder.addFunctionBindings(CelFunctionBinding.from("test", Integer.class, arg -> 1)); @@ -89,7 +161,7 @@ public void toRuntimeBuilder_collectionProperties_copied() { @Test public void toRuntimeBuilder_collectionProperties_areImmutable() { - CelRuntimeBuilder celRuntimeBuilder = CelRuntimeFactory.standardCelRuntimeBuilder(); + CelRuntimeBuilder celRuntimeBuilder = CelRuntimeFactory.legacyCelRuntimeBuilder(); CelRuntimeLegacyImpl celRuntime = (CelRuntimeLegacyImpl) celRuntimeBuilder.build(); CelRuntimeLegacyImpl.Builder newRuntimeBuilder = (CelRuntimeLegacyImpl.Builder) celRuntime.toRuntimeBuilder(); @@ -111,7 +183,7 @@ public void toRuntimeBuilder_optionalProperties() { CelStandardFunctions overriddenStandardFunctions = CelStandardFunctions.newBuilder().includeFunctions(StandardFunction.ADD).build(); CelRuntimeBuilder celRuntimeBuilder = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .setStandardEnvironmentEnabled(false) .setTypeFactory(customTypeFactory) .setStandardFunctions(overriddenStandardFunctions); @@ -131,7 +203,7 @@ public void toRuntimeBuilder_asyncProperties_copied() { CelAsyncEvaluationOptions options = CelAsyncEvaluationOptions.newBuilder().setMaxConcurrency(5).build(); CelRuntimeBuilder celRuntimeBuilder = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .setAsyncEvaluationOptions(options) .setAsyncExecutor(executor); CelRuntime celRuntime = celRuntimeBuilder.build(); @@ -146,7 +218,7 @@ public void toRuntimeBuilder_asyncProperties_copied() { @Test public void evalAsync_legacyInterpreter_throwsUnsupportedOperationException() throws Exception { CelCompiler compiler = CelCompilerFactory.standardCelCompilerBuilder().build(); - CelRuntime runtime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + CelRuntime runtime = CelRuntimeFactory.legacyCelRuntimeBuilder().build(); CelRuntime.Program program = runtime.createProgram(compiler.compile("1 + 1").getAst()); CelVariableResolver resolver = name -> Optional.of(1L); diff --git a/runtime/src/test/java/dev/cel/runtime/CelRuntimeTest.java b/runtime/src/test/java/dev/cel/runtime/CelRuntimeTest.java index ba8326d27..6c209c150 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelRuntimeTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelRuntimeTest.java @@ -27,13 +27,11 @@ import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.DescriptorProtos.FileDescriptorSet; -import com.google.protobuf.DynamicMessage; import com.google.rpc.context.AttributeContext; import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.bundle.Cel; -import dev.cel.bundle.CelFactory; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelContainer; import dev.cel.common.CelErrorCode; @@ -71,8 +69,13 @@ public class CelRuntimeTest { @Test public void evaluate_anyPackedEqualityUsingProtoDifferencer_success() throws Exception { Cel cel = - CelFactory.standardCelBuilder() - .setOptions(CelOptions.current().enableProtoDifferencerEquality(true).build()) + runtimeFlavor + .builder() + .setOptions( + CelOptions.current() + .enableHeterogeneousNumericComparisons(true) + .enableProtoDifferencerEquality(true) + .build()) .addVar("a", StructTypeReference.create(AttributeContext.getDescriptor().getFullName())) .addVar("b", StructTypeReference.create(AttributeContext.getDescriptor().getFullName())) .addMessageTypes(AttributeContext.getDescriptor()) @@ -116,10 +119,9 @@ public void evaluate_v1alpha1CheckedExpr() throws Exception { .build()) .putTypeMap(1, CelV1AlphaTypes.create(PrimitiveType.STRING)) .build(); - CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); + Cel cel = runtimeFlavor.builder().build(); CelRuntime.Program program = - celRuntime.createProgram( - CelProtoV1Alpha1AbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst()); + cel.createProgram(CelProtoV1Alpha1AbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst()); String evaluatedResult = (String) program.eval(); @@ -234,8 +236,11 @@ public void newWellKnownTypeMessage_withDifferentDescriptorInstance() throws Exc CelCompilerFactory.standardCelCompilerBuilder() .addMessageTypes(BoolValue.getDescriptor()) .build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + // TODO: CelRuntimeImpl.Builder.build() mutates valueProvider/typeProvider in + // place, leaking LinkedDescriptorPool.INSTANCE across toRuntimeBuilder(). + Cel cel = + runtimeFlavor + .builder() // CEL-Internal-2 .addFileTypes( FileDescriptorSet.newBuilder() @@ -247,29 +252,7 @@ public void newWellKnownTypeMessage_withDifferentDescriptorInstance() throws Exc CelAbstractSyntaxTree ast = celCompiler.compile("google.protobuf.BoolValue{value: false}").getAst(); - assertThat(celRuntime.createProgram(ast).eval()).isEqualTo(false); - } - - @Test - public void newWellKnownTypeMessage_inDynamicMessage_withSetTypeFactory() throws Exception { - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder() - .addMessageTypes(BoolValue.getDescriptor()) - .build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - // CEL-Internal-2 - .setTypeFactory( - (typeName) -> - typeName.equals("google.protobuf.BoolValue") - ? DynamicMessage.newBuilder(BoolValue.getDescriptor()) - : null) - .build(); - - CelAbstractSyntaxTree ast = - celCompiler.compile("google.protobuf.BoolValue{value: false}").getAst(); - - assertThat(celRuntime.createProgram(ast).eval()).isEqualTo(false); + assertThat(cel.createProgram(ast).eval()).isEqualTo(false); } @Test @@ -281,51 +264,19 @@ public void newWellKnownTypeMessage_inAnyMessage_withDifferentDescriptorInstance .addFile(Any.getDescriptor().getFile().toProto()) .addFile(BoolValue.getDescriptor().getFile().toProto()) .build(); - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder().addFileTypes(fds).build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - // CEL-Internal-2 - .addFileTypes(fds) - .build(); - - CelAbstractSyntaxTree ast = - celCompiler - .compile( - "google.protobuf.Any{type_url: 'types.googleapis.com/google.protobuf.DoubleValue'}") - .getAst(); - - assertThat(celRuntime.createProgram(ast).eval()).isEqualTo(0.0d); - } - - @Test - public void newWellKnownTypeMessage_inAnyMessage_withSetTypeFactory() throws Exception { - FileDescriptorSet fds = - FileDescriptorSet.newBuilder() - // Copy the WKT descriptors - .addFile(Any.getDescriptor().getFile().toProto()) - .addFile(BoolValue.getDescriptor().getFile().toProto()) - .build(); - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder().addFileTypes(fds).build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + Cel cel = + runtimeFlavor + .builder() // CEL-Internal-2 .addFileTypes(fds) - .setTypeFactory( - (typeName) -> - typeName.equals("google.protobuf.Any") - ? Any.newBuilder().setTypeUrl("google.protobuf.DoubleValue") - : null) .build(); CelAbstractSyntaxTree ast = - celCompiler - .compile( + cel.compile( "google.protobuf.Any{type_url: 'types.googleapis.com/google.protobuf.DoubleValue'}") .getAst(); - assertThat(celRuntime.createProgram(ast).eval()).isEqualTo(0.0d); + assertThat(cel.createProgram(ast).eval()).isEqualTo(0.0d); } @Test @@ -908,21 +859,6 @@ public void trace_shortCircuitingDisabled_ternaryWithCustomError() throws Except assertThat(e).hasCauseThat().hasMessageThat().contains("custom error"); } - @Test - public void standardEnvironmentDisabledForRuntime_throws() throws Exception { - CelCompiler celCompiler = - CelCompilerFactory.standardCelCompilerBuilder().setStandardEnvironmentEnabled(true).build(); - CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder().setStandardEnvironmentEnabled(false).build(); - CelAbstractSyntaxTree ast = celCompiler.compile("size('hello')").getAst(); - CelRuntime.Program program = celRuntime.createProgram(ast); - - CelEvaluationException e = assertThrows(CelEvaluationException.class, () -> program.eval()); - assertThat(e) - .hasMessageThat() - .contains("No matching overload for function 'size'. Overload candidates: size_string"); - } - @Test public void trace_shortCircuitingDisabled_logicalAndPrefersFirstError() throws Exception { Cel cel = diff --git a/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java b/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java index f452b99dc..098555b90 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelStandardFunctionsTest.java @@ -65,7 +65,7 @@ public void runtime_standardEnvironmentEnabled_throwsWhenOverridingFunctions() { assertThrows( IllegalArgumentException.class, () -> - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .setStandardEnvironmentEnabled(true) .setStandardFunctions( CelStandardFunctions.newBuilder() @@ -145,8 +145,7 @@ public void standardFunctions_empty() { public void standardEnvironment_subsetEnvironment() throws Exception { CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() - .setStandardEnvironmentEnabled(false) + CelRuntimeFactory.plannerRuntimeBuilder() .setStandardFunctions( CelStandardFunctions.newBuilder() .includeFunctions(StandardFunction.ADD, StandardFunction.SUBTRACT) @@ -194,7 +193,7 @@ public void heterogeneousEqualityDisabled_mixedTypeComparisons_throws(String exp .setOptions(CelOptions.current().enableHeterogeneousNumericComparisons(true).build()) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .setOptions(CelOptions.current().enableHeterogeneousNumericComparisons(false).build()) .build(); @@ -212,7 +211,7 @@ public void unsignedLongsDisabled_int64Identity_throws() { .setOptions(CelOptions.current().enableUnsignedLongs(true).build()) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .setOptions(CelOptions.current().enableUnsignedLongs(false).build()) .build(); @@ -232,7 +231,7 @@ public void timestampEpochDisabled_int64Identity_throws() { .setOptions(CelOptions.current().build()) .build(); CelRuntime celRuntime = - CelRuntimeFactory.standardCelRuntimeBuilder() + CelRuntimeFactory.legacyCelRuntimeBuilder() .setOptions(CelOptions.current().enableTimestampEpoch(false).build()) .build(); diff --git a/runtime/src/test/java/dev/cel/runtime/DescriptorTypeResolverTest.java b/runtime/src/test/java/dev/cel/runtime/DescriptorTypeResolverTest.java index bdd865601..42f4dddc9 100644 --- a/runtime/src/test/java/dev/cel/runtime/DescriptorTypeResolverTest.java +++ b/runtime/src/test/java/dev/cel/runtime/DescriptorTypeResolverTest.java @@ -28,10 +28,11 @@ import dev.cel.common.types.OptionalType; import dev.cel.common.types.ProtoMessageTypeProvider; import dev.cel.common.types.SimpleType; -import dev.cel.common.types.StructTypeReference; import dev.cel.common.types.TypeType; +import dev.cel.common.values.OpaqueValue; import dev.cel.expr.conformance.proto3.TestAllTypes; import dev.cel.extensions.CelOptionalLibrary; +import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,11 +43,18 @@ public class DescriptorTypeResolverTest { new ProtoMessageTypeProvider(ImmutableList.of(TestAllTypes.getDescriptor())); private static final Cel CEL = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .setTypeProvider(PROTO_MESSAGE_TYPE_PROVIDER) + // TODO: Replace setValueProvider with + // addMessageTypes(TestAllTypes.getDescriptor()) once CelRuntimeImpl prioritizes custom + // CelTypeProvider over its internal messageTypeProvider. + .setValueProvider( + (structType, fields) -> + structType.equals(TestAllTypes.getDescriptor().getFullName()) + ? Optional.of(TestAllTypes.getDefaultInstance()) + : Optional.empty()) .addCompilerLibraries(CelOptionalLibrary.INSTANCE) .addRuntimeLibraries(CelOptionalLibrary.INSTANCE) - .addMessageTypes(TestAllTypes.getDescriptor()) .setContainer(CelContainer.ofName(TestAllTypes.getDescriptor().getFullName())) .build(); @@ -65,7 +73,10 @@ private enum TypeLiteralTestCase { OPTIONAL_TYPE("optional_type", TypeType.create(OptionalType.create(SimpleType.DYN))), PROTO_MESSAGE_TYPE( "TestAllTypes", - TypeType.create(StructTypeReference.create(TestAllTypes.getDescriptor().getFullName()))); + TypeType.create( + PROTO_MESSAGE_TYPE_PROVIDER + .findType(TestAllTypes.getDescriptor().getFullName()) + .get())); private final String expression; @@ -79,9 +90,6 @@ private enum TypeLiteralTestCase { @Test public void typeLiteral_success(@TestParameter TypeLiteralTestCase testCase) throws Exception { - if (!testCase.equals(TypeLiteralTestCase.DURATION)) { - return; - } CelAbstractSyntaxTree ast = CEL.compile(testCase.expression).getAst(); assertThat(CEL.createProgram(ast).eval()).isEqualTo(testCase.celRuntimeType); @@ -118,7 +126,10 @@ private enum TypeCallTestCase { UINT("1u", TypeType.create(SimpleType.UINT)), PROTO_MESSAGE( "TestAllTypes{}", - TypeType.create(StructTypeReference.create(TestAllTypes.getDescriptor().getFullName()))), + TypeType.create( + PROTO_MESSAGE_TYPE_PROVIDER + .findType(TestAllTypes.getDescriptor().getFullName()) + .get())), OPTIONAL_TYPE("optional.of(1)", TypeType.create(OptionalType.create(SimpleType.DYN))); private final String expression; @@ -161,9 +172,19 @@ public void typeCall_opaqueVar() throws Exception { OpaqueType opaqueType = OpaqueType.create("opaque_type"); Cel cel = CEL.toCelBuilder().addVar("opaque_var", opaqueType).build(); CelAbstractSyntaxTree ast = cel.compile("type(opaque_var)").getAst(); - final class CustomClass {} + final class CustomClass extends OpaqueValue { + @Override + public Object value() { + return this; + } + + @Override + public OpaqueType celType() { + return opaqueType; + } + } - assertThat(CEL.createProgram(ast).eval(ImmutableMap.of("opaque_var", new CustomClass()))) + assertThat(cel.createProgram(ast).eval(ImmutableMap.of("opaque_var", new CustomClass()))) .isEqualTo(TypeType.create(opaqueType)); } } diff --git a/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java b/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java index d24e99860..59a0703d7 100644 --- a/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java +++ b/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java @@ -62,7 +62,7 @@ public void asyncProgram_basicUnknownResolution() throws Exception { return attr.toString(); }); Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) @@ -113,7 +113,7 @@ public void asyncProgram_sequentialUnknownResolution() throws Exception { return attr.toString(); }); Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.BOOL) @@ -161,7 +161,7 @@ public void asyncProgram_basicAsyncResolver() throws Exception { SettableFuture var3 = SettableFuture.create(); Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) @@ -213,7 +213,7 @@ public void asyncProgram_honorsCancellation() throws Exception { SettableFuture var3 = SettableFuture.create(); Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) @@ -259,12 +259,11 @@ interface ResolverFactory { @Test public void asyncProgram_concurrency( - @TestParameter(valuesProvider = RepeatedTestProvider.class) int testRunIndex) - throws Exception { + @TestParameter(valuesProvider = RepeatedTestProvider.class) int unused) throws Exception { Duration taskDelay = Duration.ofMillis(500); // Arrange Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) @@ -317,7 +316,7 @@ public void asyncProgram_concurrency( public void asyncProgram_elementResolver() throws Exception { // Arrange Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar( @@ -366,7 +365,7 @@ public void asyncProgram_elementResolver() throws Exception { public void asyncProgram_thrownExceptionPropagatesImmediately() throws Exception { // Arrange Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) @@ -422,7 +421,7 @@ public void asyncProgram_thrownExceptionPropagatesImmediately() throws Exception public void asyncProgram_returnedExceptionPropagatesToEvaluator() throws Exception { // Arrange Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) @@ -477,7 +476,7 @@ public void asyncProgram_returnedExceptionPropagatesToEvaluator() throws Excepti public void asyncProgram_returnedExceptionPropagatesToEvaluatorIsPruneable() throws Exception { // Arrange Cel cel = - CelFactory.standardCelBuilder() + CelFactory.legacyCelBuilder() .setOptions(CelOptions.current().enableUnknownTracking(true).build()) .addMessageTypes(TestAllTypes.getDescriptor()) .addVar("com.google.var1", SimpleType.STRING) diff --git a/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java b/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java index caa8c9ad3..2a1b647b9 100644 --- a/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java +++ b/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java @@ -82,7 +82,8 @@ @RunWith(TestParameterInjector.class) public final class OptimizedSelectPlannerTest { - private static final CelOptions CEL_OPTIONS = CelOptions.current().build(); + private static final CelOptions CEL_OPTIONS = + CelOptions.current().enableHeterogeneousNumericComparisons(true).build(); private static final RuntimeEquality RUNTIME_EQUALITY = RuntimeEquality.create(RuntimeHelpers.create(), CEL_OPTIONS); private static final CelTypeProvider TYPE_PROVIDER = @@ -119,7 +120,7 @@ public final class OptimizedSelectPlannerTest { /* asyncExecutor= */ null); private static final Cel CEL = - CelFactory.legacyCelBuilder() + CelFactory.plannerCelBuilder() .setOptions(CEL_OPTIONS) .setStandardMacros(CelStandardMacro.STANDARD_MACROS) .addVar("msg", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())) diff --git a/testing/src/main/java/dev/cel/testing/testrunner/CelTestContext.java b/testing/src/main/java/dev/cel/testing/testrunner/CelTestContext.java index 6ef988a44..86e775178 100644 --- a/testing/src/main/java/dev/cel/testing/testrunner/CelTestContext.java +++ b/testing/src/main/java/dev/cel/testing/testrunner/CelTestContext.java @@ -42,7 +42,7 @@ @AutoValue public abstract class CelTestContext { - private static final Cel DEFAULT_CEL = CelFactory.standardCelBuilder().build(); + private static final Cel DEFAULT_CEL = CelFactory.plannerCelBuilder().build(); /** * The CEL environment for the CEL test. @@ -166,7 +166,7 @@ public Optional typeRegistry() { public static CelTestContext.Builder newBuilder() { return new AutoValue_CelTestContext.Builder() .setCel(DEFAULT_CEL) - .setCelOptions(CelOptions.DEFAULT) + .setCelOptions(CelOptions.current().enableHeterogeneousNumericComparisons(true).build()) .setVariableBindings(ImmutableMap.of()) .setResultMatcher(new DefaultResultMatcher()); } diff --git a/testing/src/main/java/dev/cel/testing/testrunner/TestRunnerLibrary.java b/testing/src/main/java/dev/cel/testing/testrunner/TestRunnerLibrary.java index 1d3e49fbe..3a705412c 100644 --- a/testing/src/main/java/dev/cel/testing/testrunner/TestRunnerLibrary.java +++ b/testing/src/main/java/dev/cel/testing/testrunner/TestRunnerLibrary.java @@ -254,11 +254,11 @@ private static void evaluate( @Nullable CelCoverageIndex celCoverageIndex) throws Exception { Cel cel = celTestContext.cel(); - Program program = cel.createProgram(ast); ExprValue exprValue = null; CelEvaluationException error = null; Object evaluationResult = null; try { + Program program = cel.createProgram(ast); evaluationResult = getEvaluationResult(testCase, celTestContext, program, celCoverageIndex); exprValue = toExprValue(evaluationResult, ast.getResultType()); } catch (CelEvaluationException e) { diff --git a/testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel b/testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel index 9141832cb..9dc8aaea8 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel +++ b/testing/src/test/java/dev/cel/testing/testrunner/BUILD.bazel @@ -39,6 +39,7 @@ java_library( name = "late_function_binding_user_test", srcs = ["LateFunctionBindingUserTest.java"], deps = [ + "//bundle:cel", "//runtime", "//runtime:function_binding", "//testing/testrunner:cel_test_context", diff --git a/testing/src/test/java/dev/cel/testing/testrunner/CelCoverageIndexTest.java b/testing/src/test/java/dev/cel/testing/testrunner/CelCoverageIndexTest.java index 4c6a6cf26..a13d76914 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/CelCoverageIndexTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/CelCoverageIndexTest.java @@ -40,7 +40,7 @@ public final class CelCoverageIndexTest { @Test public void getCoverageReport_fullCoverage() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("x", SimpleType.INT) .addVar("y", SimpleType.INT) .build(); @@ -69,7 +69,7 @@ public void getCoverageReport_fullCoverage() throws Exception { @Test public void getCoverageReport_partialCoverage_shortCircuit() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("x", SimpleType.INT) .addVar("y", SimpleType.INT) .build(); @@ -98,7 +98,7 @@ public void getCoverageReport_partialCoverage_shortCircuit() throws Exception { @Test public void getCoverageReport_comprehension_generatesDotGraph() throws Exception { - Cel cel = CelFactory.standardCelBuilder().build(); + Cel cel = CelFactory.plannerCelBuilder().build(); CelCompiler compiler = cel.toCompilerBuilder() .setOptions(CelOptions.newBuilder().populateMacroCalls(true).build()) @@ -130,7 +130,7 @@ public void getCoverageReport_comprehension_generatesDotGraph() throws Exception @Test public void getCoverageReport_fullCoverage_writesToUndeclaredOutputs() throws Exception { // Setup for a more complex graph to write. - Cel cel = CelFactory.standardCelBuilder().build(); + Cel cel = CelFactory.plannerCelBuilder().build(); CelCompiler compiler = cel.toCompilerBuilder() .setOptions(CelOptions.newBuilder().populateMacroCalls(true).build()) @@ -157,7 +157,7 @@ public void getCoverageReport_fullCoverage_writesToUndeclaredOutputs() throws Ex @Test public void getCoverageReport_fullCoverage_multipleEvaluations() throws Exception { - Cel cel = CelFactory.standardCelBuilder().addVar("x", SimpleType.INT).build(); + Cel cel = CelFactory.plannerCelBuilder().addVar("x", SimpleType.INT).build(); CelAbstractSyntaxTree ast = cel.compile("x > 1").getAst(); CelRuntime.Program program = cel.createProgram(ast); CelCoverageIndex coverageIndex = new CelCoverageIndex(); diff --git a/testing/src/test/java/dev/cel/testing/testrunner/ContextPbUserTest.java b/testing/src/test/java/dev/cel/testing/testrunner/ContextPbUserTest.java index 0270cc52d..6a9766dcf 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/ContextPbUserTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/ContextPbUserTest.java @@ -33,7 +33,7 @@ public ContextPbUserTest() { super( CelTestContext.newBuilder() .setCel( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFileTypes(TestAllTypes.getDescriptor().getFile()) .addProtoTypeMasks( ProtoTypeMask.ofAllFields(TestAllTypes.getDescriptor().getFullName()) diff --git a/testing/src/test/java/dev/cel/testing/testrunner/CoverageTest.java b/testing/src/test/java/dev/cel/testing/testrunner/CoverageTest.java index c7b0c395d..f26fa27b7 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/CoverageTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/CoverageTest.java @@ -30,7 +30,7 @@ public CoverageTest() { super( CelTestContext.newBuilder() .setCel( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("x", SimpleType.INT) .addVar("y", SimpleType.INT) .build()) diff --git a/testing/src/test/java/dev/cel/testing/testrunner/CustomVariableBindingUserTest.java b/testing/src/test/java/dev/cel/testing/testrunner/CustomVariableBindingUserTest.java index 37382052c..23f7b0f72 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/CustomVariableBindingUserTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/CustomVariableBindingUserTest.java @@ -39,7 +39,7 @@ private static CelTestContext newTestContext() { return CelTestContext.newBuilder() .setCel( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addMessageTypes(TestAllTypes.getDescriptor()) .addFileTypes(TestAllTypesExtensions.getDescriptor()) .setExtensionRegistry(registry) diff --git a/testing/src/test/java/dev/cel/testing/testrunner/DefaultResultMatcherTest.java b/testing/src/test/java/dev/cel/testing/testrunner/DefaultResultMatcherTest.java index 41677c16c..b2accd23a 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/DefaultResultMatcherTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/DefaultResultMatcherTest.java @@ -51,7 +51,7 @@ public void match_resultExprEvaluationError_failure() throws Exception { IllegalArgumentException thrown = assertThrows( IllegalArgumentException.class, - () -> MATCHER.match(params, CelFactory.standardCelBuilder().build())); + () -> MATCHER.match(params, CelFactory.plannerCelBuilder().build())); assertThat(thrown) .hasMessageThat() @@ -73,7 +73,7 @@ public void match_expectedExprValueForResultExprOutputAndComputedEvalError_failu AssertionError thrown = assertThrows( AssertionError.class, - () -> MATCHER.match(params, CelFactory.standardCelBuilder().build())); + () -> MATCHER.match(params, CelFactory.plannerCelBuilder().build())); assertThat(thrown).hasMessageThat().contains("Error: evaluation error"); } @@ -97,7 +97,7 @@ public void match_expectedExprValueAndComputedEvalError_failure() throws Excepti AssertionError thrown = assertThrows( AssertionError.class, - () -> MATCHER.match(params, CelFactory.standardCelBuilder().build())); + () -> MATCHER.match(params, CelFactory.plannerCelBuilder().build())); assertThat(thrown).hasMessageThat().contains("Error: evaluation error"); } @@ -119,7 +119,7 @@ public void match_expectedEvalErrorAndComputedExprValue_failure() throws Excepti AssertionError thrown = assertThrows( AssertionError.class, - () -> MATCHER.match(params, CelFactory.standardCelBuilder().build())); + () -> MATCHER.match(params, CelFactory.plannerCelBuilder().build())); assertThat(thrown) .hasMessageThat() diff --git a/testing/src/test/java/dev/cel/testing/testrunner/EnvConfigUserTest.java b/testing/src/test/java/dev/cel/testing/testrunner/EnvConfigUserTest.java index 99b2d1f79..e0adec551 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/EnvConfigUserTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/EnvConfigUserTest.java @@ -26,6 +26,6 @@ public class EnvConfigUserTest extends CelUserTestTemplate { public EnvConfigUserTest() { - super(CelTestContext.newBuilder().setCel(CelFactory.standardCelBuilder().build()).build()); + super(CelTestContext.newBuilder().setCel(CelFactory.plannerCelBuilder().build()).build()); } } diff --git a/testing/src/test/java/dev/cel/testing/testrunner/LateFunctionBindingUserTest.java b/testing/src/test/java/dev/cel/testing/testrunner/LateFunctionBindingUserTest.java index 72992be3f..671b00408 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/LateFunctionBindingUserTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/LateFunctionBindingUserTest.java @@ -14,6 +14,7 @@ package dev.cel.testing.testrunner; +import dev.cel.bundle.CelFactory; import dev.cel.runtime.CelFunctionBinding; import dev.cel.runtime.CelLateFunctionBindings; import org.junit.runner.RunWith; @@ -26,6 +27,7 @@ public class LateFunctionBindingUserTest extends CelUserTestTemplate { public LateFunctionBindingUserTest() { super( CelTestContext.newBuilder() + .setCel(CelFactory.plannerCelBuilder().addLateBoundFunctions("foo", "bar").build()) .setCelLateFunctionBindings( CelLateFunctionBindings.from( CelFunctionBinding.from("foo_id", String.class, (String a) -> a.equals("foo")), diff --git a/testing/src/test/java/dev/cel/testing/testrunner/TestRunnerLibraryTest.java b/testing/src/test/java/dev/cel/testing/testrunner/TestRunnerLibraryTest.java index 112ef1f82..399cf5cda 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/TestRunnerLibraryTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/TestRunnerLibraryTest.java @@ -140,7 +140,7 @@ public void runPolicyTest_evaluatedContextExprNotProtoMessage_failure() throws E TestUtil.getSrcDir() + "/google3/third_party/java/cel/testing/src/test/java/dev/cel/testing/testrunner/resources/empty_policy.yaml")) .setCel( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFileTypes(TestAllTypes.getDescriptor().getFile()) .addProtoTypeMasks( ProtoTypeMask.ofAllFields( @@ -175,7 +175,7 @@ public void runPolicyTest_evaluationError_failureAssertion() throws Exception { CelExpressionSource.fromSource( TestUtil.getSrcDir() + "/google3/third_party/java/cel/testing/src/test/java/dev/cel/testing/testrunner/resources/eval_error_policy.yaml")) - .setCel(CelFactory.standardCelBuilder().addVar("x", SimpleType.INT).build()) + .setCel(CelFactory.plannerCelBuilder().addVar("x", SimpleType.INT).build()) .build())); assertThat(thrown) @@ -210,7 +210,7 @@ public void runExpressionTest_outputMismatch_failureAssertion() throws Exception CelExpressionSource.fromSource( TestUtil.getSrcDir() + "/google3/third_party/java/cel/testing/src/test/java/dev/cel/testing/testrunner/output.textproto")) - .setCel(CelFactory.standardCelBuilder().build()) + .setCel(CelFactory.plannerCelBuilder().build()) .build())); assertThat(thrown).hasMessageThat().contains("modified: value.bool_value: true -> false"); @@ -299,7 +299,7 @@ public void runTest_withBindingTransformer() throws Exception { testCase, CelTestContext.newBuilder() .setCelExpression(CelExpressionSource.fromRawExpr("x + 1")) - .setCel(CelFactory.standardCelBuilder().addVar("x", SimpleType.INT).build()) + .setCel(CelFactory.plannerCelBuilder().addVar("x", SimpleType.INT).build()) .setBindingTransformer( bindings -> { ImmutableMap.Builder transformed = ImmutableMap.builder(); @@ -340,7 +340,7 @@ public void typeRegistry_withFileTypes() throws Exception { CelTestContext celTestContext = CelTestContext.newBuilder() .setCelExpression(CelExpressionSource.fromRawExpr("true")) - .setCel(CelFactory.standardCelBuilder().build()) + .setCel(CelFactory.plannerCelBuilder().build()) .addMessageTypes(TestAllTypes.getDescriptor()) .build(); diff --git a/testing/src/test/java/dev/cel/testing/testrunner/UserTest.java b/testing/src/test/java/dev/cel/testing/testrunner/UserTest.java index 6f4c838dd..1c21e2e49 100644 --- a/testing/src/test/java/dev/cel/testing/testrunner/UserTest.java +++ b/testing/src/test/java/dev/cel/testing/testrunner/UserTest.java @@ -31,7 +31,7 @@ public UserTest() { super( CelTestContext.newBuilder() .setCel( - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("resource", MapType.create(SimpleType.STRING, SimpleType.ANY)) .build()) .build()); diff --git a/validator/src/test/java/dev/cel/validator/BUILD.bazel b/validator/src/test/java/dev/cel/validator/BUILD.bazel index d7384bf74..fd43759fe 100644 --- a/validator/src/test/java/dev/cel/validator/BUILD.bazel +++ b/validator/src/test/java/dev/cel/validator/BUILD.bazel @@ -12,11 +12,8 @@ java_library( deps = [ "//:java_truth", "//bundle:cel", - "//common:compiler_common", - "//compiler", - "//parser:parser_factory", - "//runtime", - "//validator", + "//common:cel_issue", + "//common:cel_validation_result", "//validator:validator_builder", "//validator:validator_impl", "@maven//:junit_junit", diff --git a/validator/src/test/java/dev/cel/validator/CelValidatorFactoryTest.java b/validator/src/test/java/dev/cel/validator/CelValidatorFactoryTest.java deleted file mode 100644 index 46ad1fc55..000000000 --- a/validator/src/test/java/dev/cel/validator/CelValidatorFactoryTest.java +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2023 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.validator; - -import static com.google.common.truth.Truth.assertThat; - -import dev.cel.bundle.CelFactory; -import dev.cel.compiler.CelCompilerFactory; -import dev.cel.parser.CelParserFactory; -import dev.cel.runtime.CelRuntimeFactory; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class CelValidatorFactoryTest { - - @Test - public void standardCelValidatorBuilder_withParserCheckerAndRuntime() { - CelValidatorBuilder builder = - CelValidatorFactory.standardCelValidatorBuilder( - CelParserFactory.standardCelParserBuilder().build(), - CelCompilerFactory.standardCelCheckerBuilder().build(), - CelRuntimeFactory.standardCelRuntimeBuilder().build()); - - assertThat(builder).isNotNull(); - assertThat(builder.build()).isNotNull(); - } - - @Test - public void standardCelValidatorBuilder_withCompilerAndRuntime() { - CelValidatorBuilder builder = - CelValidatorFactory.standardCelValidatorBuilder( - CelCompilerFactory.standardCelCompilerBuilder().build(), - CelRuntimeFactory.standardCelRuntimeBuilder().build()); - - assertThat(builder).isNotNull(); - assertThat(builder.build()).isNotNull(); - } - - @Test - public void standardCelValidatorBuilder_withCel() { - CelValidatorBuilder builder = - CelValidatorFactory.standardCelValidatorBuilder(CelFactory.standardCelBuilder().build()); - - assertThat(builder).isNotNull(); - assertThat(builder.build()).isNotNull(); - } -} diff --git a/validator/src/test/java/dev/cel/validator/CelValidatorImplTest.java b/validator/src/test/java/dev/cel/validator/CelValidatorImplTest.java index 521faa9d4..478a2bb2f 100644 --- a/validator/src/test/java/dev/cel/validator/CelValidatorImplTest.java +++ b/validator/src/test/java/dev/cel/validator/CelValidatorImplTest.java @@ -29,21 +29,7 @@ @RunWith(JUnit4.class) public class CelValidatorImplTest { - private static final Cel CEL = CelFactory.standardCelBuilder().build(); - - @Test - public void constructCelValidator_success() { - CelValidator celValidator = - CelValidatorImpl.newBuilder(CEL) - .addAstValidators( - (navigableAst, cel, issuesFactory) -> { - // no-op - }) - .build(); - - assertThat(celValidator).isNotNull(); - assertThat(celValidator).isInstanceOf(CelValidatorImpl.class); - } + private static final Cel CEL = CelFactory.plannerCelBuilder().build(); @Test public void validator_inOrder() throws Exception { diff --git a/validator/src/test/java/dev/cel/validator/validators/AstDepthLimitValidatorTest.java b/validator/src/test/java/dev/cel/validator/validators/AstDepthLimitValidatorTest.java index acc2544c7..72b78bb8e 100644 --- a/validator/src/test/java/dev/cel/validator/validators/AstDepthLimitValidatorTest.java +++ b/validator/src/test/java/dev/cel/validator/validators/AstDepthLimitValidatorTest.java @@ -42,7 +42,7 @@ public class AstDepthLimitValidatorTest { private static final Cel CEL = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("x", SimpleType.DYN) .addFunctionDeclarations( newFunctionDeclaration( diff --git a/validator/src/test/java/dev/cel/validator/validators/BUILD.bazel b/validator/src/test/java/dev/cel/validator/validators/BUILD.bazel index adfd406c8..b6e52d378 100644 --- a/validator/src/test/java/dev/cel/validator/validators/BUILD.bazel +++ b/validator/src/test/java/dev/cel/validator/validators/BUILD.bazel @@ -12,6 +12,7 @@ java_library( deps = [ "//:java_truth", "//bundle:cel", + "//checker:standard_decl", "//common:cel_ast", "//common:compiler_common", "//common:options", @@ -23,6 +24,7 @@ java_library( "//parser:macro", "//runtime", "//runtime:function_binding", + "//runtime:standard_functions", "//validator", "//validator:validator_builder", "//validator/validators:ast_depth_limit_validator", @@ -33,7 +35,6 @@ java_library( "//validator/validators:timestamp", "@cel_spec//proto/cel/expr:checked_java_proto", "@maven//:com_google_guava_guava", - "@maven//:com_google_protobuf_protobuf_java", "@maven//:com_google_testparameterinjector_test_parameter_injector", "@maven//:junit_junit", "@maven_android//:com_google_protobuf_protobuf_javalite", diff --git a/validator/src/test/java/dev/cel/validator/validators/ComprehensionNestingLimitValidatorTest.java b/validator/src/test/java/dev/cel/validator/validators/ComprehensionNestingLimitValidatorTest.java index ccf625d88..75d703c1e 100644 --- a/validator/src/test/java/dev/cel/validator/validators/ComprehensionNestingLimitValidatorTest.java +++ b/validator/src/test/java/dev/cel/validator/validators/ComprehensionNestingLimitValidatorTest.java @@ -37,7 +37,7 @@ public class ComprehensionNestingLimitValidatorTest { private static final Cel CEL = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries( CelExtensions.optional(), CelExtensions.comprehensions(), CelExtensions.bindings()) .addRuntimeLibraries(CelExtensions.optional(), CelExtensions.comprehensions()) diff --git a/validator/src/test/java/dev/cel/validator/validators/DurationLiteralValidatorTest.java b/validator/src/test/java/dev/cel/validator/validators/DurationLiteralValidatorTest.java index 1ec3ef4aa..fb3a23598 100644 --- a/validator/src/test/java/dev/cel/validator/validators/DurationLiteralValidatorTest.java +++ b/validator/src/test/java/dev/cel/validator/validators/DurationLiteralValidatorTest.java @@ -24,6 +24,7 @@ import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.bundle.Cel; import dev.cel.bundle.CelFactory; +import dev.cel.checker.CelStandardDeclarations; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelIssue.Severity; import dev.cel.common.CelValidationResult; @@ -31,6 +32,7 @@ import dev.cel.common.types.SimpleType; import dev.cel.runtime.CelEvaluationException; import dev.cel.runtime.CelFunctionBinding; +import dev.cel.runtime.CelStandardFunctions; import dev.cel.validator.CelValidator; import dev.cel.validator.CelValidatorFactory; import java.text.ParseException; @@ -40,7 +42,7 @@ @RunWith(TestParameterInjector.class) public class DurationLiteralValidatorTest { - private static final Cel CEL = CelFactory.standardCelBuilder().build(); + private static final Cel CEL = CelFactory.plannerCelBuilder().build(); private static final CelValidator CEL_VALIDATOR = CelValidatorFactory.standardCelValidatorBuilder(CEL) @@ -76,7 +78,7 @@ public void durationsInCallArgument_validFormat() throws Exception { @Test public void duration_withVariable_noOp() throws Exception { - Cel cel = CelFactory.standardCelBuilder().addVar("str_var", SimpleType.STRING).build(); + Cel cel = CelFactory.plannerCelBuilder().addVar("str_var", SimpleType.STRING).build(); CelAbstractSyntaxTree ast = cel.compile("duration(str_var)").getAst(); CelValidationResult result = CEL_VALIDATOR.validate(ast); @@ -97,7 +99,7 @@ public void duration_withVariable_noOp() throws Exception { @Test public void duration_withFunction_noOp() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionDeclarations( newFunctionDeclaration( "testFunc", @@ -151,8 +153,10 @@ public void duration_invalidFormat() throws Exception { @Test public void duration_unexpectedResultType_throws() throws Exception { Cel cel = - CelFactory.standardCelBuilder() - .setStandardEnvironmentEnabled(false) + CelFactory.plannerCelBuilder() + .setStandardDeclarations( + CelStandardDeclarations.newBuilder().includeFunctions().build()) + .setStandardFunctions(CelStandardFunctions.newBuilder().includeFunctions().build()) .addFunctionDeclarations( newFunctionDeclaration( "duration", @@ -174,7 +178,7 @@ public void duration_unexpectedResultType_throws() throws Exception { assertThat(result.getAllIssues().get(0).toDisplayString(ast.getSource())) .isEqualTo( "ERROR: :1:10: duration validation failed. Reason: Expected" - + " java.time.Duration type but got java.lang.Integer instead\n" + + " java.time.Duration type but got java.lang.Long instead\n" + " | duration('1h')\n" + " | .........^"); } diff --git a/validator/src/test/java/dev/cel/validator/validators/HomogeneousLiteralValidatorTest.java b/validator/src/test/java/dev/cel/validator/validators/HomogeneousLiteralValidatorTest.java index baaf3f05c..a9f856aae 100644 --- a/validator/src/test/java/dev/cel/validator/validators/HomogeneousLiteralValidatorTest.java +++ b/validator/src/test/java/dev/cel/validator/validators/HomogeneousLiteralValidatorTest.java @@ -27,7 +27,6 @@ import dev.cel.common.CelValidationResult; import dev.cel.common.types.SimpleType; import dev.cel.extensions.CelOptionalLibrary; -import dev.cel.runtime.CelFunctionBinding; import dev.cel.validator.CelValidator; import dev.cel.validator.CelValidatorFactory; import java.util.List; @@ -38,7 +37,7 @@ @RunWith(TestParameterInjector.class) public class HomogeneousLiteralValidatorTest { private static final Cel CEL = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addCompilerLibraries(CelOptionalLibrary.INSTANCE) .addRuntimeLibraries(CelOptionalLibrary.INSTANCE) .build(); @@ -228,7 +227,7 @@ public void mapValue_containsHeterogeneousLiteralsInDyn() throws Exception { @TestParameters("{source: '''%s''.format([[[1, 2, [3.0, 4]]][0]])'}") public void heterogeneousLiterals_inExemptFunction(String source) throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionDeclarations( newFunctionDeclaration( "exemptFunction", @@ -240,10 +239,6 @@ public void heterogeneousLiterals_inExemptFunction(String source) throws Excepti SimpleType.BOOL, SimpleType.STRING, SimpleType.DYN))) - .addFunctionBindings( - CelFunctionBinding.from("exemptFunctionOverloadId", Object.class, (arg) -> true), - CelFunctionBinding.from( - "stringFormatOverloadId", String.class, Object.class, (str, arg) -> true)) .build(); CelValidator validator = CelValidatorFactory.standardCelValidatorBuilder(cel) @@ -255,6 +250,5 @@ public void heterogeneousLiterals_inExemptFunction(String source) throws Excepti assertThat(result.hasError()).isFalse(); assertThat(result.getAllIssues()).isEmpty(); - assertThat(cel.createProgram(ast).eval()).isInstanceOf(Boolean.class); } } diff --git a/validator/src/test/java/dev/cel/validator/validators/RegexLiteralValidatorTest.java b/validator/src/test/java/dev/cel/validator/validators/RegexLiteralValidatorTest.java index a41317371..20d3f32e7 100644 --- a/validator/src/test/java/dev/cel/validator/validators/RegexLiteralValidatorTest.java +++ b/validator/src/test/java/dev/cel/validator/validators/RegexLiteralValidatorTest.java @@ -39,9 +39,10 @@ @RunWith(TestParameterInjector.class) public class RegexLiteralValidatorTest { - private static final CelOptions CEL_OPTIONS = CelOptions.current().build(); + private static final CelOptions CEL_OPTIONS = + CelOptions.current().enableHeterogeneousNumericComparisons(true).build(); - private static final Cel CEL = CelFactory.standardCelBuilder().setOptions(CEL_OPTIONS).build(); + private static final Cel CEL = CelFactory.plannerCelBuilder().setOptions(CEL_OPTIONS).build(); private static final CelValidator CEL_VALIDATOR = CelValidatorFactory.standardCelValidatorBuilder(CEL) @@ -80,7 +81,7 @@ public void regex_receiver_validFormat(String source) throws Exception { @Test public void regex_globalWithVariable_noOp() throws Exception { - Cel cel = CelFactory.standardCelBuilder().addVar("str_var", SimpleType.STRING).build(); + Cel cel = CelFactory.plannerCelBuilder().addVar("str_var", SimpleType.STRING).build(); CelAbstractSyntaxTree ast = cel.compile("matches('test', str_var)").getAst(); CelValidationResult result = CEL_VALIDATOR.validate(ast); @@ -102,7 +103,7 @@ public void regex_globalWithVariable_noOp() throws Exception { @Test public void regex_receiverWithVariable_noOp() throws Exception { - Cel cel = CelFactory.standardCelBuilder().addVar("str_var", SimpleType.STRING).build(); + Cel cel = CelFactory.plannerCelBuilder().addVar("str_var", SimpleType.STRING).build(); CelAbstractSyntaxTree ast = cel.compile("'test'.matches(str_var)").getAst(); CelValidationResult result = CEL_VALIDATOR.validate(ast); @@ -125,7 +126,7 @@ public void regex_receiverWithVariable_noOp() throws Exception { @Test public void regex_globalWithFunction_noOp() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionDeclarations( newFunctionDeclaration( "testFunc", newGlobalOverload("testFuncOverloadId", SimpleType.STRING))) @@ -152,7 +153,7 @@ public void regex_globalWithFunction_noOp() throws Exception { @Test public void regex_receiverWithFunction_noOp() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionDeclarations( newFunctionDeclaration( "testFunc", newGlobalOverload("testFuncOverloadId", SimpleType.STRING))) diff --git a/validator/src/test/java/dev/cel/validator/validators/TimestampLiteralValidatorTest.java b/validator/src/test/java/dev/cel/validator/validators/TimestampLiteralValidatorTest.java index 404ed7f7e..304b214ba 100644 --- a/validator/src/test/java/dev/cel/validator/validators/TimestampLiteralValidatorTest.java +++ b/validator/src/test/java/dev/cel/validator/validators/TimestampLiteralValidatorTest.java @@ -24,6 +24,7 @@ import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.bundle.Cel; import dev.cel.bundle.CelFactory; +import dev.cel.checker.CelStandardDeclarations; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelIssue.Severity; import dev.cel.common.CelOptions; @@ -32,6 +33,7 @@ import dev.cel.common.types.SimpleType; import dev.cel.runtime.CelEvaluationException; import dev.cel.runtime.CelFunctionBinding; +import dev.cel.runtime.CelStandardFunctions; import dev.cel.validator.CelValidator; import dev.cel.validator.CelValidatorFactory; import java.text.ParseException; @@ -41,9 +43,10 @@ @RunWith(TestParameterInjector.class) public class TimestampLiteralValidatorTest { - private static final CelOptions CEL_OPTIONS = CelOptions.current().build(); + private static final CelOptions CEL_OPTIONS = + CelOptions.current().enableHeterogeneousNumericComparisons(true).build(); - private static final Cel CEL = CelFactory.standardCelBuilder().setOptions(CEL_OPTIONS).build(); + private static final Cel CEL = CelFactory.plannerCelBuilder().setOptions(CEL_OPTIONS).build(); private static final CelValidator CEL_VALIDATOR = CelValidatorFactory.standardCelValidatorBuilder(CEL) @@ -81,7 +84,7 @@ public void timestampsInCallArgument_validFormat() throws Exception { @Test public void timestamp_withVariable_noOp() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addVar("str_var", SimpleType.STRING) .setOptions(CEL_OPTIONS) .build(); @@ -105,7 +108,7 @@ public void timestamp_withVariable_noOp() throws Exception { @Test public void timestamp_withFunction_noOp() throws Exception { Cel cel = - CelFactory.standardCelBuilder() + CelFactory.plannerCelBuilder() .addFunctionDeclarations( newFunctionDeclaration( "testFunc", @@ -160,8 +163,10 @@ public void timestamp_invalidFormat() throws Exception { @Test public void timestamp_unexpectedResultType_throws() throws Exception { Cel cel = - CelFactory.standardCelBuilder() - .setStandardEnvironmentEnabled(false) + CelFactory.plannerCelBuilder() + .setStandardDeclarations( + CelStandardDeclarations.newBuilder().includeFunctions().build()) + .setStandardFunctions(CelStandardFunctions.newBuilder().includeFunctions().build()) .addFunctionDeclarations( newFunctionDeclaration( "timestamp", @@ -183,7 +188,7 @@ public void timestamp_unexpectedResultType_throws() throws Exception { assertThat(result.getAllIssues().get(0).toDisplayString(ast.getSource())) .isEqualTo( "ERROR: :1:11: timestamp validation failed. Reason: Expected" - + " java.time.Instant type but got java.lang.Integer instead\n" + + " java.time.Instant type but got java.lang.Long instead\n" + " | timestamp(0)\n" + " | ..........^"); } @@ -203,8 +208,8 @@ public void parentIsNotCallExpr_doesNotThrow(String source) throws Exception { @Test public void env_withSetResultType_success() throws Exception { Cel cel = - CelFactory.standardCelBuilder() - .setOptions(CelOptions.current().build()) + CelFactory.plannerCelBuilder() + .setOptions(CEL_OPTIONS) .setResultType(SimpleType.BOOL) .build(); CelValidator validator =