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

Function TestValidateHomogeneousAggregateLiterals

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

Source from the content-addressed store, hash-verified

277}
278
279func TestValidateHomogeneousAggregateLiterals(t *testing.T) {
280 env, err := NewCustomEnv(
281 Variable("name", StringType),
282 Function(operators.In,
283 Overload(overloads.InList, []*Type{StringType, ListType(StringType)}, BoolType,
284 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
285 return rhs.(traits.Container).Contains(lhs)
286 }),
287 ),
288 Overload(overloads.InMap, []*Type{StringType, MapType(StringType, BoolType)}, BoolType,
289 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
290 return rhs.(traits.Container).Contains(lhs)
291 }),
292 ),
293 ),
294 OptionalTypes(),
295 HomogeneousAggregateLiterals(),
296 ASTValidators(ValidateHomogeneousAggregateLiterals()),
297 )
298 if err != nil {
299 t.Fatalf("NewCustomEnv() failed: %v", err)
300 }
301
302 tests := []struct {
303 expr string
304 iss string
305 }{
306 {
307 expr: `name in ['hello', 0]`,
308 iss: `
309 ERROR: <input>:1:19: expected type 'string' but found 'int'
310 | name in ['hello', 0]
311 | ..................^`,
312 },
313 {
314 expr: `{'hello':'world', 1:'!'}`,
315 iss: `
316 ERROR: <input>:1:19: expected type 'string' but found 'int'
317 | {'hello':'world', 1:'!'}
318 | ..................^`,
319 },
320 {
321 expr: `name in {'hello':'world', 'goodbye':true}`,
322 iss: `
323 ERROR: <input>:1:37: expected type 'string' but found 'bool'
324 | name in {'hello':'world', 'goodbye':true}
325 | ....................................^`,
326 },
327 {
328 expr: `name in ['hello', 'world']`,
329 },
330 {
331 expr: `name in ['hello', ?optional.ofNonZeroValue('')]`,
332 },
333 {
334 expr: `name in [?optional.ofNonZeroValue(''), 'hello', ?optional.of('')]`,
335 },
336 {

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