Skip to content

Commit 8342a01

Browse files
l46kokcopybara-github
authored andcommitted
Add parsed-only evaluation test coverage to Regex Extensions
PiperOrigin-RevId: 899682453
1 parent 207dca5 commit 8342a01

3 files changed

Lines changed: 42 additions & 50 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ java_library(
4040
"//runtime:lite_runtime_factory",
4141
"//runtime:partial_vars",
4242
"//runtime:unknown_attributes",
43+
"//testing:cel_runtime_flavor",
4344
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
4445
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
4546
"@cel_spec//proto/cel/expr/conformance/test:simple_java_proto",

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

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,38 @@
2020
import com.google.testing.junit.testparameterinjector.TestParameter;
2121
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2222
import com.google.testing.junit.testparameterinjector.TestParameters;
23+
import dev.cel.bundle.Cel;
2324
import dev.cel.common.CelAbstractSyntaxTree;
2425
import dev.cel.common.CelFunctionDecl;
2526
import dev.cel.common.CelOptions;
26-
import dev.cel.compiler.CelCompiler;
27-
import dev.cel.compiler.CelCompilerFactory;
2827
import dev.cel.runtime.CelEvaluationException;
29-
import dev.cel.runtime.CelRuntime;
30-
import dev.cel.runtime.CelRuntimeFactory;
28+
import dev.cel.testing.CelRuntimeFlavor;
3129
import java.util.Optional;
30+
import org.junit.Assume;
31+
import org.junit.Before;
3232
import org.junit.Test;
3333
import org.junit.runner.RunWith;
3434

3535
@RunWith(TestParameterInjector.class)
3636
public final class CelRegexExtensionsTest {
3737

38-
private static final CelCompiler COMPILER =
39-
CelCompilerFactory.standardCelCompilerBuilder().addLibraries(CelExtensions.regex()).build();
40-
private static final CelRuntime RUNTIME =
41-
CelRuntimeFactory.standardCelRuntimeBuilder().addLibraries(CelExtensions.regex()).build();
38+
@TestParameter public CelRuntimeFlavor runtimeFlavor;
39+
@TestParameter public boolean isParseOnly;
40+
41+
private Cel cel;
42+
43+
@Before
44+
public void setUp() {
45+
// Legacy runtime does not support parsed-only evaluation mode.
46+
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
47+
this.cel =
48+
runtimeFlavor
49+
.builder()
50+
.addCompilerLibraries(CelExtensions.regex())
51+
.addRuntimeLibraries(CelExtensions.regex())
52+
.build();
53+
}
54+
4255

4356
@Test
4457
public void library() {
@@ -80,11 +93,7 @@ public void library() {
8093
public void replaceAll_success(String target, String regex, String replaceStr, String res)
8194
throws Exception {
8295
String expr = String.format("regex.replace('%s', '%s', '%s')", target, regex, replaceStr);
83-
CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile(expr).getAst());
84-
85-
Object result = program.eval();
86-
87-
assertThat(result).isEqualTo(res);
96+
assertThat(eval(expr)).isEqualTo(res);
8897
}
8998

9099
@Test
@@ -93,11 +102,7 @@ public void replace_nested_success() throws Exception {
93102
"regex.replace("
94103
+ " regex.replace('%(foo) %(bar) %2','%\\\\((\\\\w+)\\\\)','${\\\\1}'),"
95104
+ " '%(\\\\d+)', '$\\\\1')";
96-
CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile(expr).getAst());
97-
98-
Object result = program.eval();
99-
100-
assertThat(result).isEqualTo("${foo} ${bar} $2");
105+
assertThat(eval(expr)).isEqualTo("${foo} ${bar} $2");
101106
}
102107

103108
@Test
@@ -118,11 +123,7 @@ public void replace_nested_success() throws Exception {
118123
public void replaceCount_success(String t, String re, String rep, long i, String res)
119124
throws Exception {
120125
String expr = String.format("regex.replace('%s', '%s', '%s', %d)", t, re, rep, i);
121-
CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile(expr).getAst());
122-
123-
Object result = program.eval();
124-
125-
assertThat(result).isEqualTo(res);
126+
assertThat(eval(expr)).isEqualTo(res);
126127
}
127128

128129
@Test
@@ -131,10 +132,8 @@ public void replaceCount_success(String t, String re, String rep, long i, String
131132
public void replace_invalidRegex_throwsException(String target, String regex, String replaceStr)
132133
throws Exception {
133134
String expr = String.format("regex.replace('%s', '%s', '%s')", target, regex, replaceStr);
134-
CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst();
135-
136135
CelEvaluationException e =
137-
assertThrows(CelEvaluationException.class, () -> RUNTIME.createProgram(ast).eval());
136+
assertThrows(CelEvaluationException.class, () -> eval(expr));
138137

139138
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
140139
assertThat(e).hasCauseThat().hasMessageThat().contains("Failed to compile regex: ");
@@ -143,10 +142,8 @@ public void replace_invalidRegex_throwsException(String target, String regex, St
143142
@Test
144143
public void replace_invalidCaptureGroupReplaceStr_throwsException() throws Exception {
145144
String expr = "regex.replace('test', '(.)', '\\\\2')";
146-
CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst();
147-
148145
CelEvaluationException e =
149-
assertThrows(CelEvaluationException.class, () -> RUNTIME.createProgram(ast).eval());
146+
assertThrows(CelEvaluationException.class, () -> eval(expr));
150147

151148
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
152149
assertThat(e)
@@ -158,10 +155,8 @@ public void replace_invalidCaptureGroupReplaceStr_throwsException() throws Excep
158155
@Test
159156
public void replace_trailingBackslashReplaceStr_throwsException() throws Exception {
160157
String expr = "regex.replace('id=123', 'id=(?P<value>\\\\d+)', '\\\\')";
161-
CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst();
162-
163158
CelEvaluationException e =
164-
assertThrows(CelEvaluationException.class, () -> RUNTIME.createProgram(ast).eval());
159+
assertThrows(CelEvaluationException.class, () -> eval(expr));
165160

166161
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
167162
assertThat(e)
@@ -173,10 +168,8 @@ public void replace_trailingBackslashReplaceStr_throwsException() throws Excepti
173168
@Test
174169
public void replace_invalidGroupReferenceReplaceStr_throwsException() throws Exception {
175170
String expr = "regex.replace('id=123', 'id=(?P<value>\\\\d+)', '\\\\a')";
176-
CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst();
177-
178171
CelEvaluationException e =
179-
assertThrows(CelEvaluationException.class, () -> RUNTIME.createProgram(ast).eval());
172+
assertThrows(CelEvaluationException.class, () -> eval(expr));
180173

181174
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
182175
assertThat(e)
@@ -199,9 +192,7 @@ public void replace_invalidGroupReferenceReplaceStr_throwsException() throws Exc
199192
@TestParameters("{target: 'brand', regex: 'brand', expectedResult: 'brand'}")
200193
public void extract_success(String target, String regex, String expectedResult) throws Exception {
201194
String expr = String.format("regex.extract('%s', '%s')", target, regex);
202-
CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile(expr).getAst());
203-
204-
Object result = program.eval();
195+
Object result = eval(expr);
205196

206197
assertThat(result).isInstanceOf(Optional.class);
207198
assertThat((Optional<?>) result).hasValue(expectedResult);
@@ -213,9 +204,7 @@ public void extract_success(String target, String regex, String expectedResult)
213204
@TestParameters("{target: '', regex: '\\\\w+'}")
214205
public void extract_no_match(String target, String regex) throws Exception {
215206
String expr = String.format("regex.extract('%s', '%s')", target, regex);
216-
CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile(expr).getAst());
217-
218-
Object result = program.eval();
207+
Object result = eval(expr);
219208

220209
assertThat(result).isInstanceOf(Optional.class);
221210
assertThat((Optional<?>) result).isEmpty();
@@ -227,10 +216,8 @@ public void extract_no_match(String target, String regex) throws Exception {
227216
public void extract_multipleCaptureGroups_throwsException(String target, String regex)
228217
throws Exception {
229218
String expr = String.format("regex.extract('%s', '%s')", target, regex);
230-
CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst();
231-
232219
CelEvaluationException e =
233-
assertThrows(CelEvaluationException.class, () -> RUNTIME.createProgram(ast).eval());
220+
assertThrows(CelEvaluationException.class, () -> eval(expr));
234221

235222
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
236223
assertThat(e)
@@ -263,9 +250,7 @@ private enum ExtractAllTestCase {
263250

264251
@Test
265252
public void extractAll_success(@TestParameter ExtractAllTestCase testCase) throws Exception {
266-
CelAbstractSyntaxTree ast = COMPILER.compile(testCase.expr).getAst();
267-
268-
Object result = RUNTIME.createProgram(ast).eval();
253+
Object result = eval(testCase.expr);
269254

270255
assertThat(result).isEqualTo(testCase.expectedResult);
271256
}
@@ -281,15 +266,19 @@ public void extractAll_success(@TestParameter ExtractAllTestCase testCase) throw
281266
public void extractAll_multipleCaptureGroups_throwsException(String target, String regex)
282267
throws Exception {
283268
String expr = String.format("regex.extractAll('%s', '%s')", target, regex);
284-
CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst();
285-
286269
CelEvaluationException e =
287-
assertThrows(CelEvaluationException.class, () -> RUNTIME.createProgram(ast).eval());
270+
assertThrows(CelEvaluationException.class, () -> eval(expr));
288271

289272
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
290273
assertThat(e)
291274
.hasCauseThat()
292275
.hasMessageThat()
293276
.contains("Regular expression has more than one capturing group:");
294277
}
278+
279+
private Object eval(String expr) throws Exception {
280+
CelAbstractSyntaxTree ast =
281+
isParseOnly ? cel.parse(expr).getAst() : cel.compile(expr).getAst();
282+
return cel.createProgram(ast).eval();
283+
}
295284
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ java_library(
109109
java_library(
110110
name = "cel_runtime_flavor",
111111
srcs = ["CelRuntimeFlavor.java"],
112+
tags = [
113+
],
112114
deps = [
113115
"//bundle:cel",
114116
"//bundle:cel_experimental_factory",

0 commit comments

Comments
 (0)