Skip to content

Commit a59e85c

Browse files
l46kokcopybara-github
authored andcommitted
Internal Changes
PiperOrigin-RevId: 888846734
1 parent 690d082 commit a59e85c

File tree

9 files changed

+204
-63
lines changed

9 files changed

+204
-63
lines changed

conformance/src/test/java/dev/cel/conformance/ConformanceTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
1919
import static dev.cel.testing.utils.ExprValueUtils.DEFAULT_EXTENSION_REGISTRY;
20-
import static dev.cel.testing.utils.ExprValueUtils.DEFAULT_TYPE_REGISTRY;
2120
import static dev.cel.testing.utils.ExprValueUtils.fromValue;
2221
import static dev.cel.testing.utils.ExprValueUtils.toExprValue;
2322

@@ -29,6 +28,7 @@
2928
import com.google.common.base.Preconditions;
3029
import com.google.common.collect.ImmutableList;
3130
import com.google.common.collect.ImmutableMap;
31+
import com.google.protobuf.TypeRegistry;
3232
import dev.cel.checker.CelChecker;
3333
import dev.cel.common.CelContainer;
3434
import dev.cel.common.CelOptions;
@@ -84,6 +84,12 @@ public final class ConformanceTest extends Statement {
8484
CelExtensions.strings(),
8585
CelOptionalLibrary.INSTANCE);
8686

