From 477e6ca439a6adaa371d0bfa7b949265c64a6dc0 Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Tue, 22 Jul 2025 14:57:41 -0700 Subject: [PATCH] Migrate CelBaselineTestCase away from proto based types PiperOrigin-RevId: 786008435 --- .../java/dev/cel/checker/ExprCheckerTest.java | 323 ++++++++---------- checker/src/test/resources/callStyle.baseline | 8 +- .../test/resources/standardEnvDump.baseline | 18 +- .../resources/userFunctionOverlaps.baseline | 9 +- .../cel/runtime/CelLiteInterpreterTest.java | 7 +- .../cel/runtime/CelValueInterpreterTest.java | 7 +- .../java/dev/cel/runtime/InterpreterTest.java | 8 +- .../test/resources/delayedEvaluation.baseline | 12 +- .../test/resources/longComprehension.baseline | 12 +- testing/BUILD.bazel | 5 - .../src/main/java/dev/cel/testing/BUILD.bazel | 26 +- .../dev/cel/testing/BaseInterpreterTest.java | 291 ++++++++-------- .../dev/cel/testing/CelBaselineTestCase.java | 165 +++------ .../testing/TestCelFunctionDeclWrapper.java | 45 --- .../testing/TestCelVariableDeclWrapper.java | 44 --- .../main/java/dev/cel/testing/TestDecl.java | 32 -- .../testing/TestProtoFunctionDeclWrapper.java | 44 --- .../testing/TestProtoVariableDeclWrapper.java | 40 --- 18 files changed, 372 insertions(+), 724 deletions(-) delete mode 100644 testing/src/main/java/dev/cel/testing/TestCelFunctionDeclWrapper.java delete mode 100644 testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java delete mode 100644 testing/src/main/java/dev/cel/testing/TestDecl.java delete mode 100644 testing/src/main/java/dev/cel/testing/TestProtoFunctionDeclWrapper.java delete mode 100644 testing/src/main/java/dev/cel/testing/TestProtoVariableDeclWrapper.java diff --git a/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java b/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java index 0b5d90eb2..03d983061 100644 --- a/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java +++ b/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java @@ -14,37 +14,41 @@ package dev.cel.checker; -import static dev.cel.common.types.CelProtoTypes.createList; -import static dev.cel.common.types.CelProtoTypes.createMap; -import static dev.cel.common.types.CelProtoTypes.createMessage; -import static dev.cel.common.types.CelProtoTypes.createOptionalType; -import static dev.cel.common.types.CelProtoTypes.createTypeParam; -import static dev.cel.common.types.CelProtoTypes.createWrapper; +import static com.google.common.collect.ImmutableList.toImmutableList; import static dev.cel.common.types.CelProtoTypes.format; import dev.cel.expr.CheckedExpr; import dev.cel.expr.Constant; +import dev.cel.expr.Decl; import dev.cel.expr.Expr.CreateStruct.EntryOrBuilder; import dev.cel.expr.ExprOrBuilder; import dev.cel.expr.ParsedExpr; import dev.cel.expr.Reference; -import dev.cel.expr.Type; -import dev.cel.expr.Type.AbstractType; -import dev.cel.expr.Type.PrimitiveType; import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.protobuf.DescriptorProtos.FileDescriptorSet; +import com.google.testing.junit.testparameterinjector.TestParameterInjector; // import com.google.testing.testsize.MediumTest; import dev.cel.common.CelAbstractSyntaxTree; +import dev.cel.common.CelFunctionDecl; +import dev.cel.common.CelOverloadDecl; import dev.cel.common.CelProtoAbstractSyntaxTree; +import dev.cel.common.CelVarDecl; import dev.cel.common.internal.EnvVisitable; import dev.cel.common.internal.Errors; import dev.cel.common.types.CelProtoTypes; +import dev.cel.common.types.CelType; import dev.cel.common.types.ListType; import dev.cel.common.types.MapType; +import dev.cel.common.types.NullableType; +import dev.cel.common.types.OpaqueType; +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.TypeParamType; +import dev.cel.common.types.TypeType; import dev.cel.expr.conformance.proto3.TestAllTypes; import dev.cel.testing.CelAdorner; import dev.cel.testing.CelBaselineTestCase; @@ -53,21 +57,14 @@ import java.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; /** Tests for the CEL {@link ExprChecker}. */ // @MediumTest -@RunWith(Parameterized.class) +@RunWith(TestParameterInjector.class) public class ExprCheckerTest extends CelBaselineTestCase { - @Parameters() - public static ImmutableList evalTestCases() { - return ImmutableList.copyOf(TestCase.values()); - } - - public ExprCheckerTest(TestCase testCase) { - super(testCase.declareWithCelType); + public ExprCheckerTest() { + super(); } /** Helper to run a test for configured instance variables. */ @@ -108,7 +105,28 @@ public void standardEnvDump() throws Exception { testOutput().println("Standard environment:"); ((EnvVisitable) celCompiler) - .accept((name, decls) -> testOutput().println(formatDecl(name, decls))); + .accept( + (name, decls) -> { + // TODO: Remove proto to native type adaptation after changing interface + for (Decl decl : decls) { + if (decl.hasFunction()) { + CelFunctionDecl celFunctionDecl = + CelFunctionDecl.newFunctionDeclaration( + decl.getName(), + decl.getFunction().getOverloadsList().stream() + .map(CelOverloadDecl::overloadToCelOverload) + .collect(toImmutableList())); + testOutput().println(formatFunctionDecl(celFunctionDecl)); + } else if (decl.hasIdent()) { + CelVarDecl celVarDecl = + CelVarDecl.newVarDeclaration( + decl.getName(), CelProtoTypes.typeToCelType(decl.getIdent().getType())); + testOutput().println(formatVarDecl(celVarDecl)); + } else { + throw new IllegalArgumentException("Invalid declaration: " + decl); + } + } + }); } // Operators @@ -152,7 +170,7 @@ public void operatorsBytes() throws Exception { @Test public void operatorsConditional() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "false ? x.single_timestamp : null"; runTest(); } @@ -175,7 +193,8 @@ public void referenceTypeAbsolute() throws Exception { @Test public void referenceValue() throws Exception { - declareVariable("container.x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable( + "container.x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x"; container = "container"; runTest(); @@ -192,8 +211,8 @@ public void referenceUndefinedError() throws Exception { @Test public void anyMessage() throws Exception { - declareVariable("x", CelProtoTypes.ANY); - declareVariable("y", createWrapper(PrimitiveType.INT64)); + declareVariable("x", SimpleType.ANY); + declareVariable("y", NullableType.create(SimpleType.INT)); source = "x == google.protobuf.Any{" + "type_url:'types.googleapis.com/cel.expr.conformance.proto3.TestAllTypes'}" @@ -204,7 +223,7 @@ public void anyMessage() throws Exception { @Test public void messageFieldSelect() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_nested_message.bb == 43 && has(x.single_nested_message) && has(x.single_int32)" + " && has(x.repeated_int32) && has(x.map_int64_nested_type)"; @@ -213,23 +232,22 @@ public void messageFieldSelect() throws Exception { @Test public void messageCreationError() throws Exception { - declareVariable("x", CelProtoTypes.INT64); + declareVariable("x", SimpleType.INT); source = "x{foo: 1}"; runTest(); - declareVariable("y", CelProtoTypes.create(CelProtoTypes.INT64)); + declareVariable("y", TypeType.create(SimpleType.INT)); source = "y{foo: 1}"; runTest(); - declareVariable( - "z", CelProtoTypes.create(CelProtoTypes.createMessage("msg_without_descriptor"))); + declareVariable("z", TypeType.create(StructTypeReference.create("msg_without_descriptor"))); source = "z{foo: 1}"; runTest(); } @Test public void messageFieldSelectError() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_nested_message.undefined == x.undefined"; runTest(); } @@ -239,7 +257,9 @@ public void messageFieldSelectError() throws Exception { @Test public void listOperators() throws Exception { - declareVariable("x", createList(createMessage("cel.expr.conformance.proto3.TestAllTypes"))); + declareVariable( + "x", + ListType.create(StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"))); source = "(x + x)[1].single_int32 == size(x)"; runTest(); @@ -249,14 +269,16 @@ public void listOperators() throws Exception { @Test public void listRepeatedOperators() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.repeated_int64[x.single_int32] == 23"; runTest(); } @Test public void listIndexTypeError() throws Exception { - declareVariable("x", createList(createMessage("cel.expr.conformance.proto3.TestAllTypes"))); + declareVariable( + "x", + ListType.create(StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"))); source = "x[1u]"; runTest(); } @@ -269,8 +291,10 @@ public void identError() throws Exception { @Test public void listElemTypeError() throws Exception { - declareVariable("x", createList(createMessage("cel.expr.conformance.proto3.TestAllTypes"))); - declareVariable("y", createList(CelProtoTypes.INT64)); + declareVariable( + "x", + ListType.create(StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"))); + declareVariable("y", ListType.create(SimpleType.INT)); source = "x + y"; runTest(); } @@ -282,7 +306,9 @@ public void listElemTypeError() throws Exception { public void mapOperators() throws Exception { declareVariable( "x", - createMap(CelProtoTypes.STRING, createMessage("cel.expr.conformance.proto3.TestAllTypes"))); + MapType.create( + SimpleType.STRING, + StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"))); source = "x[\"a\"].single_int32 == 23"; runTest(); @@ -294,14 +320,16 @@ public void mapOperators() throws Exception { public void mapIndexTypeError() throws Exception { declareVariable( "x", - createMap(CelProtoTypes.STRING, createMessage("cel.expr.conformance.proto3.TestAllTypes"))); + MapType.create( + SimpleType.STRING, + StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"))); source = "x[2].single_int32 == 23"; runTest(); } @Test public void mapEmpty() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "size(x.map_int64_nested_type) == 0"; runTest(); } @@ -311,14 +339,14 @@ public void mapEmpty() throws Exception { @Test public void wrapper() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_int64_wrapper + 1 != 23"; runTest(); } @Test public void equalsWrapper() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_int64_wrapper == 1 && " + "x.single_int32_wrapper != 2 && " @@ -334,14 +362,14 @@ public void equalsWrapper() throws Exception { @Test public void nullableWrapper() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_int64_wrapper == null"; runTest(); } @Test public void nullableMessage() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_nested_message != null"; runTest(); @@ -358,7 +386,7 @@ public void nullNull() throws Exception { @Test public void nullablePrimitiveError() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_int64 != null"; runTest(); } @@ -368,14 +396,14 @@ public void nullablePrimitiveError() throws Exception { @Test public void dynOperators() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_value + 1 / x.single_struct.y == 23"; runTest(); } @Test public void dynOperatorsAtRuntime() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto3.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); source = "x.single_value[23] + x.single_struct['y']"; runTest(); } @@ -394,17 +422,15 @@ public void flexibleTypeAdaption() throws Exception { source = "([[[1]], [[2]], [[3]]][0][0] + [2, 3, {'four': {'five': 'six'}}])[3]"; runTest(); - declareVariable("a", createTypeParam("T")); + declareVariable("a", TypeParamType.create("T")); source = "a.b + 1 == a[0]"; runTest(); - Type keyParam = createTypeParam("A"); - Type valParam = createTypeParam("B"); - Type mapType = createMap(keyParam, valParam); + CelType keyParam = TypeParamType.create("A"); + CelType valParam = TypeParamType.create("B"); + CelType mapType = MapType.create(keyParam, valParam); declareFunction( - "merge", - globalOverload( - "merge_maps", ImmutableList.of(mapType, mapType), ImmutableList.of("A", "B"), mapType)); + "merge", globalOverload("merge_maps", ImmutableList.of(mapType, mapType), mapType)); source = "merge({'hello': dyn(1)}, {'world': 2.0})"; runTest(); @@ -416,10 +442,8 @@ public void flexibleTypeAdaption() throws Exception { public void userFunctionOverlappingOverloadsError() throws Exception { declareFunction( "func", - memberOverload( - "overlapping_overload_1", ImmutableList.of(CelProtoTypes.INT64), CelProtoTypes.INT64), - memberOverload( - "overlapping_overload_2", ImmutableList.of(CelProtoTypes.INT64), CelProtoTypes.INT64)); + memberOverload("overlapping_overload_1", ImmutableList.of(SimpleType.INT), SimpleType.INT), + memberOverload("overlapping_overload_2", ImmutableList.of(SimpleType.INT), SimpleType.INT)); source = "func(1)"; runTest(); } @@ -429,9 +453,9 @@ public void userFunctionOverlappingOverloadsError() throws Exception { @Test public void jsonType() throws Exception { - declareVariable("x", createMessage("google.protobuf.Struct")); - declareVariable("y", createMessage("google.protobuf.ListValue")); - declareVariable("z", createMessage("google.protobuf.Value")); + declareVariable("x", StructTypeReference.create("google.protobuf.Struct")); + declareVariable("y", StructTypeReference.create("google.protobuf.ListValue")); + declareVariable("z", StructTypeReference.create("google.protobuf.Value")); source = "x[\"claims\"][\"groups\"][0].name == \"dummy\" " + "&& x.claims[\"exp\"] == y[1].time " @@ -445,18 +469,14 @@ public void jsonType() throws Exception { @Test public void callStyle() throws Exception { - Type param = createTypeParam("A"); + CelType param = TypeParamType.create("A"); // Note, the size() function here is added in a separate scope from the standard declaration // set, but the environment ensures that the standard and custom overloads are returned together // during function resolution time. declareFunction( "size", - memberOverload( - "my_size", - ImmutableList.of(createList(param)), - ImmutableList.of("A"), - CelProtoTypes.INT64)); - declareVariable("x", createList(CelProtoTypes.INT64)); + memberOverload("my_size", ImmutableList.of(ListType.create(param)), SimpleType.INT)); + declareVariable("x", ListType.create(SimpleType.INT)); source = "size(x) == x.size()"; runTest(); } @@ -467,12 +487,12 @@ public void userFunction() throws Exception { "myfun", memberOverload( "myfun_instance", - ImmutableList.of(CelProtoTypes.INT64, CelProtoTypes.BOOL, CelProtoTypes.UINT64), - CelProtoTypes.INT64), + ImmutableList.of(SimpleType.INT, SimpleType.BOOL, SimpleType.UINT), + SimpleType.INT), globalOverload( "myfun_static", - ImmutableList.of(CelProtoTypes.INT64, CelProtoTypes.BOOL, CelProtoTypes.UINT64), - CelProtoTypes.INT64)); + ImmutableList.of(SimpleType.INT, SimpleType.BOOL, SimpleType.UINT), + SimpleType.INT)); source = "myfun(1, true, 3u) + 1.myfun(false, 3u).myfun(true, 42u)"; runTest(); } @@ -481,8 +501,7 @@ public void userFunction() throws Exception { public void namespacedFunctions() throws Exception { declareFunction( "ns.func", - globalOverload( - "ns_func_overload", ImmutableList.of(CelProtoTypes.STRING), CelProtoTypes.INT64)); + globalOverload("ns_func_overload", ImmutableList.of(SimpleType.STRING), SimpleType.INT)); source = "ns.func('hello')"; runTest(); @@ -490,8 +509,8 @@ public void namespacedFunctions() throws Exception { "member", memberOverload( "ns_member_overload", - ImmutableList.of(CelProtoTypes.INT64, CelProtoTypes.INT64), - CelProtoTypes.INT64)); + ImmutableList.of(SimpleType.INT, SimpleType.INT), + SimpleType.INT)); source = "ns.func('hello').member(ns.func('test'))"; runTest(); @@ -522,12 +541,12 @@ public void namespacedFunctions() throws Exception { @Test public void namespacedVariables() throws Exception { container = "ns"; - declareVariable("ns.x", CelProtoTypes.INT64); + declareVariable("ns.x", SimpleType.INT); source = "x"; runTest(); container = "cel.expr.conformance.proto3"; - Type messageType = createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("cel.expr.conformance.proto3.msgVar", messageType); source = "msgVar.single_int32"; runTest(); @@ -535,40 +554,36 @@ public void namespacedVariables() throws Exception { @Test public void userFunctionMultipleOverloadsWithSanitization() throws Exception { - Type structType = createMessage("google.protobuf.Struct"); + CelType structType = StructTypeReference.create("google.protobuf.Struct"); declareVariable("s", structType); declareFunction( "myfun", - globalOverload("myfun_int", ImmutableList.of(CelProtoTypes.INT64), CelProtoTypes.INT64), - globalOverload("myfun_struct", ImmutableList.of(structType), CelProtoTypes.INT64)); + globalOverload("myfun_int", ImmutableList.of(SimpleType.INT), SimpleType.INT), + globalOverload("myfun_struct", ImmutableList.of(structType), SimpleType.INT)); source = "myfun(1) + myfun(s)"; runTest(); } @Test public void userFunctionOverlaps() throws Exception { - Type param = createTypeParam("TEST"); + CelType param = TypeParamType.create("TEST"); // Note, the size() function here shadows the definition of the size() function in the standard // declaration set. The type param name is chosen as 'TEST' to make sure not to conflict with // the standard environment type param name for the same overload signature. declareFunction( "size", - globalOverload( - "my_size", - ImmutableList.of(createList(param)), - ImmutableList.of("TEST"), - CelProtoTypes.UINT64)); - declareVariable("x", createList(CelProtoTypes.INT64)); + globalOverload("my_size", ImmutableList.of(ListType.create(param)), SimpleType.UINT)); + declareVariable("x", ListType.create(SimpleType.INT)); source = "size(x) == 1u"; runTest(); } @Test public void userFunctionAddsOverload() throws Exception { - Type messageType = createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("x", messageType); declareFunction( - "size", globalOverload("size_message", ImmutableList.of(messageType), CelProtoTypes.INT64)); + "size", globalOverload("size_message", ImmutableList.of(messageType), SimpleType.INT)); source = "size(x) > 4"; runTest(); } @@ -576,7 +591,7 @@ public void userFunctionAddsOverload() throws Exception { @Test public void userFunctionAddsMacroError() throws Exception { declareFunction( - "has", globalOverload("has_id", ImmutableList.of(CelProtoTypes.DYN), CelProtoTypes.DYN)); + "has", globalOverload("has_id", ImmutableList.of(SimpleType.DYN), SimpleType.DYN)); source = "false"; runTest(); } @@ -586,7 +601,7 @@ public void userFunctionAddsMacroError() throws Exception { @Test public void proto2PrimitiveField() throws Exception { - declareVariable("x", createMessage("cel.expr.conformance.proto2.TestAllTypes")); + declareVariable("x", StructTypeReference.create("cel.expr.conformance.proto2.TestAllTypes")); source = "x.single_fixed32 != 0u && x.single_fixed64 > 1u && x.single_int32 != null"; runTest(); source = "x.nestedgroup.single_name == ''"; @@ -703,12 +718,12 @@ public void enumValues() throws Exception { @Test public void nestedEnums() throws Exception { - declareVariable("x", createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); container = TestAllTypes.getDescriptor().getFile().getPackage(); source = "x.single_nested_enum == TestAllTypes.NestedEnum.BAR"; runTest(); - declareVariable("single_nested_enum", CelProtoTypes.INT64); + declareVariable("single_nested_enum", SimpleType.INT); source = "single_nested_enum == TestAllTypes.NestedEnum.BAR"; runTest(); @@ -759,7 +774,7 @@ public void conversions() throws Exception { @Test public void quantifiers() throws Exception { - Type messageType = createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("x", messageType); source = "x.repeated_int64.all(e, e > 0) " @@ -770,7 +785,7 @@ public void quantifiers() throws Exception { @Test public void quantifiersErrors() throws Exception { - Type messageType = createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("x", messageType); source = "x.all(e, 0)"; runTest(); @@ -778,7 +793,7 @@ public void quantifiersErrors() throws Exception { @Test public void mapExpr() throws Exception { - Type messageType = createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("x", messageType); source = "x.repeated_int64.map(x, double(x))"; runTest(); @@ -792,16 +807,16 @@ public void mapExpr() throws Exception { @Test public void mapFilterExpr() throws Exception { - Type messageType = createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("x", messageType); source = "x.repeated_int64.map(x, x > 0, double(x))"; runTest(); - declareVariable("lists", CelProtoTypes.DYN); + declareVariable("lists", SimpleType.DYN); source = "lists.filter(x, x > 1.5)"; runTest(); - declareVariable("args", createMap(CelProtoTypes.STRING, CelProtoTypes.DYN)); + declareVariable("args", MapType.create(SimpleType.STRING, SimpleType.DYN)); source = "args.user[\"myextension\"].customAttributes.filter(x, x.name == \"hobbies\")"; runTest(); } @@ -811,15 +826,14 @@ public void mapFilterExpr() throws Exception { @Test public void abstractTypeParameterLess() throws Exception { - Type abstractType = - Type.newBuilder().setAbstractType(AbstractType.newBuilder().setName("abs")).build(); + CelType abstractType = OpaqueType.create("abs"); // Declare the identifier 'abs' to bind to the abstract type. - declareVariable("abs", CelProtoTypes.create(abstractType)); + declareVariable("abs", TypeType.create(abstractType)); // Declare a function to create a new value of abstract type. declareFunction("make_abs", globalOverload("make_abs", ImmutableList.of(), abstractType)); // Declare a function to consume value of abstract type. declareFunction( - "as_bool", memberOverload("as_bool", ImmutableList.of(abstractType), CelProtoTypes.BOOL)); + "as_bool", memberOverload("as_bool", ImmutableList.of(abstractType), SimpleType.BOOL)); source = "type(make_abs()) == abs && make_abs().as_bool()"; runTest(); @@ -827,36 +841,22 @@ public void abstractTypeParameterLess() throws Exception { @Test public void abstractTypeParameterized() throws Exception { - Type typeParam = CelProtoTypes.createTypeParam("T"); - Type abstractType = - Type.newBuilder() - .setAbstractType( - AbstractType.newBuilder().setName("vector").addParameterTypes(typeParam)) - .build(); + CelType typeParam = TypeParamType.create("T"); + CelType abstractType = OpaqueType.create("vector", typeParam); + TypeType typeOfTypeParam = TypeType.create(typeParam); + TypeType typeOfAbstractType = TypeType.create(abstractType); declareFunction( "vector", // Declare the function 'vector' to create the abstract type. - globalOverload( - "vector_type", - ImmutableList.of(CelProtoTypes.create(typeParam)), - ImmutableList.of("T"), - CelProtoTypes.create(abstractType)), + globalOverload("vector_type", ImmutableList.of(typeOfTypeParam), typeOfAbstractType), // Declare a function to create a new value of abstract type based on a list. - globalOverload( - "vector_list", - ImmutableList.of(CelProtoTypes.createList(typeParam)), - ImmutableList.of("T"), - abstractType)); + globalOverload("vector_list", ImmutableList.of(ListType.create(typeParam)), abstractType)); // Declare a function to consume value of abstract type. declareFunction( "at", - memberOverload( - "vector_at_int", - ImmutableList.of(abstractType, CelProtoTypes.INT64), - ImmutableList.of("T"), - typeParam)); + memberOverload("vector_at_int", ImmutableList.of(abstractType, SimpleType.INT), typeParam)); // The parameterization of 'vector(dyn)' is erased at runtime and so is checked as a 'vector', // but no further. @@ -866,26 +866,17 @@ public void abstractTypeParameterized() throws Exception { @Test public void abstractTypeParameterizedInListLiteral() throws Exception { - Type typeParam = createTypeParam("T"); - Type abstractType = - Type.newBuilder() - .setAbstractType( - AbstractType.newBuilder().setName("vector").addParameterTypes(typeParam)) - .build(); + CelType typeParam = TypeParamType.create("T"); + CelType abstractType = OpaqueType.create("vector", typeParam); + TypeType typeOfAbstractType = TypeType.create(abstractType); + TypeType typeOfTypeParam = TypeType.create(typeParam); + declareFunction( "vector", // Declare the function 'vector' to create the abstract type. - globalOverload( - "vector_type", - ImmutableList.of(CelProtoTypes.create(typeParam)), - ImmutableList.of("T"), - CelProtoTypes.create(abstractType)), + globalOverload("vector_type", ImmutableList.of(typeOfTypeParam), typeOfAbstractType), // Declare a function to create a new value of abstract type based on a list. - globalOverload( - "vector_list", - ImmutableList.of(createList(typeParam)), - ImmutableList.of("T"), - abstractType)); + globalOverload("vector_list", ImmutableList.of(ListType.create(typeParam)), abstractType)); source = "size([vector([1, 2]), vector([2u, -1])]) == 2"; runTest(); @@ -893,34 +884,23 @@ public void abstractTypeParameterizedInListLiteral() throws Exception { @Test public void abstractTypeParameterizedError() throws Exception { - Type typeParam = createTypeParam("T"); - Type abstractType = - Type.newBuilder() - .setAbstractType( - AbstractType.newBuilder().setName("vector").addParameterTypes(typeParam)) - .build(); + CelType typeParam = TypeParamType.create("T"); + CelType abstractType = OpaqueType.create("vector", typeParam); + TypeType typeOfAbstractType = TypeType.create(abstractType); + TypeType typeOfTypeParam = TypeType.create(typeParam); + declareFunction( "vector", // Declare the function 'vector' to create the abstract type. - globalOverload( - "vector_type", - ImmutableList.of(CelProtoTypes.create(typeParam)), - ImmutableList.of("T"), - CelProtoTypes.create(abstractType)), + globalOverload("vector_type", ImmutableList.of(typeOfTypeParam), typeOfAbstractType), // Declare a function to create a new value of abstract type based on a list. - globalOverload( - "vector_list", - ImmutableList.of(createList(typeParam)), - ImmutableList.of("T"), - abstractType)); + globalOverload("vector_list", ImmutableList.of(ListType.create(typeParam)), abstractType)); declareFunction( "add", globalOverload( "add_vector_type", - ImmutableList.of( - CelProtoTypes.create(abstractType), CelProtoTypes.create(abstractType)), - ImmutableList.of("T"), - CelProtoTypes.create(abstractType))); + ImmutableList.of(typeOfAbstractType, typeOfAbstractType), + typeOfAbstractType)); source = "add(vector([1, 2]), vector([2u, -1])) == vector([1, 2, 2u, -1])"; runTest(); } @@ -928,12 +908,12 @@ public void abstractTypeParameterizedError() throws Exception { // Optionals @Test public void optionals() throws Exception { - declareVariable("a", createMap(CelProtoTypes.STRING, CelProtoTypes.STRING)); + declareVariable("a", MapType.create(SimpleType.STRING, SimpleType.STRING)); source = "a.?b"; runTest(); clearAllDeclarations(); - declareVariable("x", createOptionalType(createMap(CelProtoTypes.STRING, CelProtoTypes.STRING))); + declareVariable("x", OptionalType.create(MapType.create(SimpleType.STRING, SimpleType.STRING))); source = "x.y"; runTest(); @@ -941,7 +921,7 @@ public void optionals() throws Exception { runTest(); clearAllDeclarations(); - declareVariable("d", createOptionalType(CelProtoTypes.DYN)); + declareVariable("d", OptionalType.create(SimpleType.DYN)); source = "d.dynamic"; runTest(); @@ -949,7 +929,7 @@ public void optionals() throws Exception { runTest(); clearAllDeclarations(); - declareVariable("e", createOptionalType(createMap(CelProtoTypes.STRING, CelProtoTypes.DYN))); + declareVariable("e", OptionalType.create(MapType.create(SimpleType.STRING, SimpleType.DYN))); source = "has(e.?b.c)"; runTest(); @@ -965,8 +945,8 @@ public void optionals() throws Exception { runTest(); container = ""; - declareVariable("a", createOptionalType(CelProtoTypes.STRING)); - declareVariable("b", createOptionalType(CelProtoTypes.STRING)); + declareVariable("a", OptionalType.create(SimpleType.STRING)); + declareVariable("b", OptionalType.create(SimpleType.STRING)); source = "[?a, ?b, 'world']"; runTest(); @@ -990,7 +970,7 @@ public void optionalErrors() throws Exception { runTest(); source = "a.?b"; - declareVariable("a", createMap(CelProtoTypes.STRING, CelProtoTypes.STRING)); + declareVariable("a", MapType.create(SimpleType.STRING, SimpleType.STRING)); prepareCompiler(new ProtoMessageTypeProvider()); ParsedExpr parsedExpr = CelProtoAbstractSyntaxTree.fromCelAst(celCompiler.parse(source).getAst()).toParsedExpr(); @@ -1003,17 +983,6 @@ public void optionalErrors() throws Exception { runErroneousTest(parsedExprBuilder.build()); } - private enum TestCase { - CEL_TYPE(true), - PROTO_TYPE(false); - - private final boolean declareWithCelType; - - TestCase(boolean declareWithCelType) { - this.declareWithCelType = declareWithCelType; - } - } - private static class CheckedExprAdorner implements CelAdorner { private final CheckedExpr checkedExpr; diff --git a/checker/src/test/resources/callStyle.baseline b/checker/src/test/resources/callStyle.baseline index 3e1cfcc0a..415f87e62 100644 --- a/checker/src/test/resources/callStyle.baseline +++ b/checker/src/test/resources/callStyle.baseline @@ -1,14 +1,14 @@ Source: size(x) == x.size() -declare size { - function my_size list(A).() -> int -} declare x { value list(int) } +declare size { + function my_size list(A).() -> int +} =====> _==_( size( x~list(int)^x )~int^size_list, x~list(int)^x.size()~int^my_size -)~bool^equals +)~bool^equals \ No newline at end of file diff --git a/checker/src/test/resources/standardEnvDump.baseline b/checker/src/test/resources/standardEnvDump.baseline index e4e810a30..42f1eb25d 100644 --- a/checker/src/test/resources/standardEnvDump.baseline +++ b/checker/src/test/resources/standardEnvDump.baseline @@ -143,11 +143,15 @@ declare _||_ { } declare bool { value type(bool) +} +declare bool { function bool_to_bool (bool) -> bool function string_to_bool (string) -> bool } declare bytes { value type(bytes) +} +declare bytes { function bytes_to_bytes (bytes) -> bytes function string_to_bytes (string) -> bytes } @@ -156,6 +160,8 @@ declare contains { } declare double { value type(double) +} +declare double { function double_to_double (double) -> double function int64_to_double (int) -> double function uint64_to_double (uint) -> double @@ -167,6 +173,8 @@ declare duration { } declare dyn { value type(dyn) +} +declare dyn { function to_dyn (A) -> dyn } declare endsWith { @@ -218,6 +226,8 @@ declare getSeconds { } declare int { value type(int) +} +declare int { function int64_to_int64 (int) -> int function uint64_to_int64 (uint) -> int function double_to_int64 (double) -> int @@ -252,6 +262,8 @@ declare startsWith { } declare string { value type(string) +} +declare string { function string_to_string (string) -> string function int64_to_string (int) -> string function uint64_to_string (uint) -> string @@ -268,12 +280,16 @@ declare timestamp { } declare type { value type(dyn) +} +declare type { function type (A) -> type(A) } declare uint { value type(uint) +} +declare uint { function uint64_to_uint64 (uint) -> uint function int64_to_uint64 (int) -> uint function double_to_uint64 (double) -> uint function string_to_uint64 (string) -> uint -} +} \ No newline at end of file diff --git a/checker/src/test/resources/userFunctionOverlaps.baseline b/checker/src/test/resources/userFunctionOverlaps.baseline index 0053f4919..059ba94b3 100644 --- a/checker/src/test/resources/userFunctionOverlaps.baseline +++ b/checker/src/test/resources/userFunctionOverlaps.baseline @@ -1,15 +1,14 @@ Source: size(x) == 1u -declare size { - function my_size (list(TEST)) -> uint -} declare x { value list(int) } +declare size { + function my_size (list(TEST)) -> uint +} =====> _==_( size( x~list(int)^x )~uint^my_size, 1u~uint -)~bool^equals - +)~bool^equals \ No newline at end of file diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java index af802f39a..c135ce323 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java @@ -14,7 +14,6 @@ package dev.cel.runtime; -import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import dev.cel.common.values.ProtoMessageLiteValueProvider; import dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor; @@ -28,16 +27,14 @@ */ @RunWith(TestParameterInjector.class) public class CelLiteInterpreterTest extends BaseInterpreterTest { - public CelLiteInterpreterTest(@TestParameter InterpreterTestOption testOption) { + public CelLiteInterpreterTest() { super( - testOption.celOptions.toBuilder().enableCelValue(true).build(), - testOption.useNativeCelType, CelRuntimeFactory.standardCelRuntimeBuilder() .setValueProvider( ProtoMessageLiteValueProvider.newInstance( TestAllTypesCelDescriptor.getDescriptor())) .addLibraries(CelOptionalLibrary.INSTANCE) - .setOptions(testOption.celOptions.toBuilder().enableCelValue(true).build()) + .setOptions(newBaseCelOptions().toBuilder().enableCelValue(true).build()) .build()); } diff --git a/runtime/src/test/java/dev/cel/runtime/CelValueInterpreterTest.java b/runtime/src/test/java/dev/cel/runtime/CelValueInterpreterTest.java index 2a7de836d..f56bb3012 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelValueInterpreterTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelValueInterpreterTest.java @@ -14,7 +14,6 @@ package dev.cel.runtime; -import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; // import com.google.testing.testsize.MediumTest; import dev.cel.testing.BaseInterpreterTest; @@ -25,9 +24,7 @@ @RunWith(TestParameterInjector.class) public class CelValueInterpreterTest extends BaseInterpreterTest { - public CelValueInterpreterTest(@TestParameter InterpreterTestOption testOption) { - super( - testOption.celOptions.toBuilder().enableCelValue(true).build(), - testOption.useNativeCelType); + public CelValueInterpreterTest() { + super(newBaseCelOptions().toBuilder().enableCelValue(true).build()); } } diff --git a/runtime/src/test/java/dev/cel/runtime/InterpreterTest.java b/runtime/src/test/java/dev/cel/runtime/InterpreterTest.java index 1f8c18c22..a6342c6e4 100644 --- a/runtime/src/test/java/dev/cel/runtime/InterpreterTest.java +++ b/runtime/src/test/java/dev/cel/runtime/InterpreterTest.java @@ -14,7 +14,6 @@ package dev.cel.runtime; -import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; // import com.google.testing.testsize.MediumTest; import dev.cel.testing.BaseInterpreterTest; @@ -23,9 +22,4 @@ /** Tests for {@link Interpreter} and related functionality. */ // @MediumTest @RunWith(TestParameterInjector.class) -public class InterpreterTest extends BaseInterpreterTest { - - public InterpreterTest(@TestParameter InterpreterTestOption testOption) { - super(testOption.celOptions, testOption.useNativeCelType); - } -} +public class InterpreterTest extends BaseInterpreterTest {} diff --git a/runtime/src/test/resources/delayedEvaluation.baseline b/runtime/src/test/resources/delayedEvaluation.baseline index 887611d2d..8604c359c 100644 --- a/runtime/src/test/resources/delayedEvaluation.baseline +++ b/runtime/src/test/resources/delayedEvaluation.baseline @@ -10,29 +10,29 @@ bindings: {} result: true Source: f_force(f_delay(1 + four)) == 5 +declare four { + value int +} declare f_delay { function f_delay (int) -> dyn } declare f_force { function f_force (dyn) -> int } -declare four { - value int -} =====> bindings: {four=4} result: true Source: [1, 2, 3].map(i, f_delay(i + four)).map(d, f_force(d)) == [5, 6, 7] +declare four { + value int +} declare f_delay { function f_delay (int) -> dyn } declare f_force { function f_force (dyn) -> int } -declare four { - value int -} =====> bindings: {four=4} result: true diff --git a/runtime/src/test/resources/longComprehension.baseline b/runtime/src/test/resources/longComprehension.baseline index 66ddeb07e..cb6b64e95 100644 --- a/runtime/src/test/resources/longComprehension.baseline +++ b/runtime/src/test/resources/longComprehension.baseline @@ -7,23 +7,23 @@ bindings: {} result: true Source: size(longlist.map(x, x+1)) == 1000 -declare constantLongList { - function constantLongList () -> list(int) -} declare longlist { value list(int) } +declare constantLongList { + function constantLongList () -> list(int) +} =====> bindings: {longlist=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999]} result: true Source: f_unleash(longlist.map(x, f_slow_inc(x)))[0] == 1 -declare constantLongList { - function constantLongList () -> list(int) -} declare longlist { value list(int) } +declare constantLongList { + function constantLongList () -> list(int) +} declare f_slow_inc { function f_slow_inc (int) -> int } diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index d4ace9a7c..c1b2a92b4 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -21,11 +21,6 @@ java_library( exports = ["//testing/src/main/java/dev/cel/testing:baseline_test_case"], ) -java_library( - name = "test_decls", - exports = ["//testing/src/main/java/dev/cel/testing:test_decls"], -) - java_library( name = "cel_baseline_test_case", exports = ["//testing/src/main/java/dev/cel/testing:cel_baseline_test_case"], diff --git a/testing/src/main/java/dev/cel/testing/BUILD.bazel b/testing/src/main/java/dev/cel/testing/BUILD.bazel index 22fec50d6..d9fa50c98 100644 --- a/testing/src/main/java/dev/cel/testing/BUILD.bazel +++ b/testing/src/main/java/dev/cel/testing/BUILD.bazel @@ -6,14 +6,6 @@ package( default_visibility = ["//testing:__pkg__"], ) -TEST_DECL_SOURCES = [ - "TestCelFunctionDeclWrapper.java", - "TestCelVariableDeclWrapper.java", - "TestDecl.java", - "TestProtoFunctionDeclWrapper.java", - "TestProtoVariableDeclWrapper.java", -] - java_library( name = "baseline_test_case", srcs = [ @@ -48,37 +40,21 @@ java_library( ], ) -java_library( - name = "test_decls", - srcs = TEST_DECL_SOURCES, - deps = [ - "//common:compiler_common", - "//common/types:cel_proto_types", - "//common/types:type_providers", - "//compiler:compiler_builder", - "@cel_spec//proto/cel/expr:checked_java_proto", - "@maven//:com_google_guava_guava", - ], -) - java_library( name = "cel_baseline_test_case", srcs = ["CelBaselineTestCase.java"], deps = [ ":baseline_test_case", - ":test_decls", "//:java_truth", "//common:cel_ast", "//common:compiler_common", "//common:options", - "//common/types:cel_proto_types", "//common/types:cel_types", "//common/types:message_type_provider", "//common/types:type_providers", "//compiler", "//compiler:compiler_builder", "//parser:macro", - "@cel_spec//proto/cel/expr:checked_java_proto", "@maven//:com_google_guava_guava", "@maven//:com_google_protobuf_protobuf_java", ], @@ -104,7 +80,7 @@ java_library( "//common/internal:file_descriptor_converter", "//common/internal:proto_time_utils", "//common/resources/testdata/proto3:standalone_global_enum_java_proto", - "//common/types:cel_proto_types", + "//common/types", "//common/types:type_providers", "//extensions:optional_library", "//runtime", diff --git a/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java b/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java index 0c29ea038..c098f710e 100644 --- a/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java +++ b/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java @@ -21,7 +21,6 @@ import dev.cel.expr.CheckedExpr; import dev.cel.expr.Type; -import dev.cel.expr.Type.AbstractType; import com.google.common.base.Ascii; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -62,8 +61,14 @@ import dev.cel.common.internal.DefaultDescriptorPool; import dev.cel.common.internal.FileDescriptorSetConverter; import dev.cel.common.internal.ProtoTimeUtils; -import dev.cel.common.types.CelProtoTypes; +import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; +import dev.cel.common.types.ListType; +import dev.cel.common.types.MapType; +import dev.cel.common.types.OpaqueType; +import dev.cel.common.types.SimpleType; +import dev.cel.common.types.StructTypeReference; +import dev.cel.common.types.TypeParamType; import dev.cel.expr.conformance.proto3.TestAllTypes; import dev.cel.expr.conformance.proto3.TestAllTypes.NestedEnum; import dev.cel.expr.conformance.proto3.TestAllTypes.NestedMessage; @@ -89,29 +94,6 @@ /** Base class for evaluation outputs that can be stored and used as a baseline test. */ public abstract class BaseInterpreterTest extends CelBaselineTestCase { - private static CelOptions.Builder newBaseOptions() { - return CelOptions.current() - .enableTimestampEpoch(true) - .enableHeterogeneousNumericComparisons(true) - .enableOptionalSyntax(true) - .comprehensionMaxIterations(1_000); - } - - /** Test options to supply for interpreter tests. */ - protected enum InterpreterTestOption { - CEL_TYPE(newBaseOptions().build(), true), - PROTO_TYPE(newBaseOptions().build(), false), - ; - - public final CelOptions celOptions; - public final boolean useNativeCelType; - - InterpreterTestOption(CelOptions celOptions, boolean useNativeCelType) { - this.celOptions = celOptions; - this.useNativeCelType = useNativeCelType; - } - } - protected static final Descriptor TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR = getDeserializedTestAllTypeDescriptor(); @@ -121,27 +103,39 @@ protected enum InterpreterTestOption { StandaloneGlobalEnum.getDescriptor().getFile(), TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR.getFile()); - private final CelOptions celOptions; + private static final CelOptions BASE_CEL_OPTIONS = + CelOptions.current() + .enableTimestampEpoch(true) + .enableHeterogeneousNumericComparisons(true) + .enableOptionalSyntax(true) + .comprehensionMaxIterations(1_000) + .build(); private CelRuntime celRuntime; - public BaseInterpreterTest(CelOptions celOptions, boolean useNativeCelType) { - this( - celOptions, - useNativeCelType, - CelRuntimeFactory.standardCelRuntimeBuilder() - .addLibraries(CelOptionalLibrary.INSTANCE) - .addFileTypes(TEST_FILE_DESCRIPTORS) - .setOptions(celOptions) - .build()); - } - - public BaseInterpreterTest( - CelOptions celOptions, boolean useNativeCelType, CelRuntime celRuntime) { - super(useNativeCelType); - this.celOptions = celOptions; + protected BaseInterpreterTest() { + this(newRuntime(BASE_CEL_OPTIONS)); + } + + protected BaseInterpreterTest(CelOptions celOptions) { + this(newRuntime(celOptions)); + } + + protected BaseInterpreterTest(CelRuntime celRuntime) { this.celRuntime = celRuntime; } + private static CelRuntime newRuntime(CelOptions celOptions) { + return CelRuntimeFactory.standardCelRuntimeBuilder() + .addLibraries(CelOptionalLibrary.INSTANCE) + .addFileTypes(TEST_FILE_DESCRIPTORS) + .setOptions(celOptions) + .build(); + } + + protected static CelOptions newBaseCelOptions() { + return BASE_CEL_OPTIONS; + } + @Override protected void prepareCompiler(CelTypeProvider typeProvider) { super.prepareCompiler(typeProvider); @@ -159,20 +153,24 @@ private CelAbstractSyntaxTree compileTestCase() { return ast; } + @CanIgnoreReturnValue private Object runTest() { return runTest(ImmutableMap.of()); } + @CanIgnoreReturnValue private Object runTest(CelVariableResolver variableResolver) { return runTestInternal(variableResolver, Optional.empty()); } /** Helper to run a test for configured instance variables. */ + @CanIgnoreReturnValue private Object runTest(Map input) { return runTestInternal(input, Optional.empty()); } /** Helper to run a test for configured instance variables. */ + @CanIgnoreReturnValue private Object runTest(Map input, CelLateFunctionBindings lateFunctionBindings) { return runTestInternal(input, Optional.of(lateFunctionBindings)); } @@ -227,11 +225,11 @@ public void arithmInt64() { source = "1 < 2 && 1 <= 1 && 2 > 1 && 1 >= 1 && 1 == 1 && 2 != 1"; runTest(); - declareVariable("x", CelProtoTypes.INT64); + declareVariable("x", SimpleType.INT); source = "1 + 2 - x * 3 / x + (x % 3)"; runTest(ImmutableMap.of("x", -5L)); - declareVariable("y", CelProtoTypes.DYN); + declareVariable("y", SimpleType.DYN); source = "x + y == 1"; runTest(extend(ImmutableMap.of("x", -5L), ImmutableMap.of("y", 6L))); } @@ -265,12 +263,12 @@ public void arithmUInt64() { source = "1u < 2u && 1u <= 1u && 2u > 1u && 1u >= 1u && 1u == 1u && 2u != 1u"; runTest(); - boolean useUnsignedLongs = celOptions.enableUnsignedLongs(); - declareVariable("x", CelProtoTypes.UINT64); + boolean useUnsignedLongs = BASE_CEL_OPTIONS.enableUnsignedLongs(); + declareVariable("x", SimpleType.UINT); source = "1u + 2u + x * 3u / x + (x % 3u)"; runTest(ImmutableMap.of("x", useUnsignedLongs ? UnsignedLong.valueOf(5L) : 5L)); - declareVariable("y", CelProtoTypes.DYN); + declareVariable("y", SimpleType.DYN); source = "x + y == 11u"; runTest( extend( @@ -307,11 +305,11 @@ public void arithmDouble() { source = "1.9 < 2.0 && 1.1 <= 1.1 && 2.0 > 1.9 && 1.1 >= 1.1 && 1.1 == 1.1 && 2.0 != 1.9"; runTest(); - declareVariable("x", CelProtoTypes.DOUBLE); + declareVariable("x", SimpleType.DOUBLE); source = "1.0 + 2.3 + x * 3.0 / x"; runTest(ImmutableMap.of("x", 3.33)); - declareVariable("y", CelProtoTypes.DYN); + declareVariable("y", SimpleType.DYN); source = "x + y == 9.99"; runTest(extend(ImmutableMap.of("x", 3.33d), ImmutableMap.of("y", 6.66))); } @@ -340,9 +338,9 @@ public void quantifiers() { @Test public void arithmTimestamp() { container = Type.getDescriptor().getFile().getPackage(); - declareVariable("ts1", CelProtoTypes.TIMESTAMP); - declareVariable("ts2", CelProtoTypes.TIMESTAMP); - declareVariable("d1", CelProtoTypes.DURATION); + declareVariable("ts1", SimpleType.TIMESTAMP); + declareVariable("ts2", SimpleType.TIMESTAMP); + declareVariable("d1", SimpleType.DURATION); Duration d1 = Duration.newBuilder().setSeconds(15).setNanos(25).build(); Timestamp ts1 = Timestamp.newBuilder().setSeconds(25).setNanos(35).build(); Timestamp ts2 = Timestamp.newBuilder().setSeconds(10).setNanos(10).build(); @@ -367,9 +365,9 @@ public void arithmTimestamp() { @Test public void arithmDuration() { container = Type.getDescriptor().getFile().getPackage(); - declareVariable("d1", CelProtoTypes.DURATION); - declareVariable("d2", CelProtoTypes.DURATION); - declareVariable("d3", CelProtoTypes.DURATION); + declareVariable("d1", SimpleType.DURATION); + declareVariable("d2", SimpleType.DURATION); + declareVariable("d3", SimpleType.DURATION); Duration d1 = Duration.newBuilder().setSeconds(15).setNanos(25).build(); Duration d2 = Duration.newBuilder().setSeconds(10).setNanos(20).build(); Duration d3 = Duration.newBuilder().setSeconds(25).setNanos(45).build(); @@ -388,7 +386,7 @@ public void arithmDuration() { @Test public void arithCrossNumericTypes() { - if (!celOptions.enableUnsignedLongs()) { + if (!BASE_CEL_OPTIONS.enableUnsignedLongs()) { skipBaselineVerification(); return; } @@ -407,7 +405,7 @@ public void arithCrossNumericTypes() { @Test public void booleans() { - declareVariable("x", CelProtoTypes.BOOL); + declareVariable("x", SimpleType.BOOL); source = "x ? 1 : 0"; runTest(ImmutableMap.of("x", true)); runTest(ImmutableMap.of("x", false)); @@ -418,7 +416,7 @@ public void booleans() { source = "(1 / 0 == 0 || true) == (true || 1 / 0 == 0)"; runTest(); - declareVariable("y", CelProtoTypes.INT64); + declareVariable("y", SimpleType.INT); source = "1 / y == 1 || true"; runTest(ImmutableMap.of("y", 0L)); @@ -476,7 +474,7 @@ public void booleans() { @Test public void booleans_error() { - declareVariable("y", CelProtoTypes.INT64); + declareVariable("y", SimpleType.INT); source = "1 / y == 1 || false"; runTest(ImmutableMap.of("y", 0L)); @@ -496,7 +494,7 @@ public void strings() throws Exception { source = "'a' < 'b' && 'a' <= 'b' && 'b' > 'a' && 'a' >= 'a' && 'a' == 'a' && 'a' != 'b'"; runTest(); - declareVariable("x", CelProtoTypes.STRING); + declareVariable("x", SimpleType.STRING); source = "'abc' + x == 'abcdef' && " + "x.endsWith('ef') && " @@ -512,13 +510,13 @@ public void messages() throws Exception { TestAllTypes.newBuilder() .setSingleNestedMessage(NestedMessage.newBuilder().setBb(43)) .build(); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); source = "x.single_nested_message.bb == 43 && has(x.single_nested_message)"; runTest(ImmutableMap.of("x", nestedMessage)); declareVariable( "single_nested_message", - CelProtoTypes.createMessage(NestedMessage.getDescriptor().getFullName())); + StructTypeReference.create(NestedMessage.getDescriptor().getFullName())); source = "single_nested_message.bb == 43"; runTest(ImmutableMap.of("single_nested_message", nestedMessage.getSingleNestedMessage())); @@ -543,7 +541,7 @@ public void optional() { source = "optional.unwrap([])"; runTest(); - declareVariable("str", CelProtoTypes.STRING); + declareVariable("str", SimpleType.STRING); source = "optional.unwrap([optional.none(), optional.of(1), optional.of(str)])"; runTest(ImmutableMap.of("str", "foo")); } @@ -568,7 +566,7 @@ public void has() throws Exception { .putMapInt32Int64(1, 2L) .setSingleNestedMessage(NestedMessage.newBuilder().setBb(43)) .build(); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); source = "has(x.single_int32) && !has(x.single_int64) && has(x.single_bool_wrapper)" + " && has(x.single_int32_wrapper) && !has(x.single_int64_wrapper)" @@ -582,8 +580,8 @@ public void has() throws Exception { @Test public void duration() throws Exception { - declareVariable("d1", CelProtoTypes.DURATION); - declareVariable("d2", CelProtoTypes.DURATION); + declareVariable("d1", SimpleType.DURATION); + declareVariable("d2", SimpleType.DURATION); Duration d1010 = Duration.newBuilder().setSeconds(10).setNanos(10).build(); Duration d1009 = Duration.newBuilder().setSeconds(10).setNanos(9).build(); Duration d0910 = Duration.newBuilder().setSeconds(9).setNanos(10).build(); @@ -620,8 +618,8 @@ public void duration() throws Exception { @Test public void timestamp() throws Exception { - declareVariable("t1", CelProtoTypes.TIMESTAMP); - declareVariable("t2", CelProtoTypes.TIMESTAMP); + declareVariable("t1", SimpleType.TIMESTAMP); + declareVariable("t2", SimpleType.TIMESTAMP); Timestamp ts1010 = Timestamp.newBuilder().setSeconds(10).setNanos(10).build(); Timestamp ts1009 = Timestamp.newBuilder().setSeconds(10).setNanos(9).build(); Timestamp ts0910 = Timestamp.newBuilder().setSeconds(9).setNanos(10).build(); @@ -659,16 +657,16 @@ public void timestamp() throws Exception { @Test public void packUnpackAny() { // The use of long values results in the incorrect type being serialized for a uint value. - if (!celOptions.enableUnsignedLongs()) { + if (!BASE_CEL_OPTIONS.enableUnsignedLongs()) { skipBaselineVerification(); return; } container = TestAllTypes.getDescriptor().getFile().getPackage(); - declareVariable("any", CelProtoTypes.ANY); - declareVariable("d", CelProtoTypes.DURATION); + declareVariable("any", SimpleType.ANY); + declareVariable("d", SimpleType.DURATION); declareVariable( - "message", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); - declareVariable("list", CelProtoTypes.createList(CelProtoTypes.DYN)); + "message", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); + declareVariable("list", ListType.create(SimpleType.DYN)); Duration duration = ProtoTimeUtils.fromSecondsToDuration(100); Any any = Any.pack(duration); TestAllTypes message = TestAllTypes.newBuilder().setSingleAny(any).build(); @@ -710,12 +708,12 @@ public void packUnpackAny() { @Test public void nestedEnums() { TestAllTypes nestedEnum = TestAllTypes.newBuilder().setSingleNestedEnum(NestedEnum.BAR).build(); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); container = TestAllTypes.getDescriptor().getFile().getPackage(); source = "x.single_nested_enum == TestAllTypes.NestedEnum.BAR"; runTest(ImmutableMap.of("x", nestedEnum)); - declareVariable("single_nested_enum", CelProtoTypes.INT64); + declareVariable("single_nested_enum", SimpleType.INT); source = "single_nested_enum == TestAllTypes.NestedEnum.BAR"; runTest(ImmutableMap.of("single_nested_enum", nestedEnum.getSingleNestedEnumValue())); @@ -726,15 +724,15 @@ public void nestedEnums() { @Test public void globalEnums() { - declareVariable("x", CelProtoTypes.INT64); + declareVariable("x", SimpleType.INT); source = "x == dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGAR"; runTest(ImmutableMap.of("x", StandaloneGlobalEnum.SGAR.getNumber())); } @Test public void lists() throws Exception { - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); - declareVariable("y", CelProtoTypes.INT64); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); + declareVariable("y", SimpleType.INT); container = TestAllTypes.getDescriptor().getFile().getPackage(); source = "([1, 2, 3] + x.repeated_int32)[3] == 4"; runTest(ImmutableMap.of("x", TestAllTypes.newBuilder().addRepeatedInt32(4).build())); @@ -751,7 +749,7 @@ public void lists() throws Exception { source = "!(4 in [1, 2, 3]) && 1 in [1, 2, 3]"; runTest(); - declareVariable("list", CelProtoTypes.createList(CelProtoTypes.INT64)); + declareVariable("list", ListType.create(SimpleType.INT)); source = "!(4 in list) && 1 in list"; runTest(ImmutableMap.of("list", ImmutableList.of(1L, 2L, 3L))); @@ -765,8 +763,8 @@ public void lists() throws Exception { @Test public void lists_error() { - declareVariable("y", CelProtoTypes.INT64); - declareVariable("list", CelProtoTypes.createList(CelProtoTypes.INT64)); + declareVariable("y", SimpleType.INT); + declareVariable("list", ListType.create(SimpleType.INT)); source = "list[3]"; runTest(ImmutableMap.of("y", 1L, "list", ImmutableList.of(1L, 2L, 3L))); @@ -774,7 +772,7 @@ public void lists_error() { @Test public void maps() throws Exception { - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); container = TestAllTypes.getDescriptor().getFile().getPackage(); source = "{1: 2, 3: 4}[3] == 4"; runTest(); @@ -794,8 +792,8 @@ public void maps() throws Exception { + ".oneof_type.payload.map_int32_int64[22] == 23"; runTest(ImmutableMap.of("x", TestAllTypes.newBuilder().putMapInt32Int64(22, 23).build())); - declareVariable("y", CelProtoTypes.INT64); - declareVariable("map", CelProtoTypes.createMap(CelProtoTypes.INT64, CelProtoTypes.INT64)); + declareVariable("y", SimpleType.INT); + declareVariable("map", MapType.create(SimpleType.INT, SimpleType.INT)); // Constant key in variable map. source = "!(4 in map) && 1 in map"; @@ -818,7 +816,7 @@ public void maps() throws Exception { runTest(); // Repeated key - expressions - declareVariable("b", CelProtoTypes.BOOL); + declareVariable("b", SimpleType.BOOL); source = "{b: 1, !b: 2, b: 3}[true]"; runTest(ImmutableMap.of("b", true)); } @@ -837,28 +835,16 @@ public void comprehension() throws Exception { @Test public void abstractType() { - Type typeParam = CelProtoTypes.createTypeParam("T"); - Type abstractType = - Type.newBuilder() - .setAbstractType( - AbstractType.newBuilder().setName("vector").addParameterTypes(typeParam)) - .build(); + CelType typeParam = TypeParamType.create("T"); + CelType abstractType = OpaqueType.create("vector", typeParam); + // Declare a function to create a vector. declareFunction( "vector", - globalOverload( - "vector", - ImmutableList.of(CelProtoTypes.createList(typeParam)), - ImmutableList.of("T"), - abstractType)); + globalOverload("vector", ImmutableList.of(ListType.create(typeParam)), abstractType)); // Declare a function to access element of a vector. declareFunction( - "at", - memberOverload( - "at", - ImmutableList.of(abstractType, CelProtoTypes.INT64), - ImmutableList.of("T"), - typeParam)); + "at", memberOverload("at", ImmutableList.of(abstractType, SimpleType.INT), typeParam)); // Add function bindings for above addFunctionBinding( CelFunctionBinding.from( @@ -887,14 +873,13 @@ public void abstractType() { public void namespacedFunctions() { declareFunction( "ns.func", - globalOverload( - "ns_func_overload", ImmutableList.of(CelProtoTypes.STRING), CelProtoTypes.INT64)); + globalOverload("ns_func_overload", ImmutableList.of(SimpleType.STRING), SimpleType.INT)); declareFunction( "member", memberOverload( "ns_member_overload", - ImmutableList.of(CelProtoTypes.INT64, CelProtoTypes.INT64), - CelProtoTypes.INT64)); + ImmutableList.of(SimpleType.INT, SimpleType.INT), + SimpleType.INT)); addFunctionBinding( CelFunctionBinding.from("ns_func_overload", String.class, s -> (long) s.length()), CelFunctionBinding.from("ns_member_overload", Long.class, Long.class, Long::sum)); @@ -934,12 +919,12 @@ public void namespacedFunctions() { @Test public void namespacedVariables() { container = "ns"; - declareVariable("ns.x", CelProtoTypes.INT64); + declareVariable("ns.x", SimpleType.INT); source = "x"; runTest(ImmutableMap.of("ns.x", 2)); container = "dev.cel.testing.testdata.proto3"; - Type messageType = CelProtoTypes.createMessage("cel.expr.conformance.proto3.TestAllTypes"); + CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes"); declareVariable("dev.cel.testing.testdata.proto3.msgVar", messageType); source = "msgVar.single_int32"; runTest( @@ -950,7 +935,7 @@ public void namespacedVariables() { @Test public void durationFunctions() { - declareVariable("d1", CelProtoTypes.DURATION); + declareVariable("d1", SimpleType.DURATION); Duration d1 = Duration.newBuilder().setSeconds(25 * 3600 + 59 * 60 + 1).setNanos(11000000).build(); Duration d2 = @@ -973,7 +958,7 @@ public void durationFunctions() { runTest(ImmutableMap.of("d1", d1)); runTest(ImmutableMap.of("d1", d2)); - declareVariable("val", CelProtoTypes.INT64); + declareVariable("val", SimpleType.INT); source = "d1.getHours() < val"; runTest(extend(ImmutableMap.of("d1", d1), ImmutableMap.of("val", 30L))); source = "d1.getMinutes() > val"; @@ -986,7 +971,7 @@ public void durationFunctions() { @Test public void timestampFunctions() { - declareVariable("ts1", CelProtoTypes.TIMESTAMP); + declareVariable("ts1", SimpleType.TIMESTAMP); container = Type.getDescriptor().getFile().getPackage(); Timestamp ts1 = Timestamp.newBuilder().setSeconds(1).setNanos(11000000).build(); Timestamp ts2 = ProtoTimeUtils.fromSecondsToTimestamp(-1); @@ -1091,7 +1076,7 @@ public void timestampFunctions() { source = "ts1.getMilliseconds(\"-8:00\")"; runTest(ImmutableMap.of("ts1", ts1)); - declareVariable("val", CelProtoTypes.INT64); + declareVariable("val", SimpleType.INT); source = "ts1.getFullYear() < val"; runTest(extend(ImmutableMap.of("ts1", ts1), ImmutableMap.of("val", 2013L))); source = "ts1.getMonth() < val"; @@ -1117,7 +1102,7 @@ public void timestampFunctions() { @Test public void unknownField() { container = TestAllTypes.getDescriptor().getFile().getPackage(); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); // Unknown field is accessed. source = "x.single_int32"; @@ -1149,7 +1134,7 @@ public void unknownField() { @Test public void unknownResultSet() { container = TestAllTypes.getDescriptor().getFile().getPackage(); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); TestAllTypes message = TestAllTypes.newBuilder() .setSingleString("test") @@ -1226,9 +1211,7 @@ public void unknownResultSet() { // dispatch test declareFunction( - "f", - memberOverload( - "f", Arrays.asList(CelProtoTypes.INT64, CelProtoTypes.INT64), CelProtoTypes.BOOL)); + "f", memberOverload("f", Arrays.asList(SimpleType.INT, SimpleType.INT), SimpleType.BOOL)); addFunctionBinding(CelFunctionBinding.from("f", Integer.class, Integer.class, Objects::equals)); // dispatch: unknown.f(1) ==> unknown @@ -1353,7 +1336,7 @@ public void unknownResultSet() { @Test public void timeConversions() { container = Type.getDescriptor().getFile().getPackage(); - declareVariable("t1", CelProtoTypes.TIMESTAMP); + declareVariable("t1", SimpleType.TIMESTAMP); source = "timestamp(\"1972-01-01T10:00:20.021-05:00\")"; runTest(); @@ -1386,8 +1369,8 @@ public void timeConversions_error() { @Test public void sizeTests() { container = Type.getDescriptor().getFile().getPackage(); - declareVariable("str", CelProtoTypes.STRING); - declareVariable("b", CelProtoTypes.BYTES); + declareVariable("str", SimpleType.STRING); + declareVariable("b", SimpleType.BYTES); source = "size(b) == 5 && b.size() == 5"; runTest(ImmutableMap.of("b", ByteString.copyFromUtf8("happy"))); @@ -1412,7 +1395,7 @@ public void nonstrictQuantifierTests() { source = "![0, 2, 4].all(x, 4/x != 2 && 4/(4-x) != 2)"; runTest(); - declareVariable("four", CelProtoTypes.INT64); + declareVariable("four", SimpleType.INT); // Condition is dynamic. source = "[0, 2, 4].exists(x, four/x == 2 && four/(four-x) == 2)"; @@ -1482,7 +1465,7 @@ public void regexpMatchingTests() { runTest(); // Constant string. - declareVariable("regexp", CelProtoTypes.STRING); + declareVariable("regexp", SimpleType.STRING); source = "matches(\"alpha\", regexp) == true"; runTest(ImmutableMap.of("regexp", "^al.*")); @@ -1510,7 +1493,7 @@ public void regexpMatchingTests() { runTest(ImmutableMap.of("regexp", ".*ha.$")); // Constant regexp. - declareVariable("s", CelProtoTypes.STRING); + declareVariable("s", SimpleType.STRING); source = "matches(s, \"^al.*\") == true"; runTest(ImmutableMap.of("s", "alpha")); @@ -1590,7 +1573,7 @@ public void int64Conversions_error() { public void uint64Conversions() { // The test case `uint(1e19)` succeeds with unsigned longs and fails with longs in a way that // cannot be easily tested. - if (!celOptions.enableUnsignedLongs()) { + if (!BASE_CEL_OPTIONS.enableUnsignedLongs()) { skipBaselineVerification(); return; } @@ -1753,7 +1736,7 @@ public void dyn_error() { @Test public void jsonValueTypes() { container = TestAllTypes.getDescriptor().getFile().getPackage(); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); // JSON bool selection. TestAllTypes xBool = @@ -1836,9 +1819,7 @@ public void jsonValueTypes() { declareFunction( "pair", globalOverload( - "pair", - ImmutableList.of(CelProtoTypes.STRING, CelProtoTypes.STRING), - CelProtoTypes.DYN)); + "pair", ImmutableList.of(SimpleType.STRING, SimpleType.STRING), SimpleType.DYN)); addFunctionBinding( CelFunctionBinding.from( "pair", @@ -1858,8 +1839,8 @@ public void jsonValueTypes() { @Test public void jsonConversions() { - declareVariable("ts", CelProtoTypes.TIMESTAMP); - declareVariable("du", CelProtoTypes.DURATION); + declareVariable("ts", SimpleType.TIMESTAMP); + declareVariable("du", SimpleType.DURATION); source = "google.protobuf.Struct { fields: {'timestamp': ts, 'duration': du } }"; runTest( ImmutableMap.of( @@ -1938,7 +1919,7 @@ public void wrappers() throws Exception { .setSingleUint32Wrapper(UInt32Value.of(12)) .setSingleUint64Wrapper(UInt64Value.of(34)); - declareVariable("x", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("x", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); source = "x.single_bool_wrapper == true && " + "x.single_bytes_wrapper == b'hi' && " @@ -1980,7 +1961,7 @@ public void wrappers() throws Exception { + "x.single_uint64_wrapper == null"; runTest(ImmutableMap.of("x", TestAllTypes.getDefaultInstance())); - declareVariable("dyn_var", CelProtoTypes.DYN); + declareVariable("dyn_var", SimpleType.DYN); source = "dyn_var"; runTest(ImmutableMap.of("dyn_var", NullValue.NULL_VALUE)); } @@ -1993,13 +1974,12 @@ public void longComprehension() { // Comprehension over compile-time constant long list. declareFunction( "constantLongList", - globalOverload( - "constantLongList", ImmutableList.of(), CelProtoTypes.createList(CelProtoTypes.INT64))); + globalOverload("constantLongList", ImmutableList.of(), ListType.create(SimpleType.INT))); source = "size(constantLongList().map(x, x+1)) == 1000"; runTest(); // Comprehension over long list that is not compile-time constant. - declareVariable("longlist", CelProtoTypes.createList(CelProtoTypes.INT64)); + declareVariable("longlist", ListType.create(SimpleType.INT)); source = "size(longlist.map(x, x+1)) == 1000"; runTest(ImmutableMap.of("longlist", l)); @@ -2012,14 +1992,11 @@ public void longComprehension() { CelFunctionBinding.from("f_unleash", Object.class, x -> x)); declareFunction( "f_slow_inc", - globalOverload("f_slow_inc", ImmutableList.of(CelProtoTypes.INT64), CelProtoTypes.INT64)); + globalOverload("f_slow_inc", ImmutableList.of(SimpleType.INT), SimpleType.INT)); declareFunction( "f_unleash", globalOverload( - "f_unleash", - ImmutableList.of(CelProtoTypes.createTypeParam("A")), - ImmutableList.of("A"), - CelProtoTypes.createTypeParam("A"))); + "f_unleash", ImmutableList.of(TypeParamType.create("A")), TypeParamType.create("A"))); source = "f_unleash(longlist.map(x, f_slow_inc(x)))[0] == 1"; runTest(ImmutableMap.of("longlist", l)); } @@ -2027,7 +2004,7 @@ public void longComprehension() { @Test public void maxComprehension() { // Comprehension over long list that is not compile-time constant. - declareVariable("longlist", CelProtoTypes.createList(CelProtoTypes.INT64)); + declareVariable("longlist", ListType.create(SimpleType.INT)); source = "size(longlist.map(x, x+1)) == 1000"; // Comprehension which exceeds the configured iteration limit. @@ -2091,7 +2068,7 @@ public void dynamicMessage_adapted() throws Exception { wrapperBindings.toByteArray(), DefaultDescriptorPool.INSTANCE.getExtensionRegistry())); - declareVariable("msg", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + declareVariable("msg", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); source = "msg.single_any"; assertThat(runTest(input)).isInstanceOf(NestedMessage.class); @@ -2119,11 +2096,11 @@ public void dynamicMessage_adapted() throws Exception { source = "msg.single_uint32_wrapper"; assertThat(runTest(input)) - .isInstanceOf(celOptions.enableUnsignedLongs() ? UnsignedLong.class : Long.class); + .isInstanceOf(BASE_CEL_OPTIONS.enableUnsignedLongs() ? UnsignedLong.class : Long.class); source = "msg.single_uint64_wrapper"; assertThat(runTest(input)) - .isInstanceOf(celOptions.enableUnsignedLongs() ? UnsignedLong.class : Long.class); + .isInstanceOf(BASE_CEL_OPTIONS.enableUnsignedLongs() ? UnsignedLong.class : Long.class); source = "msg.single_duration"; assertThat(runTest(input)).isInstanceOf(Duration.class); @@ -2168,10 +2145,10 @@ public void dynamicMessage_dynamicDescriptor() throws Exception { assertThat(runTest()).isInstanceOf(Double.class); source = "TestAllTypes { single_uint32_wrapper: 2u}.single_uint32_wrapper"; assertThat(runTest()) - .isInstanceOf(celOptions.enableUnsignedLongs() ? UnsignedLong.class : Long.class); + .isInstanceOf(BASE_CEL_OPTIONS.enableUnsignedLongs() ? UnsignedLong.class : Long.class); source = "TestAllTypes { single_uint64_wrapper: 2u}.single_uint64_wrapper"; assertThat(runTest()) - .isInstanceOf(celOptions.enableUnsignedLongs() ? UnsignedLong.class : Long.class); + .isInstanceOf(BASE_CEL_OPTIONS.enableUnsignedLongs() ? UnsignedLong.class : Long.class); source = "TestAllTypes { single_list_value: ['a', 1.5, true] }.single_list_value"; assertThat(runTest()).isInstanceOf(List.class); @@ -2195,7 +2172,7 @@ public void dynamicMessage_dynamicDescriptor() throws Exception { // Test any unpacking // With well-known type Any anyDuration = Any.pack(ProtoTimeUtils.fromSecondsToDuration(100)); - declareVariable("dur", CelProtoTypes.TIMESTAMP); + declareVariable("dur", SimpleType.TIMESTAMP); source = "TestAllTypes { single_any: dur }.single_any"; assertThat(runTest(ImmutableMap.of("dur", anyDuration))).isInstanceOf(Duration.class); // with custom message @@ -2203,16 +2180,16 @@ public void dynamicMessage_dynamicDescriptor() throws Exception { Any anyTestMsg = Any.pack(TestAllTypes.newBuilder().setSingleString("hello").build()); declareVariable( "any_packed_test_msg", - CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); source = "TestAllTypes { single_any: any_packed_test_msg }.single_any"; assertThat(runTest(ImmutableMap.of("any_packed_test_msg", anyTestMsg))) .isInstanceOf(TestAllTypes.class); // Test JSON map behavior declareVariable( - "test_msg", CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())); + "test_msg", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); declareVariable( - "dynamic_msg", CelProtoTypes.createMessage(TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR.getFullName())); + "dynamic_msg", StructTypeReference.create(TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR.getFullName())); DynamicMessage.Builder dynamicMessageBuilder = DynamicMessage.newBuilder(TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR); JsonFormat.parser().merge("{ 'map_string_string' : { 'foo' : 'bar' } }", dynamicMessageBuilder); @@ -2232,13 +2209,13 @@ public void dynamicMessage_dynamicDescriptor() throws Exception { globalOverload( "f_msg_generated", ImmutableList.of( - CelProtoTypes.createMessage(TestAllTypes.getDescriptor().getFullName())), - CelProtoTypes.BOOL), + StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())), + SimpleType.BOOL), globalOverload( "f_msg_dynamic", ImmutableList.of( - CelProtoTypes.createMessage(TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR.getFullName())), - CelProtoTypes.BOOL)); + StructTypeReference.create(TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR.getFullName())), + SimpleType.BOOL)); addFunctionBinding( CelFunctionBinding.from("f_msg_generated", TestAllTypes.class, x -> true), CelFunctionBinding.from("f_msg_dynamic", DynamicMessage.class, x -> true)); @@ -2259,12 +2236,12 @@ private static final class RecordedValues { private final Map recordedValues = new HashMap<>(); @CanIgnoreReturnValue - public Object record(String key, Object value) { + private Object record(String key, Object value) { recordedValues.put(key, value); return value; } - public ImmutableMap getRecordedValues() { + private ImmutableMap getRecordedValues() { return ImmutableMap.copyOf(recordedValues); } } @@ -2280,8 +2257,8 @@ public void lateBoundFunctions() throws Exception { "record", globalOverload( "record_string_dyn", - ImmutableList.of(CelProtoTypes.STRING, CelProtoTypes.DYN), - CelProtoTypes.DYN)); + ImmutableList.of(SimpleType.STRING, SimpleType.DYN), + SimpleType.DYN)); source = "record('foo', 'bar')"; assertThat(runTest(ImmutableMap.of(), lateBindings)).isEqualTo("bar"); assertThat(recordedValues.getRecordedValues()).containsExactly("foo", "bar"); diff --git a/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java b/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java index 04d6a3c58..0f4c9484a 100644 --- a/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java +++ b/testing/src/main/java/dev/cel/testing/CelBaselineTestCase.java @@ -14,21 +14,20 @@ package dev.cel.testing; -import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.truth.Truth.assertWithMessage; +import static dev.cel.common.CelFunctionDecl.newFunctionDeclaration; -import dev.cel.expr.Decl; -import dev.cel.expr.Decl.FunctionDecl.Overload; -import dev.cel.expr.Type; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.protobuf.DescriptorProtos.FileDescriptorSet; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.FileDescriptor; import dev.cel.common.CelAbstractSyntaxTree; +import dev.cel.common.CelFunctionDecl; import dev.cel.common.CelOptions; import dev.cel.common.CelOverloadDecl; import dev.cel.common.CelValidationException; -import dev.cel.common.types.CelProtoTypes; +import dev.cel.common.CelVarDecl; import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; import dev.cel.common.types.CelTypes; @@ -45,8 +44,8 @@ * to ensure consistent behavior on future test runs. */ public abstract class CelBaselineTestCase extends BaselineTestCase { - private final boolean declareWithCelTypes; - private final List decls = new ArrayList<>(); + private final List varDecls = new ArrayList<>(); + private final List functionDecls = new ArrayList<>(); protected String source; protected String container = ""; @@ -63,16 +62,7 @@ public abstract class CelBaselineTestCase extends BaselineTestCase { .comprehensionMaxIterations(1_000) .build(); - /** - * @param declareWithCelTypes If true, variables, functions and their overloads are declared - * internally using java native types {@link CelType}. This will also make the declarations to - * be loaded via their type equivalent APIs to the compiler. (Example: {@link - * CelCompilerBuilder#addFunctionDeclarations} vs. {@link CelCompilerBuilder#addDeclarations} - * ). Setting false will declare these using protobuf types {@link Type} instead. - */ - protected CelBaselineTestCase(boolean declareWithCelTypes) { - this.declareWithCelTypes = declareWithCelTypes; - } + protected CelBaselineTestCase() {} protected CelAbstractSyntaxTree prepareTest(List descriptors) { return prepareTest(new ProtoMessageTypeProvider(ImmutableSet.copyOf(descriptors))); @@ -107,31 +97,6 @@ private CelAbstractSyntaxTree prepareTest(CelTypeProvider typeProvider) { private void validateTestSetup() { assertWithMessage("The source field must be non-null").that(source).isNotNull(); - if (declareWithCelTypes) { - assertWithMessage( - "Test is incorrectly setup. Declarations must be done with CEL native types with" - + " declareWithCelTypes enabled") - .that( - decls.stream() - .filter( - d -> - d instanceof TestProtoFunctionDeclWrapper - || d instanceof TestProtoVariableDeclWrapper) - .count()) - .isEqualTo(0); - } else { - assertWithMessage( - "Test is incorrectly setup. Declarations must be done with proto types with" - + " declareWithCelTypes disabled.") - .that( - decls.stream() - .filter( - d -> - d instanceof TestCelFunctionDeclWrapper - || d instanceof TestCelVariableDeclWrapper) - .count()) - .isEqualTo(0); - } } protected void prepareCompiler(CelTypeProvider typeProvider) { @@ -149,9 +114,8 @@ protected void prepareCompiler(CelTypeProvider typeProvider) { celCompilerBuilder.setResultType(expectedType); } - // Add the function declarations appropriate to the type we're working with (Either CelType or - // Protobuf Type) - decls.forEach(d -> d.loadDeclsToCompiler(celCompilerBuilder)); + varDecls.forEach(celCompilerBuilder::addVarDeclarations); + functionDecls.forEach(celCompilerBuilder::addFunctionDeclarations); celCompiler = celCompilerBuilder.build(); } @@ -161,17 +125,14 @@ protected void prepareCompiler(CelTypeProvider typeProvider) { * * @param name Variable name */ - protected void declareVariable(String name, Type type) { - TestDecl varDecl = - this.declareWithCelTypes - ? new TestCelVariableDeclWrapper(name, type) - : new TestProtoVariableDeclWrapper(name, type); - decls.add(varDecl); + protected void declareVariable(String name, CelType type) { + varDecls.add(CelVarDecl.newVarDeclaration(name, type)); } /** Clears all function and variable declarations. */ protected void clearAllDeclarations() { - decls.clear(); + functionDecls.clear(); + varDecls.clear(); } /** Returns the test source description. */ @@ -182,56 +143,42 @@ protected String testSourceDescription() { protected void printTestSetup() { // Print the source. testOutput().printf("Source: %s%n", source); - for (TestDecl testDecl : decls) { - testOutput().println(formatDecl(testDecl.getDecl())); + for (CelVarDecl varDecl : varDecls) { + testOutput().println(formatVarDecl(varDecl)); } + for (CelFunctionDecl functionDecl : functionDecls) { + testOutput().println(formatFunctionDecl(functionDecl)); + } + testOutput().println("=====>"); } - protected String formatDecl(Decl decl) { + protected String formatFunctionDecl(CelFunctionDecl decl) { StringBuilder declStr = new StringBuilder(); - declStr.append(String.format("declare %s {%n", decl.getName())); - formatDeclImpl(decl, declStr); + declStr.append(String.format("declare %s {%n", decl.name())); + for (CelOverloadDecl overload : decl.overloads()) { + declStr.append( + String.format( + " function %s %s%n", + overload.overloadId(), + CelTypes.formatFunction( + overload.resultType(), + ImmutableList.copyOf(overload.parameterTypes()), + overload.isInstanceFunction(), + /* typeParamToDyn= */ false))); + } declStr.append("}"); return declStr.toString(); } - protected String formatDecl(String name, List declarations) { + protected String formatVarDecl(CelVarDecl decl) { StringBuilder declStr = new StringBuilder(); - declStr.append(String.format("declare %s {%n", name)); - for (Decl decl : declarations) { - formatDeclImpl(decl, declStr); - } + declStr.append(String.format("declare %s {%n", decl.name())); + declStr.append(String.format(" value %s%n", CelTypes.format(decl.type()))); declStr.append("}"); return declStr.toString(); } - private void formatDeclImpl(Decl decl, StringBuilder declStr) { - switch (decl.getDeclKindCase()) { - case IDENT: - declStr.append( - String.format(" value %s%n", CelProtoTypes.format(decl.getIdent().getType()))); - break; - case FUNCTION: - for (Overload overload : decl.getFunction().getOverloadsList()) { - declStr.append( - String.format( - " function %s %s%n", - overload.getOverloadId(), - CelTypes.formatFunction( - CelProtoTypes.typeToCelType(overload.getResultType()), - overload.getParamsList().stream() - .map(CelProtoTypes::typeToCelType) - .collect(toImmutableList()), - overload.getIsInstanceFunction(), - /* typeParamToDyn= */ false))); - } - break; - default: - break; - } - } - /** * Declares a function with one or more overloads * @@ -240,48 +187,34 @@ private void formatDeclImpl(Decl decl, StringBuilder declStr) { * is set, the protobuf overloads are internally converted into java native versions {@link * CelOverloadDecl}. */ - protected void declareFunction(String functionName, Overload... overloads) { - TestDecl functionDecl = - this.declareWithCelTypes - ? new TestCelFunctionDeclWrapper(functionName, overloads) - : new TestProtoFunctionDeclWrapper(functionName, overloads); - this.decls.add(functionDecl); + protected void declareFunction(String functionName, CelOverloadDecl... overloads) { + this.functionDecls.add(newFunctionDeclaration(functionName, overloads)); } - protected void declareGlobalFunction(String name, List paramTypes, Type resultType) { + protected void declareGlobalFunction(String name, List paramTypes, CelType resultType) { declareFunction(name, globalOverload(name, paramTypes, resultType)); } - protected void declareMemberFunction(String name, List paramTypes, Type resultType) { + protected void declareMemberFunction(String name, List paramTypes, CelType resultType) { declareFunction(name, memberOverload(name, paramTypes, resultType)); } - protected Overload memberOverload(String overloadId, List paramTypes, Type resultType) { - return overload(overloadId, paramTypes, resultType).setIsInstanceFunction(true).build(); - } - - protected Overload memberOverload( - String overloadId, List paramTypes, List typeParams, Type resultType) { - return overload(overloadId, paramTypes, resultType) - .addAllTypeParams(typeParams) - .setIsInstanceFunction(true) - .build(); - } - - protected Overload globalOverload(String overloadId, List paramTypes, Type resultType) { - return overload(overloadId, paramTypes, resultType).build(); + protected CelOverloadDecl memberOverload( + String overloadId, List paramTypes, CelType resultType) { + return overloadBuilder(overloadId, paramTypes, resultType).setIsInstanceFunction(true).build(); } - protected Overload globalOverload( - String overloadId, List paramTypes, List typeParams, Type resultType) { - return overload(overloadId, paramTypes, resultType).addAllTypeParams(typeParams).build(); + protected CelOverloadDecl globalOverload( + String overloadId, List paramTypes, CelType resultType) { + return overloadBuilder(overloadId, paramTypes, resultType).setIsInstanceFunction(false).build(); } - private Overload.Builder overload(String overloadId, List paramTypes, Type resultType) { - return Overload.newBuilder() + private CelOverloadDecl.Builder overloadBuilder( + String overloadId, List paramTypes, CelType resultType) { + return CelOverloadDecl.newBuilder() .setOverloadId(overloadId) .setResultType(resultType) - .addAllParams(paramTypes); + .addParameterTypes(paramTypes); } protected void printTestValidationError(CelValidationException error) { diff --git a/testing/src/main/java/dev/cel/testing/TestCelFunctionDeclWrapper.java b/testing/src/main/java/dev/cel/testing/TestCelFunctionDeclWrapper.java deleted file mode 100644 index db6349bfc..000000000 --- a/testing/src/main/java/dev/cel/testing/TestCelFunctionDeclWrapper.java +++ /dev/null @@ -1,45 +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.testing; - -import dev.cel.expr.Decl; -import dev.cel.expr.Decl.FunctionDecl.Overload; -import com.google.common.collect.Iterables; -import dev.cel.common.CelFunctionDecl; -import dev.cel.common.CelOverloadDecl; -import dev.cel.compiler.CelCompilerBuilder; -import java.util.Arrays; - -/** Wrapper for CEL native type based function declarations {@link CelFunctionDecl} */ -class TestCelFunctionDeclWrapper extends TestDecl { - private final CelFunctionDecl functionDecl; - - TestCelFunctionDeclWrapper(String functionName, Overload... overloads) { - this.functionDecl = - CelFunctionDecl.newFunctionDeclaration( - functionName, - Iterables.transform(Arrays.asList(overloads), CelOverloadDecl::overloadToCelOverload)); - } - - @Override - void loadDeclsToCompiler(CelCompilerBuilder compiler) { - compiler.addFunctionDeclarations(functionDecl); - } - - @Override - Decl getDecl() { - return CelFunctionDecl.celFunctionDeclToDecl(functionDecl); - } -} diff --git a/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java b/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java deleted file mode 100644 index 2cb94f87b..000000000 --- a/testing/src/main/java/dev/cel/testing/TestCelVariableDeclWrapper.java +++ /dev/null @@ -1,44 +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.testing; - -import dev.cel.expr.Decl; -import dev.cel.expr.Decl.IdentDecl; -import dev.cel.expr.Type; -import dev.cel.common.types.CelProtoTypes; -import dev.cel.common.types.CelType; -import dev.cel.compiler.CelCompilerBuilder; - -/** Wrapper for CEL native type based variable declarations */ -class TestCelVariableDeclWrapper extends TestDecl { - private final String name; - private final Type type; - - TestCelVariableDeclWrapper(String name, Type type) { - this.name = name; - this.type = type; - } - - @Override - void loadDeclsToCompiler(CelCompilerBuilder compiler) { - CelType celType = CelProtoTypes.typeToCelType(type); - compiler.addVar(name, celType); - } - - @Override - Decl getDecl() { - return Decl.newBuilder().setName(name).setIdent(IdentDecl.newBuilder().setType(type)).build(); - } -} diff --git a/testing/src/main/java/dev/cel/testing/TestDecl.java b/testing/src/main/java/dev/cel/testing/TestDecl.java deleted file mode 100644 index f17b7d235..000000000 --- a/testing/src/main/java/dev/cel/testing/TestDecl.java +++ /dev/null @@ -1,32 +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.testing; - -import dev.cel.expr.Decl; -import dev.cel.expr.Type; -import dev.cel.common.types.CelType; -import dev.cel.compiler.CelCompilerBuilder; - -/** - * Abstract declaration type that can work with Proto based types {@link Type} and CEL native types - * {@link CelType}. This abstraction is defined so that expression evaluations can be tested using - * both types. - */ -abstract class TestDecl { - - abstract void loadDeclsToCompiler(CelCompilerBuilder builder); - - abstract Decl getDecl(); -} diff --git a/testing/src/main/java/dev/cel/testing/TestProtoFunctionDeclWrapper.java b/testing/src/main/java/dev/cel/testing/TestProtoFunctionDeclWrapper.java deleted file mode 100644 index f48258059..000000000 --- a/testing/src/main/java/dev/cel/testing/TestProtoFunctionDeclWrapper.java +++ /dev/null @@ -1,44 +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.testing; - -import dev.cel.expr.Decl; -import dev.cel.expr.Decl.FunctionDecl; -import dev.cel.expr.Decl.FunctionDecl.Overload; -import dev.cel.compiler.CelCompilerBuilder; -import java.util.Arrays; - -/** Wrapper for proto-based function declarations. */ -class TestProtoFunctionDeclWrapper extends TestDecl { - private final Decl functionDecl; - - TestProtoFunctionDeclWrapper(String functionName, Overload... overloads) { - this.functionDecl = - Decl.newBuilder() - .setName(functionName) - .setFunction(FunctionDecl.newBuilder().addAllOverloads(Arrays.asList(overloads))) - .build(); - } - - @Override - void loadDeclsToCompiler(CelCompilerBuilder compiler) { - compiler.addDeclarations(functionDecl); - } - - @Override - Decl getDecl() { - return functionDecl; - } -} diff --git a/testing/src/main/java/dev/cel/testing/TestProtoVariableDeclWrapper.java b/testing/src/main/java/dev/cel/testing/TestProtoVariableDeclWrapper.java deleted file mode 100644 index 1c18b0f94..000000000 --- a/testing/src/main/java/dev/cel/testing/TestProtoVariableDeclWrapper.java +++ /dev/null @@ -1,40 +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.testing; - -import dev.cel.expr.Decl; -import dev.cel.expr.Decl.IdentDecl; -import dev.cel.expr.Type; -import dev.cel.compiler.CelCompilerBuilder; - -/** Wrapper for proto-based variable declarations. */ -class TestProtoVariableDeclWrapper extends TestDecl { - private final Decl varDecl; - - TestProtoVariableDeclWrapper(String name, Type type) { - varDecl = - Decl.newBuilder().setName(name).setIdent(IdentDecl.newBuilder().setType(type)).build(); - } - - @Override - void loadDeclsToCompiler(CelCompilerBuilder compiler) { - compiler.addDeclarations(varDecl); - } - - @Override - Decl getDecl() { - return varDecl; - } -}