Skip to content

Commit 952d04e

Browse files
jnthntatumcopybara-github
authored andcommitted
Enable conformance tests for two-var comprehensions.
Add existsOne (preferred alternative to older exists_one) PiperOrigin-RevId: 853942944
1 parent f7b9112 commit 952d04e

7 files changed

Lines changed: 40 additions & 1 deletion

File tree

bundle/src/test/java/dev/cel/bundle/CelEnvironmentExporterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void standardLibrarySubset_favorExclusion() throws Exception {
110110
FunctionSelector.create("matches", ImmutableSet.of()),
111111
FunctionSelector.create(
112112
"timestamp", ImmutableSet.of("string_to_timestamp"))))
113-
.setExcludedMacros(ImmutableSet.of("map", "filter"))
113+
.setExcludedMacros(ImmutableSet.of("map", "existsOne", "filter"))
114114
.build());
115115
}
116116

common/src/main/java/dev/cel/common/Operator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public enum Operator {
4545
HAS("has"),
4646
ALL("all"),
4747
EXISTS("exists"),
48+
// Prefer EXISTS_ONE_NEW in new usages.
4849
EXISTS_ONE("exists_one"),
50+
EXISTS_ONE_NEW("existsOne"),
4951
MAP("map"),
5052
FILTER("filter"),
5153
NOT_STRICTLY_FALSE("@not_strictly_false"),
@@ -109,6 +111,7 @@ public static Optional<Operator> find(String text) {
109111
.put(EQUALS.getFunction(), EQUALS)
110112
.put(EXISTS.getFunction(), EXISTS)
111113
.put(EXISTS_ONE.getFunction(), EXISTS_ONE)
114+
.put(EXISTS_ONE_NEW.getFunction(), EXISTS_ONE_NEW)
112115
.put(FILTER.getFunction(), FILTER)
113116
.put(GREATER.getFunction(), GREATER)
114117
.put(GREATER_EQUALS.getFunction(), GREATER_EQUALS)

conformance/src/test/java/dev/cel/conformance/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ _ALL_TESTS = [
7575
"@cel_spec//tests/simple:testdata/lists.textproto",
7676
"@cel_spec//tests/simple:testdata/logic.textproto",
7777
"@cel_spec//tests/simple:testdata/macros.textproto",
78+
"@cel_spec//tests/simple:testdata/macros2.textproto",
7879
"@cel_spec//tests/simple:testdata/math_ext.textproto",
7980
"@cel_spec//tests/simple:testdata/namespace.textproto",
8081
"@cel_spec//tests/simple:testdata/optionals.textproto",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public final class ConformanceTest extends Statement {
6666
.setOptions(OPTIONS)
6767
.addLibraries(
6868
CelExtensions.bindings(),
69+
CelExtensions.comprehensions(),
6970
CelExtensions.encoders(OPTIONS),
7071
CelExtensions.math(OPTIONS),
7172
CelExtensions.protos(),
@@ -80,6 +81,7 @@ public final class ConformanceTest extends Statement {
8081
.setOptions(OPTIONS)
8182
.addLibraries(
8283
CelExtensions.bindings(),
84+
CelExtensions.comprehensions(),
8385
CelExtensions.encoders(OPTIONS),
8486
CelExtensions.math(OPTIONS),
8587
CelExtensions.protos(),
@@ -106,10 +108,12 @@ private static CelChecker getChecker(SimpleTest test) throws Exception {
106108
.addFileTypes(dev.cel.expr.conformance.proto2.TestAllTypesExtensions.getDescriptor())
107109
.addLibraries(
108110
CelExtensions.bindings(),
111+
CelExtensions.comprehensions(),
109112
CelExtensions.encoders(OPTIONS),
110113
CelExtensions.math(OPTIONS),
111114
CelExtensions.sets(OPTIONS),
112115
CelExtensions.strings(),
116+
CelExtensions.comprehensions(),
113117
CelOptionalLibrary.INSTANCE)
114118
.addMessageTypes(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor())
115119
.addMessageTypes(dev.cel.expr.conformance.proto3.TestAllTypes.getDescriptor())
@@ -120,6 +124,7 @@ private static CelChecker getChecker(SimpleTest test) throws Exception {
120124
CelRuntimeFactory.standardCelRuntimeBuilder()
121125
.setOptions(OPTIONS)
122126
.addLibraries(
127+
CelExtensions.comprehensions(),
123128
CelExtensions.encoders(OPTIONS),
124129
CelExtensions.math(OPTIONS),
125130
CelExtensions.sets(OPTIONS),
@@ -182,6 +187,7 @@ public void evaluate() throws Throwable {
182187
CelValidationResult response = getParser(test).parse(test.getExpr(), test.getName());
183188
assertThat(response.hasError()).isFalse();
184189
response = getChecker(test).check(response.getAst());
190+
System.out.println("response: " + response.getIssueString());
185191
assertThat(response.hasError()).isFalse();
186192
Type resultType = CelProtoTypes.celTypeToType(response.getAst().getResultType());
187193

extensions/src/main/java/dev/cel/extensions/CelComprehensionsExtensions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public ImmutableSet<CelMacro> macros() {
159159
Operator.EXISTS_ONE.getFunction(),
160160
3,
161161
CelComprehensionsExtensions::expandExistsOneMacro),
162+
CelMacro.newReceiverMacro(
163+
Operator.EXISTS_ONE_NEW.getFunction(),
164+
3,
165+
CelComprehensionsExtensions::expandExistsOneMacro),
162166
CelMacro.newReceiverMacro(
163167
"transformList", 3, CelComprehensionsExtensions::transformListMacro),
164168
CelMacro.newReceiverMacro(

parser/src/main/java/dev/cel/parser/CelStandardMacro.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public enum CelStandardMacro {
5454
CelMacro.newReceiverMacro(
5555
Operator.EXISTS_ONE.getFunction(), 2, CelStandardMacro::expandExistsOneMacro)),
5656

57+
/**
58+
* Boolean comprehension which asserts that a predicate holds true for exactly one element in the
59+
* input range.
60+
*/
61+
EXISTS_ONE_NEW(
62+
CelMacro.newReceiverMacro(
63+
Operator.EXISTS_ONE_NEW.getFunction(), 2, CelStandardMacro::expandExistsOneMacro)),
64+
5765
/**
5866
* Comprehension which applies a transform to each element in the input range and produces a list
5967
* of equivalent size as output.

parser/src/test/java/dev/cel/parser/CelStandardMacroTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public void getFunction() {
3232
assertThat(CelStandardMacro.EXISTS.getFunction()).isEqualTo(Operator.EXISTS.getFunction());
3333
assertThat(CelStandardMacro.EXISTS_ONE.getFunction())
3434
.isEqualTo(Operator.EXISTS_ONE.getFunction());
35+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getFunction())
36+
.isEqualTo(Operator.EXISTS_ONE_NEW.getFunction());
3537
assertThat(CelStandardMacro.FILTER.getFunction()).isEqualTo(Operator.FILTER.getFunction());
3638
assertThat(CelStandardMacro.MAP.getFunction()).isEqualTo(Operator.MAP.getFunction());
3739
assertThat(CelStandardMacro.MAP_FILTER.getFunction()).isEqualTo(Operator.MAP.getFunction());
@@ -90,6 +92,21 @@ public void testExistsOne() {
9092
.isEqualTo(CelStandardMacro.EXISTS_ONE.getDefinition().getKey().hashCode());
9193
}
9294

95+
@Test
96+
public void testExistsOneNew() {
97+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getFunction())
98+
.isEqualTo(Operator.EXISTS_ONE_NEW.getFunction());
99+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().getArgumentCount()).isEqualTo(2);
100+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().isReceiverStyle()).isTrue();
101+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().getKey())
102+
.isEqualTo("existsOne:2:true");
103+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().isVariadic()).isFalse();
104+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().toString())
105+
.isEqualTo(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().getKey());
106+
assertThat(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().hashCode())
107+
.isEqualTo(CelStandardMacro.EXISTS_ONE_NEW.getDefinition().getKey().hashCode());
108+
}
109+
93110
@Test
94111
public void testMap2() {
95112
assertThat(CelStandardMacro.MAP.getFunction()).isEqualTo(Operator.MAP.getFunction());

0 commit comments

Comments
 (0)