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

Function TestValidateBindNestingLimit

ext/bindings_test.go:964–1026  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

962}
963
964func TestValidateBindNestingLimit(t *testing.T) {
965 env, err := cel.NewEnv(
966 Bindings(),
967 cel.ASTValidators(cel.ValidateBindNestingLimit(2)),
968 )
969 if err != nil {
970 t.Fatalf("cel.NewEnv() failed: %v", err)
971 }
972 tests := []struct {
973 expr string
974 iss string
975 }{
976 {
977 expr: `cel.bind(a, 1, a + 1)`,
978 },
979 {
980 expr: `cel.bind(a, 1, cel.bind(b, 2, a + b))`,
981 },
982 {
983 // two cel.binds, but in separate branches
984 expr: `cel.bind(a, 1, a) + cel.bind(b, 2, b)`,
985 },
986 {
987 // empty iteration range comprehension (e.g. cel.bind) does not count against comprehension limit,
988 // but counts against cel.bind nesting limit.
989 expr: `[1, 2, 3].exists(i, cel.bind(a, i, cel.bind(b, a, a + b) > 0))`,
990 },
991 {
992 // three cel.binds, three levels deep
993 expr: `cel.bind(a, 1, cel.bind(b, 2, cel.bind(c, 3, a + b + c)))`,
994 iss: `
995 ERROR: <input>:1:39: cel.bind exceeds nesting limit
996 | cel.bind(a, 1, cel.bind(b, 2, cel.bind(c, 3, a + b + c)))
997 | ......................................^`,
998 },
999 {
1000 // three cel.binds, three levels deep with non-comprehension ancestor (+)
1001 expr: `cel.bind(a, 1, cel.bind(b, 2, 1 + cel.bind(c, 3, a + b + c)))`,
1002 iss: `
1003 ERROR: <input>:1:43: cel.bind exceeds nesting limit
1004 | cel.bind(a, 1, cel.bind(b, 2, 1 + cel.bind(c, 3, a + b + c)))
1005 | ..........................................^`,
1006 },
1007 }
1008 for _, tst := range tests {
1009 tc := tst
1010 t.Run(tc.expr, func(t *testing.T) {
1011 _, iss := env.Compile(tc.expr)
1012 if tc.iss != "" {
1013 if iss.Err() == nil {
1014 t.Fatalf("env.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss)
1015 }
1016 if !test.Compare(iss.Err().Error(), tc.iss) {
1017 t.Fatalf("env.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss)
1018 }
1019 return
1020 }
1021 if iss.Err() != nil {

Callers

nothing calls this directly

Calls 8

CompileMethod · 0.95
NewEnvFunction · 0.92
ASTValidatorsFunction · 0.92
ValidateBindNestingLimitFunction · 0.92
CompareFunction · 0.92
BindingsFunction · 0.85
ErrMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected