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

Function TestValidateHomogeneousAggregateLiterals

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

Source from the content-addressed store, hash-verified

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

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