-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCelComprehensionsExtensionsTest.java
More file actions
392 lines (362 loc) · 17.4 KB
/
CelComprehensionsExtensionsTest.java
File metadata and controls
392 lines (362 loc) · 17.4 KB
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
// Copyright 2025 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.extensions;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertThrows;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameters;
import dev.cel.bundle.Cel;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelValidationException;
import dev.cel.common.CelValidationResult;
import dev.cel.common.exceptions.CelAttributeNotFoundException;
import dev.cel.common.exceptions.CelDivideByZeroException;
import dev.cel.common.exceptions.CelIndexOutOfBoundsException;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.TypeParamType;
import dev.cel.parser.CelMacro;
import dev.cel.parser.CelStandardMacro;
import dev.cel.parser.CelUnparser;
import dev.cel.parser.CelUnparserFactory;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.testing.CelRuntimeFlavor;
import java.util.Map;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Test for {@link CelExtensions#comprehensions()} */
@RunWith(TestParameterInjector.class)
public class CelComprehensionsExtensionsTest {
private static final CelOptions CEL_OPTIONS =
CelOptions.current()
.enableHeterogeneousNumericComparisons(true)
// Enable macro call population for unparsing
.populateMacroCalls(true)
.build();
@TestParameter public CelRuntimeFlavor runtimeFlavor;
@TestParameter public boolean isParseOnly;
private Cel cel;
@Before
public void setUp() {
// Legacy runtime does not support parsed-only evaluation mode.
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.LEGACY) && isParseOnly);
this.cel =
runtimeFlavor
.builder()
.setOptions(CEL_OPTIONS)
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
.addCompilerLibraries(CelExtensions.comprehensions())
.addCompilerLibraries(CelExtensions.lists())
.addCompilerLibraries(CelExtensions.strings())
.addCompilerLibraries(CelOptionalLibrary.INSTANCE, CelExtensions.bindings())
.addRuntimeLibraries(CelOptionalLibrary.INSTANCE)
.addRuntimeLibraries(CelExtensions.lists())
.addRuntimeLibraries(CelExtensions.strings())
.addRuntimeLibraries(CelExtensions.comprehensions())
.build();
}
private static final CelUnparser UNPARSER = CelUnparserFactory.newUnparser();
@Test
public void library() {
CelExtensionLibrary<?> library =
CelExtensions.getExtensionLibrary("comprehensions", CelOptions.DEFAULT);
assertThat(library.name()).isEqualTo("comprehensions");
assertThat(library.latest().version()).isEqualTo(0);
assertThat(library.version(0).functions().stream().map(CelFunctionDecl::name)).isEmpty();
assertThat(library.version(0).macros().stream().map(CelMacro::getFunction))
.containsAtLeast(
"all", "exists", "exists_one", "transformList", "transformMap", "transformMapEntry");
}
@Test
public void allMacro_twoVarComprehension_success(
@TestParameter({
// list.all()
"[1, 2, 3, 4].all(i, v, i < 5 && v > 0)",
"[1, 2, 3, 4].all(i, v, i < v)",
"[1, 2, 3, 4].all(i, v, i > v) == false",
"cel.bind(listA, [1, 2, 3, 4], cel.bind(listB, [1, 2, 3, 4, 5], listA.all(i, v,"
+ " listB[?i].hasValue() && listB[i] == v)))",
"cel.bind(listA, [1, 2, 3, 4, 5, 6], cel.bind(listB, [1, 2, 3, 4, 5], listA.all(i, v,"
+ " listB[?i].hasValue() && listB[i] == v))) == false",
// map.all()
"{'hello': 'world', 'hello!': 'world'}.all(k, v, k.startsWith('hello') && v =="
+ " 'world')",
"{'hello': 'world', 'hello!': 'worlds'}.all(k, v, k.startsWith('hello') &&"
+ " v.endsWith('world')) == false",
"{'a': 1, 'b': 2}.all(k, v, k.startsWith('a') && v == 1) == false",
})
String expr)
throws Exception {
assertThat(eval(expr)).isEqualTo(true);
}
@Test
public void existsMacro_twoVarComprehension_success(
@TestParameter({
// list.exists()
"[1, 2, 3, 4].exists(i, v, i > 2 && v < 5)",
"[10, 1, 30].exists(i, v, i == v)",
"[].exists(i, v, true) == false",
"cel.bind(l, ['hello', 'world', 'hello!', 'worlds'], l.exists(i, v,"
+ " v.startsWith('hello') && l[?(i+1)].optMap(next,"
+ " next.endsWith('world')).orValue(false)))",
// map.exists()
"{'hello': 'world', 'hello!': 'worlds'}.exists(k, v, k.startsWith('hello') &&"
+ " v.endsWith('world'))",
"{}.exists(k, v, true) == false",
"{'a': 1, 'b': 2}.exists(k, v, v == 3) == false",
"{'a': 'b', 'c': 'c'}.exists(k, v, k == v)"
})
String expr)
throws Exception {
assertThat(eval(expr)).isEqualTo(true);
}
@Test
public void exists_oneMacro_twoVarComprehension_success(
@TestParameter({
// list.exists_one()
"[0, 5, 6].exists_one(i, v, i == v)",
"[0, 1, 5].exists_one(i, v, i == v) == false",
"[10, 11, 12].exists_one(i, v, i == v) == false",
"cel.bind(l, ['hello', 'world', 'hello!', 'worlds'], l.exists_one(i, v,"
+ " v.startsWith('hello') && l[?(i+1)].optMap(next,"
+ " next.endsWith('world')).orValue(false)))",
"cel.bind(l, ['hello', 'goodbye', 'hello!', 'goodbye'], l.exists_one(i, v,"
+ " v.startsWith('hello') && l[?(i+1)].optMap(next, next =="
+ " 'goodbye').orValue(false))) == false",
// map.exists_one()
"{'hello': 'world', 'hello!': 'worlds'}.exists_one(k, v, k.startsWith('hello') &&"
+ " v.endsWith('world'))",
"{'hello': 'world', 'hello!': 'wow, world'}.exists_one(k, v, k.startsWith('hello') &&"
+ " v.endsWith('world')) == false",
"{'a': 1, 'b': 1}.exists_one(k, v, v == 2) == false"
})
String expr)
throws Exception {
assertThat(eval(expr)).isEqualTo(true);
}
@Test
public void transformListMacro_twoVarComprehension_success(
@TestParameter({
// list.transformList()
"[1, 2, 3].transformList(i, v, (i * v) + v) == [1, 4, 9]",
"[1, 2, 3].transformList(i, v, i % 2 == 0, (i * v) + v) == [1, 9]",
"[1, 2, 3].transformList(i, v, i > 0 && v < 3, (i * v) + v) == [4]",
"[1, 2, 3].transformList(i, v, i % 2 == 0, (i * v) + v) == [1, 9]",
"[1, 2, 3].transformList(i, v, (i * v) + v) == [1, 4, 9]",
"[-1, -2, -3].transformList(i, v, [1, 2].transformList(i, v, i + v)) == [[1, 3], [1,"
+ " 3], [1, 3]]",
// map.transformList()
"{'greeting': 'hello', 'farewell': 'goodbye'}.transformList(k, _, k).sort() =="
+ " ['farewell', 'greeting']",
"{'greeting': 'hello', 'farewell': 'goodbye'}.transformList(_, v, v).sort() =="
+ " ['goodbye', 'hello']"
})
String expr)
throws Exception {
assertThat(eval(expr)).isEqualTo(true);
}
@Test
public void transformMapMacro_twoVarComprehension_success(
@TestParameter({
// list.transformMap()
"['Hello', 'world'].transformMap(i, v, [v.lowerAscii()]) == {0: ['hello'], 1:"
+ " ['world']}",
"['world', 'Hello'].transformMap(i, v, [v.lowerAscii()]).transformList(k, v,"
+ " v).flatten().sort() == ['hello', 'world']",
"[1, 2, 3].transformMap(indexVar, valueVar, (indexVar * valueVar) + valueVar) == {0:"
+ " 1, 1: 4, 2: 9}",
"[1, 2, 3].transformMap(indexVar, valueVar, indexVar % 2 == 0, (indexVar * valueVar)"
+ " + valueVar) == {0: 1, 2: 9}",
// map.transformMap()
"{'greeting': 'hello'}.transformMap(k, v, v + '!') == {'greeting': 'hello!'}",
"dyn({'greeting': 'hello'}).transformMap(k, v, v + '!') == {'greeting': 'hello!'}",
"{'hello': 'world', 'goodbye': 'cruel world'}.transformMap(k, v, v.startsWith('world'),"
+ " v + '!') == {'hello': 'world!'}",
"{}.transformMap(k, v, v + '!') == {}"
})
String expr)
throws Exception {
assertThat(eval(expr)).isEqualTo(true);
}
@Test
public void transformMapEntryMacro_twoVarComprehension_success(
@TestParameter({
// list.transformMapEntry()
"'key1:value1 key2:value2 key3:value3'.split(' ').transformMapEntry(i, v,"
+ " cel.bind(entry, v.split(':'),entry.size() == 2 ? {entry[0]: entry[1]} : {})) =="
+ " {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}",
"'key1:value1:extra key2:value2 key3'.split(' ').transformMapEntry(i, v,"
+ " cel.bind(entry, v.split(':'), {?entry[0]: entry[?1]})) == {'key1': 'value1',"
+ " 'key2': 'value2'}",
// map.transformMapEntry()
"{'hello': 'world', 'greetings': 'tacocat'}.transformMapEntry(k, v, {}) == {}",
"{'a': 1, 'b': 2}.transformMapEntry(k, v, {k: v}) == {'a': 1, 'b': 2}",
"{'a': 1, 'b': 2}.transformMapEntry(k, v, {k + '_new': v * 2}) == {'a_new': 2,"
+ " 'b_new': 4}",
"{'a': 1, 'b': 2, 'c': 3}.transformMapEntry(k, v, v % 2 == 1, {k: v * 10}) == {'a': 10,"
+ " 'c': 30}",
"{'a': 1, 'b': 2}.transformMapEntry(k, v, k == 'a', {k + '_filtered': v}) =="
+ " {'a_filtered': 1}",
})
String expr)
throws Exception {
assertThat(eval(expr)).isEqualTo(true);
}
@Test
public void comprehension_onTypeParam_success() throws Exception {
Assume.assumeFalse(isParseOnly);
Cel customCel =
runtimeFlavor
.builder()
.setOptions(CEL_OPTIONS)
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
.addCompilerLibraries(CelExtensions.comprehensions())
.addVar("items", TypeParamType.create("T"))
.build();
CelAbstractSyntaxTree ast = customCel.compile("items.all(i, v, v > 0)").getAst();
assertThat(ast.getResultType()).isEqualTo(SimpleType.BOOL);
}
@Test
public void unparseAST_twoVarComprehension(
@TestParameter({
"cel.bind(listA, [1, 2, 3, 4], cel.bind(listB, [1, 2, 3, 4, 5], listA.all(i, v,"
+ " listB[?i].hasValue() && listB[i] == v)))",
"[1, 2, 3, 4].exists(i, v, i > 2 && v < 5)",
"{\"a\": 1, \"b\": 1}.exists_one(k, v, v == 2) == false",
"[1, 2, 3].transformList(i, v, i > 0 && v < 3, i * v + v) == [4]",
"[1, 2, 2].transformList(i, v, i / 2 == 1)",
"{\"a\": \"b\", \"c\": \"d\"}.exists_one(k, v, k == \"b\" || v == \"b\")",
"{\"a\": \"b\", \"c\": \"d\"}.exists(k, v, k == \"b\" || v == \"b\")",
"[null, null, \"hello\", string].all(i, v, i == 0 || type(v) != int)"
})
String expr)
throws Exception {
CelAbstractSyntaxTree ast = isParseOnly ? cel.parse(expr).getAst() : cel.compile(expr).getAst();
String unparsed = UNPARSER.unparse(ast);
assertThat(unparsed).isEqualTo(expr);
}
@Test
@TestParameters(
"{expr: '[].exists_one(i.j, k, i.j < k)', err: 'The argument must be a simple name'}")
@TestParameters(
"{expr: '[].exists_one(i, [k], i < [k])', err: 'The argument must be a simple name'}")
@TestParameters(
"{expr: '1.exists_one(j, j < 5)', err: 'cannot be range of a comprehension (must be list,"
+ " map, or dynamic)'}")
@TestParameters(
"{expr: '1.exists_one(j, k, j < k)', err: 'cannot be range of a comprehension (must be list,"
+ " map, or dynamic)'}")
@TestParameters(
"{expr: '[].transformList(__result__, i, __result__ < i)', err: 'The iteration variable"
+ " __result__ overwrites accumulator variable'}")
@TestParameters(
"{expr: '[].exists(__result__, i, __result__ < i)', err: 'The iteration variable __result__"
+ " overwrites accumulator variable'}")
@TestParameters(
"{expr: '[].exists(j, __result__, __result__ < j)', err: 'The iteration variable __result__"
+ " overwrites accumulator variable'}")
@TestParameters(
"{expr: 'no_such_var.all(i, v, v > 0)', err: \"undeclared reference to 'no_such_var'\"}")
@TestParameters(
"{expr: '{}.transformMap(i.j, k, i.j + k)', err: 'argument must be a simple name'}")
@TestParameters(
"{expr: '{}.transformMap(i, k.j, i + k.j)', err: 'argument must be a simple name'}")
@TestParameters(
"{expr: '{}.transformMapEntry(j, i.k, {j: i.k})', err: 'argument must be a simple name'}")
@TestParameters(
"{expr: '{}.transformMapEntry(i.j, k, {k: i.j})', err: 'argument must be a simple name'}")
@TestParameters(
"{expr: '{}.transformMapEntry(j, k, \"bad filter\", {k: j})', err: 'no matching overload'}")
@TestParameters(
"{expr: '[1, 2].transformList(i, v, v % 2 == 0 ? [v] : v)', err: 'no matching overload'}")
@TestParameters(
"{expr: \"{'hello': 'world', 'greetings': 'tacocat'}.transformMapEntry(k, v, []) == {}\","
+ " err: 'no matching overload'}")
public void twoVarComprehension_compilerErrors(String expr, String err) throws Exception {
Assume.assumeFalse(isParseOnly);
CelValidationResult result = cel.compile(expr);
CelValidationException e = assertThrows(CelValidationException.class, () -> result.getAst());
assertThat(e).hasMessageThat().contains(err);
}
@Test
@TestParameters(
"{expr: \"['a:1', 'b:2', 'a:3'].transformMapEntry(i, v, cel.bind(p, v.split(':'), {p[0]:"
+ " p[1]})) == {'a': '3', 'b': '2'}\", err: \"insert failed: key 'a' already exists\"}")
@TestParameters(
"{expr: '[1, 1].transformMapEntry(i, v, {v: i})', err: \"insert failed: key '1' already"
+ " exists\"}")
@TestParameters(
"{expr: \"{'a': 65, 'b': 65u}.transformMapEntry(i, v, {v: i})\", err: \"insert failed: key"
+ " '65' already exists\"}")
@TestParameters(
"{expr: \"{'a': 2, 'b': 2.0}.transformMapEntry(i, v, {v: i})\", err: \"insert failed: key"
+ " '2.0' already exists\"}")
public void twoVarComprehension_keyCollision_runtimeError(String expr, String err)
throws Exception {
// Planner does not allow decimals for map keys
Assume.assumeFalse(runtimeFlavor.equals(CelRuntimeFlavor.PLANNER) && expr.contains("2.0"));
CelEvaluationException e = assertThrows(CelEvaluationException.class, () -> eval(expr));
Throwable cause =
Throwables.getCausalChain(e).stream()
.filter(IllegalArgumentException.class::isInstance)
.filter(t -> t.getMessage() != null && t.getMessage().contains(err))
.findFirst()
.orElse(null);
assertWithMessage(
"Expected IllegalArgumentException with message containing '%s' in cause chain", err)
.that(cause)
.isNotNull();
}
@Test
public void twoVarComprehension_arithmeticException_runtimeError() throws Exception {
CelEvaluationException e =
assertThrows(CelEvaluationException.class, () -> eval("[0].all(i, k, i/k < k)"));
assertThat(e).hasCauseThat().isInstanceOf(CelDivideByZeroException.class);
assertThat(e).hasCauseThat().hasMessageThat().contains("/ by zero");
}
@Test
public void twoVarComprehension_outOfBounds_runtimeError() throws Exception {
CelEvaluationException e =
assertThrows(CelEvaluationException.class, () -> eval("[1, 2].exists(i, v, [0][v] > 0)"));
assertThat(e).hasCauseThat().isInstanceOf(CelIndexOutOfBoundsException.class);
assertThat(e).hasCauseThat().hasMessageThat().contains("Index out of bounds: 1");
}
@Test
public void mutableMapValue_select_missingKeyException() throws Exception {
CelEvaluationException e =
assertThrows(
CelEvaluationException.class, () -> eval("cel.bind(my_map, {'a': 1}, my_map.b)"));
assertThat(e).hasCauseThat().isInstanceOf(CelAttributeNotFoundException.class);
assertThat(e).hasCauseThat().hasMessageThat().contains("key 'b' is not present in map.");
}
private Object eval(String expression) throws Exception {
return eval(this.cel, expression, ImmutableMap.of());
}
private Object eval(Cel cel, String expression, Map<String, ?> variables) throws Exception {
CelAbstractSyntaxTree ast;
if (isParseOnly) {
ast = cel.parse(expression).getAst();
} else {
ast = cel.compile(expression).getAst();
}
return cel.createProgram(ast).eval(variables);
}
}