Repository navigation
Expand file tree
/
Copy pathCelStandardFunctionsTest.java
More file actions
246 lines (221 loc) · 9.64 KB
/
Copy pathCelStandardFunctionsTest.java
File metadata and controls
246 lines (221 loc) · 9.64 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
// Copyright 2024 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.runtime;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableSet;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameters;
import dev.cel.common.CelOptions;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.runtime.CelStandardFunctions.StandardFunction;
import dev.cel.runtime.standard.AddOperator.AddOverload;
import dev.cel.runtime.standard.CelStandardOverload;
import dev.cel.runtime.standard.SubtractOperator.SubtractOverload;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(TestParameterInjector.class)
public class CelStandardFunctionsTest {
@Test
@TestParameters("{includeFunction: true, excludeFunction: true, filterFunction: true}")
@TestParameters("{includeFunction: true, excludeFunction: true, filterFunction: false}")
@TestParameters("{includeFunction: true, excludeFunction: false, filterFunction: true}")
@TestParameters("{includeFunction: false, excludeFunction: true, filterFunction: true}")
public void standardFunction_moreThanOneFunctionFilterSet_throws(
boolean includeFunction, boolean excludeFunction, boolean filterFunction) {
CelStandardFunctions.Builder builder = CelStandardFunctions.newBuilder();
if (includeFunction) {
builder.includeFunctions(StandardFunction.ADD);
}
if (excludeFunction) {
builder.excludeFunctions(StandardFunction.SUBTRACT);
}
if (filterFunction) {
builder.filterFunctions((func, over) -> true);
}
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, builder::build);
assertThat(e)
.hasMessageThat()
.contains(
"You may only populate one of the following builder methods: includeFunctions,"
+ " excludeFunctions or filterFunctions");
}
@Test
public void runtime_standardEnvironmentEnabled_throwsWhenOverridingFunctions() {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class,
() ->
CelRuntimeFactory.legacyCelRuntimeBuilder()
.setStandardEnvironmentEnabled(true)
.setStandardFunctions(
CelStandardFunctions.newBuilder()
.includeFunctions(StandardFunction.ADD, StandardFunction.SUBTRACT)
.build())
.build());
assertThat(e)
.hasMessageThat()
.contains(
"setStandardEnvironmentEnabled must be set to false to override standard"
+ " function bindings");
}
@Test
public void standardFunctions_includeFunctions() {
CelStandardFunctions celStandardFunctions =
CelStandardFunctions.newBuilder()
.includeFunctions(
CelStandardFunctions.StandardFunction.ADD,
CelStandardFunctions.StandardFunction.SUBTRACT)
.build();
assertThat(celStandardFunctions.getOverloads())
.containsExactlyElementsIn(
ImmutableSet.<CelStandardOverload>builder()
.addAll(CelStandardFunctions.StandardFunction.ADD.getOverloads())
.addAll(CelStandardFunctions.StandardFunction.SUBTRACT.getOverloads())
.build());
}
@Test
public void standardFunctions_excludeFunctions() {
CelStandardFunctions celStandardFunction =
CelStandardFunctions.newBuilder()
.excludeFunctions(
CelStandardFunctions.StandardFunction.ADD,
CelStandardFunctions.StandardFunction.SUBTRACT)
.build();
assertThat(celStandardFunction.getOverloads())
.doesNotContain(CelStandardFunctions.StandardFunction.ADD.getOverloads());
assertThat(celStandardFunction.getOverloads())
.doesNotContain(CelStandardFunctions.StandardFunction.SUBTRACT.getOverloads());
}
@Test
public void standardFunctions_filterFunctions() {
CelStandardFunctions celStandardFunction =
CelStandardFunctions.newBuilder()
.filterFunctions(
(func, over) -> {
if (func.equals(CelStandardFunctions.StandardFunction.ADD)
&& over.equals(AddOverload.ADD_INT64)) {
return true;
}
if (func.equals(CelStandardFunctions.StandardFunction.SUBTRACT)
&& over.equals(SubtractOverload.SUBTRACT_INT64)) {
return true;
}
return false;
})
.build();
assertThat(celStandardFunction.getOverloads())
.containsExactly(AddOverload.ADD_INT64, SubtractOverload.SUBTRACT_INT64);
}
@Test
public void standardFunctions_empty() {
assertThat(CelStandardFunctions.EMPTY.getOverloads()).isEmpty();
}
@Test
public void standardEnvironment_subsetEnvironment() throws Exception {
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build();
CelRuntime celRuntime =
CelRuntimeFactory.plannerRuntimeBuilder()
.setStandardFunctions(
CelStandardFunctions.newBuilder()
.includeFunctions(StandardFunction.ADD, StandardFunction.SUBTRACT)
.build())
.build();
assertThat(celRuntime.createProgram(celCompiler.compile("1 + 2 - 3").getAst()).eval())
.isEqualTo(0);
CelEvaluationException e =
assertThrows(
CelEvaluationException.class,
() -> celRuntime.createProgram(celCompiler.compile("1 * 2 / 3").getAst()).eval());
assertThat(e)
.hasMessageThat()
.contains("No matching overload for function '_*_'. Overload candidates: multiply_int64");
}
@Test
@TestParameters("{expression: '1 > 2.0'}")
@TestParameters("{expression: '2.0 > 1'}")
@TestParameters("{expression: '1 > 2u'}")
@TestParameters("{expression: '2u > 1'}")
@TestParameters("{expression: '2u > 1.0'}")
@TestParameters("{expression: '1.0 > 2u'}")
@TestParameters("{expression: '1 >= 2.0'}")
@TestParameters("{expression: '2.0 >= 1'}")
@TestParameters("{expression: '1 >= 2u'}")
@TestParameters("{expression: '2u >= 1'}")
@TestParameters("{expression: '2u >= 1.0'}")
@TestParameters("{expression: '1.0 >= 2u'}")
@TestParameters("{expression: '1 < 2.0'}")
@TestParameters("{expression: '2.0 < 1'}")
@TestParameters("{expression: '1 < 2u'}")
@TestParameters("{expression: '2u < 1'}")
@TestParameters("{expression: '2u < 1.0'}")
@TestParameters("{expression: '1.0 < 2u'}")
@TestParameters("{expression: '1 <= 2.0'}")
@TestParameters("{expression: '2.0 <= 1'}")
@TestParameters("{expression: '1 <= 2u'}")
@TestParameters("{expression: '2u <= 1'}")
@TestParameters("{expression: '2u <= 1.0'}")
@TestParameters("{expression: '1.0 <= 2u'}")
public void unsignedLongsDisabled_mixedTypeComparisons_throws(String expression)
throws Exception {
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build();
CelRuntime celRuntime =
CelRuntimeFactory.legacyCelRuntimeBuilder()
.setOptions(CelOptions.current().enableUnsignedLongs(false).build())
.build();
CelRuntime.Program program = celRuntime.createProgram(celCompiler.compile(expression).getAst());
CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
assertThat(e).hasMessageThat().contains("No matching overload for function");
}
@Test
public void unsignedLongsDisabled_int64Identity_throws() {
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setOptions(CelOptions.current().enableUnsignedLongs(true).build())
.build();
CelRuntime celRuntime =
CelRuntimeFactory.legacyCelRuntimeBuilder()
.setOptions(CelOptions.current().enableUnsignedLongs(false).build())
.build();
CelEvaluationException e =
assertThrows(
CelEvaluationException.class,
() -> celRuntime.createProgram(celCompiler.compile("int(1)").getAst()).eval());
assertThat(e)
.hasMessageThat()
.contains("No matching overload for function 'int'. Overload candidates: int64_to_int64");
}
@Test
public void timestampEpochDisabled_int64Identity_throws() {
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.setOptions(CelOptions.current().build())
.build();
CelRuntime celRuntime =
CelRuntimeFactory.legacyCelRuntimeBuilder()
.setOptions(CelOptions.current().enableTimestampEpoch(false).build())
.build();
CelEvaluationException e =
assertThrows(
CelEvaluationException.class,
() ->
celRuntime.createProgram(celCompiler.compile("timestamp(10000)").getAst()).eval());
assertThat(e)
.hasMessageThat()
.contains(
"No matching overload for function 'timestamp'. Overload candidates:"
+ " int64_to_timestamp");
}
}