Skip to content

Commit e7dff9b

Browse files
l46kokcopybara-github
authored andcommitted
Create a base class for extension tests
PiperOrigin-RevId: 904054337
1 parent d12fb7e commit e7dff9b

11 files changed

Lines changed: 197 additions & 329 deletions

extensions/src/main/java/dev/cel/extensions/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ java_library(
122122
":extension_library",
123123
"//checker:checker_builder",
124124
"//common:compiler_common",
125+
"//common:options",
125126
"//common/ast",
126127
"//common/exceptions:numeric_overflow",
127128
"//common/internal:comparison_functions",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ java_library(
1212
"//bundle:cel",
1313
"//bundle:cel_experimental_factory",
1414
"//common:cel_ast",
15+
"//common:cel_exception",
1516
"//common:compiler_common",
1617
"//common:container",
1718
"//common:options",

extensions/src/test/java/dev/cel/extensions/CelBindingsExtensionsTest.java

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2424
import com.google.testing.junit.testparameterinjector.TestParameters;
2525
import dev.cel.bundle.Cel;
26-
import dev.cel.common.CelAbstractSyntaxTree;
2726
import dev.cel.common.CelFunctionDecl;
2827
import dev.cel.common.CelOptions;
2928
import dev.cel.common.CelOverloadDecl;
@@ -36,36 +35,24 @@
3635
import dev.cel.parser.CelStandardMacro;
3736
import dev.cel.runtime.CelEvaluationException;
3837
import dev.cel.runtime.CelFunctionBinding;
39-
import dev.cel.testing.CelRuntimeFlavor;
4038
import java.util.Arrays;
4139
import java.util.List;
42-
import java.util.Map;
4340
import java.util.concurrent.atomic.AtomicInteger;
44-
import org.junit.Assume;
45-
import org.junit.Before;
4641
import org.junit.Test;
4742
import org.junit.runner.RunWith;
4843

4944
@RunWith(TestParameterInjector.class)
50-
public final class CelBindingsExtensionsTest {
51-
52-
@TestParameter public CelRuntimeFlavor runtimeFlavor;
53-
@TestParameter public boolean isParseOnly;
54-
55-
private Cel cel;
56-
57-
@Before
58-
public void setUp() {
59-
// Legacy runtime does not support parsed-only evaluation mode.
60-
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
61-
cel =
62-
runtimeFlavor
63-
.builder()
64-
.setOptions(CelOptions.current().enableHeterogeneousNumericComparisons(true).build())
65-
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
66-
.addCompilerLibraries(CelOptionalLibrary.INSTANCE, CelExtensions.bindings())
67-
.addRuntimeLibraries(CelOptionalLibrary.INSTANCE)
68-
.build();
45+
public final class CelBindingsExtensionsTest extends CelExtensionTestBase {
46+
47+
@Override
48+
protected Cel newCelEnv() {
49+
return runtimeFlavor
50+
.builder()
51+
.setOptions(CelOptions.current().enableHeterogeneousNumericComparisons(true).build())
52+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
53+
.addCompilerLibraries(CelOptionalLibrary.INSTANCE, CelExtensions.bindings())
54+
.addRuntimeLibraries(CelOptionalLibrary.INSTANCE)
55+
.build();
6956
}
7057

7158
@Test
@@ -331,21 +318,5 @@ public void lazyBinding_boundAttributeInNestedComprehension() throws Exception {
331318
assertThat(invocation.get()).isEqualTo(1);
332319
}
333320

334-
private Object eval(Cel cel, String expression) throws Exception {
335-
return eval(cel, expression, ImmutableMap.of());
336-
}
337-
338-
private Object eval(Cel cel, String expression, Map<String, ?> variables) throws Exception {
339-
CelAbstractSyntaxTree ast;
340-
if (isParseOnly) {
341-
ast = cel.parse(expression).getAst();
342-
} else {
343-
ast = cel.compile(expression).getAst();
344-
}
345-
return cel.createProgram(ast).eval(variables);
346-
}
347321

348-
private Object eval(String expression) throws Exception {
349-
return eval(this.cel, expression, ImmutableMap.of());
350-
}
351322
}

extensions/src/test/java/dev/cel/extensions/CelComprehensionsExtensionsTest.java

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.junit.Assert.assertThrows;
2020

2121
import com.google.common.base.Throwables;
22-
import com.google.common.collect.ImmutableMap;
2322
import com.google.testing.junit.testparameterinjector.TestParameter;
2423
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2524
import com.google.testing.junit.testparameterinjector.TestParameters;
@@ -40,15 +39,13 @@
4039
import dev.cel.parser.CelUnparserFactory;
4140
import dev.cel.runtime.CelEvaluationException;
4241
import dev.cel.testing.CelRuntimeFlavor;
43-
import java.util.Map;
4442
import org.junit.Assume;
45-
import org.junit.Before;
4643
import org.junit.Test;
4744
import org.junit.runner.RunWith;
4845

4946
/** Test for {@link CelExtensions#comprehensions()} */
5047
@RunWith(TestParameterInjector.class)
51-
public class CelComprehensionsExtensionsTest {
48+
public class CelComprehensionsExtensionsTest extends CelExtensionTestBase {
5249

5350
private static final CelOptions CEL_OPTIONS =
5451
CelOptions.current()
@@ -57,29 +54,21 @@ public class CelComprehensionsExtensionsTest {
5754
.populateMacroCalls(true)
5855
.build();
5956

60-
@TestParameter public CelRuntimeFlavor runtimeFlavor;
61-
@TestParameter public boolean isParseOnly;
62-
63-
private Cel cel;
64-
65-
@Before
66-
public void setUp() {
67-
// Legacy runtime does not support parsed-only evaluation mode.
68-
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
69-
this.cel =
70-
runtimeFlavor
71-
.builder()
72-
.setOptions(CEL_OPTIONS)
73-
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
74-
.addCompilerLibraries(CelExtensions.comprehensions())
75-
.addCompilerLibraries(CelExtensions.lists())
76-
.addCompilerLibraries(CelExtensions.strings())
77-
.addCompilerLibraries(CelOptionalLibrary.INSTANCE, CelExtensions.bindings())
78-
.addRuntimeLibraries(CelOptionalLibrary.INSTANCE)
79-
.addRuntimeLibraries(CelExtensions.lists())
80-
.addRuntimeLibraries(CelExtensions.strings())
81-
.addRuntimeLibraries(CelExtensions.comprehensions())
82-
.build();
57+
@Override
58+
protected Cel newCelEnv() {
59+
return runtimeFlavor
60+
.builder()
61+
.setOptions(CEL_OPTIONS)
62+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
63+
.addCompilerLibraries(CelExtensions.comprehensions())
64+
.addCompilerLibraries(CelExtensions.lists())
65+
.addCompilerLibraries(CelExtensions.strings())
66+
.addCompilerLibraries(CelOptionalLibrary.INSTANCE, CelExtensions.bindings())
67+
.addRuntimeLibraries(CelOptionalLibrary.INSTANCE)
68+
.addRuntimeLibraries(CelExtensions.lists())
69+
.addRuntimeLibraries(CelExtensions.strings())
70+
.addRuntimeLibraries(CelExtensions.comprehensions())
71+
.build();
8372
}
8473

8574
private static final CelUnparser UNPARSER = CelUnparserFactory.newUnparser();
@@ -376,17 +365,5 @@ public void mutableMapValue_select_missingKeyException() throws Exception {
376365
assertThat(e).hasCauseThat().hasMessageThat().contains("key 'b' is not present in map.");
377366
}
378367

379-
private Object eval(String expression) throws Exception {
380-
return eval(this.cel, expression, ImmutableMap.of());
381-
}
382368

383-
private Object eval(Cel cel, String expression, Map<String, ?> variables) throws Exception {
384-
CelAbstractSyntaxTree ast;
385-
if (isParseOnly) {
386-
ast = cel.parse(expression).getAst();
387-
} else {
388-
ast = cel.compile(expression).getAst();
389-
}
390-
return cel.createProgram(ast).eval(variables);
391-
}
392369
}

extensions/src/test/java/dev/cel/extensions/CelEncoderExtensionsTest.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,32 @@
1919
import static org.junit.Assert.assertThrows;
2020

2121
import com.google.common.collect.ImmutableMap;
22-
import com.google.testing.junit.testparameterinjector.TestParameter;
2322
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2423
import dev.cel.bundle.Cel;
25-
import dev.cel.common.CelAbstractSyntaxTree;
2624
import dev.cel.common.CelFunctionDecl;
2725
import dev.cel.common.CelOptions;
2826
import dev.cel.common.CelValidationException;
2927
import dev.cel.common.types.SimpleType;
3028
import dev.cel.common.values.CelByteString;
3129
import dev.cel.runtime.CelEvaluationException;
32-
import dev.cel.testing.CelRuntimeFlavor;
3330
import org.junit.Assume;
34-
import org.junit.Before;
3531
import org.junit.Test;
3632
import org.junit.runner.RunWith;
3733

3834
@RunWith(TestParameterInjector.class)
39-
public class CelEncoderExtensionsTest {
35+
public class CelEncoderExtensionsTest extends CelExtensionTestBase {
4036
private static final CelOptions CEL_OPTIONS =
4137
CelOptions.current().enableHeterogeneousNumericComparisons(true).build();
4238

43-
@TestParameter public CelRuntimeFlavor runtimeFlavor;
44-
@TestParameter public boolean isParseOnly;
45-
46-
private Cel cel;
47-
48-
@Before
49-
public void setUp() {
50-
// Legacy runtime does not support parsed-only evaluation mode.
51-
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
52-
this.cel =
53-
runtimeFlavor
54-
.builder()
55-
.setOptions(CEL_OPTIONS)
56-
.addCompilerLibraries(CelExtensions.encoders(CEL_OPTIONS))
57-
.addRuntimeLibraries(CelExtensions.encoders(CEL_OPTIONS))
58-
.addVar("stringVar", SimpleType.STRING)
59-
.build();
39+
@Override
40+
protected Cel newCelEnv() {
41+
return runtimeFlavor
42+
.builder()
43+
.setOptions(CEL_OPTIONS)
44+
.addCompilerLibraries(CelExtensions.encoders(CEL_OPTIONS))
45+
.addRuntimeLibraries(CelExtensions.encoders(CEL_OPTIONS))
46+
.addVar("stringVar", SimpleType.STRING)
47+
.build();
6048
}
6149

6250
@Test
@@ -132,12 +120,5 @@ public void decode_malformedBase64Char_throwsEvaluationException() throws Except
132120
assertThat(e).hasCauseThat().hasMessageThat().contains("Illegal base64 character");
133121
}
134122

135-
private Object eval(String expr) throws Exception {
136-
return eval(expr, ImmutableMap.of());
137-
}
138123

139-
private Object eval(String expr, ImmutableMap<String, Object> vars) throws Exception {
140-
CelAbstractSyntaxTree ast = isParseOnly ? cel.parse(expr).getAst() : cel.compile(expr).getAst();
141-
return cel.createProgram(ast).eval(vars);
142-
}
143124
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.extensions;
16+
17+
import com.google.common.collect.ImmutableMap;
18+
import com.google.testing.junit.testparameterinjector.TestParameter;
19+
import dev.cel.bundle.Cel;
20+
import dev.cel.common.CelAbstractSyntaxTree;
21+
import dev.cel.common.CelException;
22+
import dev.cel.testing.CelRuntimeFlavor;
23+
import java.util.Map;
24+
import org.junit.Assume;
25+
import org.junit.Before;
26+
27+
/**
28+
* Abstract base class for extension tests to facilitate executing tests with both legacy and
29+
* planner runtime, along with parsed-only and checked expression evaluations for the planner.
30+
*/
31+
abstract class CelExtensionTestBase {
32+
@TestParameter public CelRuntimeFlavor runtimeFlavor;
33+
@TestParameter public boolean isParseOnly;
34+
35+
@Before
36+
public void setUpBase() {
37+
// Legacy runtime does not support parsed-only evaluation.
38+
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
39+
this.cel = newCelEnv();
40+
}
41+
42+
protected Cel cel;
43+
44+
/**
45+
* Subclasses must implement this to provide a Cel instance configured with the specific
46+
* extensions being tested.
47+
*/
48+
protected abstract Cel newCelEnv();
49+
50+
protected Object eval(String expr) throws CelException {
51+
return eval(cel, expr, ImmutableMap.of());
52+
}
53+
54+
protected Object eval(String expr, Map<String, ?> variables) throws CelException {
55+
return eval(cel, expr, variables);
56+
}
57+
58+
protected Object eval(Cel cel, String expr) throws CelException {
59+
return eval(cel, expr, ImmutableMap.of());
60+
}
61+
62+
protected Object eval(Cel cel, String expr, Map<String, ?> variables) throws CelException {
63+
CelAbstractSyntaxTree ast = isParseOnly ? cel.parse(expr).getAst() : cel.compile(expr).getAst();
64+
return cel.createProgram(ast).eval(variables);
65+
}
66+
}

extensions/src/test/java/dev/cel/extensions/CelListsExtensionsTest.java

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,34 @@
1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.common.collect.ImmutableSortedMultiset;
2121
import com.google.common.collect.ImmutableSortedSet;
22-
import com.google.testing.junit.testparameterinjector.TestParameter;
2322
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2423
import com.google.testing.junit.testparameterinjector.TestParameters;
2524
import dev.cel.bundle.Cel;
26-
import dev.cel.bundle.CelBuilder;
27-
import dev.cel.common.CelAbstractSyntaxTree;
2825
import dev.cel.common.CelContainer;
2926
import dev.cel.common.CelValidationException;
3027
import dev.cel.common.CelValidationResult;
3128
import dev.cel.common.types.SimpleType;
3229
import dev.cel.expr.conformance.test.SimpleTest;
3330
import dev.cel.parser.CelStandardMacro;
3431
import dev.cel.runtime.CelEvaluationException;
35-
import dev.cel.testing.CelRuntimeFlavor;
36-
import java.util.Map;
3732
import org.junit.Assume;
38-
import org.junit.Before;
3933
import org.junit.Test;
4034
import org.junit.runner.RunWith;
4135

4236
@RunWith(TestParameterInjector.class)
43-
public class CelListsExtensionsTest {
44-
@TestParameter public CelRuntimeFlavor runtimeFlavor;
45-
@TestParameter public boolean isParseOnly;
37+
public class CelListsExtensionsTest extends CelExtensionTestBase {
4638

47-
private Cel cel;
48-
49-
@Before
50-
public void setUp() {
51-
// Legacy runtime does not support parsed-only evaluation mode.
52-
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
53-
this.cel = setupEnv(runtimeFlavor.builder());
39+
@Override
40+
protected Cel newCelEnv() {
41+
return runtimeFlavor
42+
.builder()
43+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
44+
.addCompilerLibraries(CelExtensions.lists())
45+
.addRuntimeLibraries(CelExtensions.lists())
46+
.setContainer(CelContainer.ofName("cel.expr.conformance.test"))
47+
.addMessageTypes(SimpleTest.getDescriptor())
48+
.addVar("non_list", SimpleType.DYN)
49+
.build();
5450
}
5551

5652
@Test
@@ -322,23 +318,5 @@ public void sortBy_throws_evaluationException(String expression, String expected
322318
.contains(expectedError);
323319
}
324320

325-
private static Cel setupEnv(CelBuilder celBuilder) {
326-
return celBuilder
327-
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
328-
.addCompilerLibraries(CelExtensions.lists())
329-
.addRuntimeLibraries(CelExtensions.lists())
330-
.setContainer(CelContainer.ofName("cel.expr.conformance.test"))
331-
.addMessageTypes(SimpleTest.getDescriptor())
332-
.addVar("non_list", SimpleType.DYN)
333-
.build();
334-
}
335-
336-
private Object eval(Cel cel, String expr) throws Exception {
337-
return eval(cel, expr, ImmutableMap.of());
338-
}
339321

340-
private Object eval(Cel cel, String expr, Map<String, ?> vars) throws Exception {
341-
CelAbstractSyntaxTree ast = isParseOnly ? cel.parse(expr).getAst() : cel.compile(expr).getAst();
342-
return cel.createProgram(ast).eval(vars);
343-
}
344322
}

0 commit comments

Comments
 (0)