MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestValidateHomogeneousAggregateLiterals

Function TestValidateHomogeneousAggregateLiterals

cel/validator_test.go:207–292  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

205}
206
207func TestValidateHomogeneousAggregateLiterals(t *testing.T) {
208 env, err := NewCustomEnv(
209 Variable("name", StringType),
210 Function(operators.In,
211 Overload(overloads.InList, []*Type{StringType, ListType(StringType)}, BoolType,
212 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
213 return rhs.(traits.Container).Contains(lhs)
214 }),
215 ),
216 Overload(overloads.InMap, []*Type{StringType, MapType(StringType, BoolType)}, BoolType,
217 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
218 return rhs.(traits.Container).Contains(lhs)
219 }),
220 ),
221 ),
222 OptionalTypes(),
223 HomogeneousAggregateLiterals(),
224 ASTValidators(ValidateHomogeneousAggregateLiterals()),
225 )
226 if err != nil {
227 t.Fatalf("NewCustomEnv() failed: %v", err)
228 }
229
230 tests := []struct {
231 expr string
232 iss string
233 }{
234 {
235 expr: `name in ['hello', 0]`,
236 iss: `
237 ERROR: <input>:1:19: expected type 'string' but found 'int'
238 | name in ['hello', 0]
239 | ..................^`,
240 },
241 {
242 expr: `{'hello':'world', 1:'!'}`,
243 iss: `
244 ERROR: <input>:1:19: expected type 'string' but found 'int'
245 | {'hello':'world', 1:'!'}
246 | ..................^`,
247 },
248 {
249 expr: `name in {'hello':'world', 'goodbye':true}`,
250 iss: `
251 ERROR: <input>:1:37: expected type 'string' but found 'bool'
252 | name in {'hello':'world', 'goodbye':true}
253 | ....................................^`,
254 },
255 {
256 expr: `name in ['hello', 'world']`,
257 },
258 {
259 expr: `name in ['hello', ?optional.ofNonZeroValue('')]`,
260 },
261 {
262 expr: `name in [?optional.ofNonZeroValue(''), 'hello', ?optional.of('')]`,
263 },
264 {

Callers

nothing calls this directly

Calls 14

CompareFunction · 0.92
NewCustomEnvFunction · 0.85
OptionalTypesFunction · 0.85
ASTValidatorsFunction · 0.85
ErrMethod · 0.80
VariableFunction · 0.70
FunctionFunction · 0.70
OverloadFunction · 0.70
BinaryBindingFunction · 0.70
ContainsMethod · 0.65

Tested by

no test coverage detected