87+
static final TypeRegistry CONFORMANCE_TYPE_REGISTRY =
88+
TypeRegistry.newBuilder()
89+
.add(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor())
90+
.add(dev.cel.expr.conformance.proto3.TestAllTypes.getDescriptor())
91+
.build();
92+
8793
private static final CelParser PARSER_WITH_MACROS =
8894
CelParserFactory.standardCelParserBuilder()
8995
.setOptions(OPTIONS)
@@ -151,7 +157,7 @@ private static ImmutableMap<String, Object> getBindings(SimpleTest test) throws
151157
private static Object fromExprValue(ExprValue value) throws Exception {
152158
switch (value.getKindCase()) {
153159
case VALUE:
154-
return fromValue(value.getValue());
160+
return fromValue(value.getValue(), CONFORMANCE_TYPE_REGISTRY, DEFAULT_EXTENSION_REGISTRY);
155161
default:
156162
throw new IllegalArgumentException(
157163
String.format("Unexpected binding value kind: %s", value.getKindCase()));
@@ -224,7 +230,7 @@ public void evaluate() throws Throwable {
224230
assertThat(result)
225231
.ignoringRepeatedFieldOrderOfFieldDescriptors(
226232
MapValue.getDescriptor().findFieldByName("entries"))
227-
.unpackingAnyUsing(DEFAULT_TYPE_REGISTRY, DEFAULT_EXTENSION_REGISTRY)
233+
.unpackingAnyUsing(CONFORMANCE_TYPE_REGISTRY, DEFAULT_EXTENSION_REGISTRY)
228234
.isEqualTo(ExprValue.newBuilder().setValue(test.getValue()).build());
229235
break;
230236
case EVAL_ERROR:
@@ -237,7 +243,7 @@ public void evaluate() throws Throwable {
237243
assertThat(result)
238244
.ignoringRepeatedFieldOrderOfFieldDescriptors(
239245
MapValue.getDescriptor().findFieldByName("entries"))
240-
.unpackingAnyUsing(DEFAULT_TYPE_REGISTRY, DEFAULT_EXTENSION_REGISTRY)
246+
.unpackingAnyUsing(CONFORMANCE_TYPE_REGISTRY, DEFAULT_EXTENSION_REGISTRY)
241247
.isEqualTo(ExprValue.newBuilder().setValue(test.getTypedResult().getResult()).build());
242248
assertThat(resultType).isEqualTo(test.getTypedResult().getDeducedType());
243249
break;

conformance/src/test/java/dev/cel/conformance/ConformanceTestRunner.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package dev.cel.conformance;
1616

1717
import static dev.cel.testing.utils.ExprValueUtils.DEFAULT_EXTENSION_REGISTRY;
18-
import static dev.cel.testing.utils.ExprValueUtils.DEFAULT_TYPE_REGISTRY;
1918

2019
import com.google.common.base.Preconditions;
2120
import com.google.common.base.Splitter;
@@ -50,7 +49,9 @@ private static ImmutableSortedMap<String, SimpleTestFile> loadTestFiles() {
5049
SPLITTER.splitToList(System.getProperty("dev.cel.conformance.ConformanceTests.tests"));
5150
try {
5251
TextFormat.Parser parser =
53-
TextFormat.Parser.newBuilder().setTypeRegistry(DEFAULT_TYPE_REGISTRY).build();
52+
TextFormat.Parser.newBuilder()
53+
.setTypeRegistry(ConformanceTest.CONFORMANCE_TYPE_REGISTRY)
54+
.build();
5455
ImmutableSortedMap.Builder<String, SimpleTestFile> testFiles =
5556
ImmutableSortedMap.naturalOrder();
5657
for (String testPath : testPaths) {

policy/src/main/java/dev/cel/policy/CelPolicy.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Arrays;
2828
import java.util.Collection;
2929
import java.util.Collections;
30+
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Optional;
@@ -77,8 +78,7 @@ public abstract static class Builder {
7778

7879
public abstract Builder setPolicySource(CelPolicySource policySource);
7980

80-
// This should stay package-private to encourage add/set methods to be used instead.
81-
abstract ImmutableMap.Builder<String, Object> metadataBuilder();
81+
private final HashMap<String, Object> metadata = new HashMap<>();
8282

8383
public abstract Builder setMetadata(ImmutableMap<String, Object> value);
8484

@@ -90,6 +90,10 @@ public List<Import> imports() {
9090
return Collections.unmodifiableList(importList);
9191
}
9292

93+
public Map<String, Object> metadata() {
94+
return Collections.unmodifiableMap(metadata);
95+
}
96+
9397
@CanIgnoreReturnValue
9498
public Builder addImport(Import value) {
9599
importList.add(value);
@@ -104,13 +108,13 @@ public Builder addImports(Collection<Import> values) {
104108

105109
@CanIgnoreReturnValue
106110
public Builder putMetadata(String key, Object value) {
107-
metadataBuilder().put(key, value);
111+
metadata.put(key, value);
108112
return this;
109113
}
110114

111115
@CanIgnoreReturnValue
112116
public Builder putMetadata(Map<String, Object> map) {
113-
metadataBuilder().putAll(map);
117+
metadata.putAll(map);
114118
return this;
115119
}
116120

testing/src/main/java/dev/cel/testing/testrunner/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,13 @@ java_library(
163163
":result_matcher",
164164
"//:auto_value",
165165
"//bundle:cel",
166+
"//common:cel_descriptor_util",
166167
"//common:options",
167168
"//policy:parser",
168169
"//runtime",
170+
"//testing/testrunner:proto_descriptor_utils",
169171
"@maven//:com_google_guava_guava",
172+
"@maven//:com_google_protobuf_protobuf_java",
170173
],
171174
)
172175

testing/src/main/java/dev/cel/testing/testrunner/CelTestContext.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@
1414
package dev.cel.testing.testrunner;
1515

1616
import com.google.auto.value.AutoValue;
17+
import com.google.auto.value.extension.memoized.Memoized;
1718
import com.google.common.collect.ImmutableMap;
19+
import com.google.common.collect.ImmutableSet;
20+
import com.google.protobuf.Descriptors.Descriptor;
21+
import com.google.protobuf.Descriptors.FileDescriptor;
22+
import com.google.protobuf.ExtensionRegistry;
23+
import com.google.protobuf.TypeRegistry;
1824
import dev.cel.bundle.Cel;
1925
import dev.cel.bundle.CelFactory;
26+
import dev.cel.common.CelDescriptorUtil;
2027
import dev.cel.common.CelOptions;
2128
import dev.cel.policy.CelPolicyParser;
2229
import dev.cel.runtime.CelLateFunctionBindings;
30+
import dev.cel.testing.utils.ProtoDescriptorUtils;
31+
import java.io.IOException;
2332
import java.util.Map;
2433
import java.util.Optional;
2534

@@ -63,6 +72,19 @@ public abstract class CelTestContext {
6372
*/
6473
public abstract Optional<CelLateFunctionBindings> celLateFunctionBindings();
6574

75+
/** Interface for transforming bindings before evaluation. */
76+
@FunctionalInterface
77+
public interface BindingTransformer {
78+
ImmutableMap<String, Object> transform(ImmutableMap<String, Object> bindings) throws Exception;
79+
}
80+
81+
/**
82+
* The binding transformer for the CEL test.
83+
*
84+
* <p>This transformer is used to transform the bindings before evaluation.
85+
*/
86+
public abstract Optional<BindingTransformer> bindingTransformer();
87+
6688
/**
6789
* The variable bindings for the CEL test.
6890
*
@@ -99,6 +121,39 @@ public abstract class CelTestContext {
99121
*/
100122
public abstract Optional<String> fileDescriptorSetPath();
101123

124+
abstract ImmutableSet<Descriptor> messageTypes();
125+
126+
abstract ImmutableSet<FileDescriptor> fileTypes();
127+
128+
@Memoized
129+
public Optional<TypeRegistry> typeRegistry() {
130+
if (messageTypes().isEmpty() && fileTypes().isEmpty() && !fileDescriptorSetPath().isPresent()) {
131+
return Optional.empty();
132+
}
133+
TypeRegistry.Builder builder = TypeRegistry.newBuilder();
134+
if (!messageTypes().isEmpty()) {
135+
builder.add(messageTypes());
136+
}
137+
if (!fileTypes().isEmpty()) {
138+
builder.add(
139+
CelDescriptorUtil.getAllDescriptorsFromFileDescriptor(fileTypes())
140+
.messageTypeDescriptors());
141+
}
142+
if (fileDescriptorSetPath().isPresent()) {
143+
try {
144+
builder.add(
145+
ProtoDescriptorUtils.getAllDescriptorsFromJvm(fileDescriptorSetPath().get())
146+
.messageTypeDescriptors());
147+
} catch (IOException e) {
148+
throw new IllegalStateException(
149+
"Failed to load descriptors from path: " + fileDescriptorSetPath().get(), e);
150+
}
151+
}
152+
return Optional.of(builder.build());
153+
}
154+
155+
public abstract Optional<ExtensionRegistry> extensionRegistry();
156+
102157
/** Returns a builder for {@link CelTestContext} with the current instance's values. */
103158
public abstract Builder toBuilder();
104159

@@ -123,6 +178,8 @@ public abstract static class Builder {
123178
public abstract Builder setCelLateFunctionBindings(
124179
CelLateFunctionBindings celLateFunctionBindings);
125180

181+
public abstract Builder setBindingTransformer(BindingTransformer bindingTransformer);
182+
126183
public abstract Builder setVariableBindings(Map<String, Object> variableBindings);
127184

128185
public abstract Builder setResultMatcher(ResultMatcher resultMatcher);
@@ -133,6 +190,36 @@ public abstract Builder setCelLateFunctionBindings(
133190

134191
public abstract Builder setFileDescriptorSetPath(String fileDescriptorSetPath);
135192

193+
abstract ImmutableSet.Builder<Descriptor> messageTypesBuilder();
194+
195+
abstract ImmutableSet.Builder<FileDescriptor> fileTypesBuilder();
196+
197+
public Builder addMessageTypes(Descriptor... descriptors) {
198+
for (Descriptor d : descriptors) {
199+
messageTypesBuilder().add(d);
200+
}
201+
return this;
202+
}
203+
204+
public Builder addMessageTypes(Iterable<Descriptor> descriptors) {
205+
messageTypesBuilder().addAll(descriptors);
206+
return this;
207+
}
208+
209+
public Builder addFileTypes(FileDescriptor... fileDescriptors) {
210+
for (FileDescriptor fd : fileDescriptors) {
211+
fileTypesBuilder().add(fd);
212+
}
213+
return this;
214+
}
215+
216+
public Builder addFileTypes(Iterable<FileDescriptor> fileDescriptors) {
217+
fileTypesBuilder().addAll(fileDescriptors);
218+
return this;
219+
}
220+
221+
public abstract Builder setExtensionRegistry(ExtensionRegistry extensionRegistry);
222+
136223
public abstract CelTestContext build();
137224
}
138225
}

testing/src/main/java/dev/cel/testing/testrunner/CelTestSuiteTextProtoParser.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,36 @@
3636
* CelTestSuiteTextProtoParser intakes a textproto document that describes the structure of a CEL
3737
* test suite, parses it then creates a {@link CelTestSuite}.
3838
*/
39-
final class CelTestSuiteTextProtoParser {
39+
public final class CelTestSuiteTextProtoParser {
4040

4141
/** Creates a new instance of {@link CelTestSuiteTextProtoParser}. */
42-
static CelTestSuiteTextProtoParser newInstance() {
42+
public static CelTestSuiteTextProtoParser newInstance() {
4343
return new CelTestSuiteTextProtoParser();
4444
}
4545

46-
CelTestSuite parse(String textProto) throws IOException, CelTestSuiteException {
47-
TestSuite testSuite = parseTestSuite(textProto);
46+
public CelTestSuite parse(String textProto) throws IOException, CelTestSuiteException {
47+
return parse(
48+
textProto, TypeRegistry.getEmptyTypeRegistry(), ExtensionRegistry.getEmptyRegistry());
49+
}
50+
51+
public CelTestSuite parse(String textProto, TypeRegistry customTypeRegistry)
52+
throws IOException, CelTestSuiteException {
53+
return parse(textProto, customTypeRegistry, ExtensionRegistry.getEmptyRegistry());
54+
}
55+
56+
public CelTestSuite parse(
57+
String textProto, TypeRegistry customTypeRegistry, ExtensionRegistry customExtensionRegistry)
58+
throws IOException, CelTestSuiteException {
59+
TestSuite testSuite = parseTestSuite(textProto, customTypeRegistry, customExtensionRegistry);
4860
return parseCelTestSuite(testSuite);
4961
}
5062

51-
private TestSuite parseTestSuite(String textProto) throws IOException {
63+
private TestSuite parseTestSuite(
64+
String textProto, TypeRegistry customTypeRegistry, ExtensionRegistry customExtensionRegistry)
65+
throws IOException {
5266
String fileDescriptorSetPath = System.getProperty("file_descriptor_set_path");
53-
TypeRegistry typeRegistry = TypeRegistry.getEmptyTypeRegistry();
54-
ExtensionRegistry extensionRegistry = ExtensionRegistry.getEmptyRegistry();
67+
TypeRegistry typeRegistry = customTypeRegistry;
68+
ExtensionRegistry extensionRegistry = customExtensionRegistry;
5569
if (fileDescriptorSetPath != null) {
5670
extensionRegistry = RegistryUtils.getExtensionRegistry(fileDescriptorSetPath);
5771
typeRegistry = RegistryUtils.getTypeRegistry(fileDescriptorSetPath);

testing/src/main/java/dev/cel/testing/testrunner/CelTestSuiteYamlParser.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
* CelTestSuiteYamlParser intakes a YAML document that describes the structure of a CEL test suite,
4545
* parses it then creates a {@link CelTestSuite}.
4646
*/
47-
final class CelTestSuiteYamlParser {
47+
public final class CelTestSuiteYamlParser {
4848

4949
/** Creates a new instance of {@link CelTestSuiteYamlParser}. */
50-
static CelTestSuiteYamlParser newInstance() {
50+
public static CelTestSuiteYamlParser newInstance() {
5151
return new CelTestSuiteYamlParser();
5252
}
5353

54-
CelTestSuite parse(String celTestSuiteYamlContent) throws CelTestSuiteException {
54+
public CelTestSuite parse(String celTestSuiteYamlContent) throws CelTestSuiteException {
5555
return parseYaml(celTestSuiteYamlContent, "<input>");
5656
}
5757

@@ -86,7 +86,7 @@ private CelTestSuite parseYaml(String celTestSuiteYamlContent, String descriptio
8686
}
8787

8888
private CelTestSuite.Builder parseTestSuite(ParserContext<Node> ctx, Node node) {
89-
CelTestSuite.Builder builder = CelTestSuite.newBuilder();
89+
CelTestSuite.Builder builder = CelTestSuite.newBuilder().setName("");
9090
long id = ctx.collectMetadata(node);
9191
if (!assertYamlType(ctx, id, node, YamlNodeType.MAP)) {
9292
ctx.reportError(id, "Unknown test suite type: " + node.getTag());
@@ -110,6 +110,7 @@ private CelTestSuite.Builder parseTestSuite(ParserContext<Node> ctx, Node node)
110110
case "description":
111111
builder.setDescription(newString(ctx, valueNode));
112112
break;
113+
case "section":
113114
case "sections":
114115
builder.setSections(parseSections(ctx, valueNode));
115116
break;
@@ -143,7 +144,7 @@ private CelTestSection parseSection(ParserContext<Node> ctx, Node node) {
143144
return CelTestSection.newBuilder().build();
144145
}
145146

146-
CelTestSection.Builder celTestSectionBuilder = CelTestSection.newBuilder();
147+
CelTestSection.Builder celTestSectionBuilder = CelTestSection.newBuilder().setDescription("");
147148
MappingNode sectionNode = (MappingNode) node;
148149
for (NodeTuple nodeTuple : sectionNode.getValue()) {
149150
Node keyNode = nodeTuple.getKeyNode();
@@ -185,7 +186,7 @@ private ImmutableSet<CelTestCase> parseTests(ParserContext<Node> ctx, Node node)
185186

186187
private CelTestCase parseTestCase(ParserContext<Node> ctx, Node node) {
187188
long valueId = ctx.collectMetadata(node);
188-
CelTestCase.Builder celTestCaseBuilder = CelTestCase.newBuilder();
189+
CelTestCase.Builder celTestCaseBuilder = CelTestCase.newBuilder().setDescription("");
189190
if (!assertYamlType(ctx, valueId, node, YamlNodeType.MAP)) {
190191
ctx.reportError(valueId, "Testcase is not a map: " + node.getTag());
191192
return celTestCaseBuilder.build();

0 commit comments

Comments
 (0)