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

Function TestValidateRegexLiterals

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

Source from the content-addressed store, hash-verified

146}
147
148func TestValidateRegexLiterals(t *testing.T) {
149 env, err := NewEnv(
150 Variable("x", types.StringType),
151 ASTValidators(ValidateRegexLiterals()))
152 if err != nil {
153 t.Fatalf("NewEnv(ValidateRegexLiterals()) failed: %v", err)
154 }
155
156 tests := []struct {
157 expr string
158 iss string
159 }{
160 {
161 expr: `'hello'.matches('el*')`,
162 },
163 {
164 expr: `'hello'.matches('x++')`,
165 iss: `
166 ERROR: <input>:1:17: invalid matches argument
167 | 'hello'.matches('x++')
168 | ................^`,
169 },
170 {
171 expr: `'hello'.matches('(?<name%>el*)')`,
172 iss: `
173 ERROR: <input>:1:17: invalid matches argument
174 | 'hello'.matches('(?<name%>el*)')
175 | ................^`,
176 },
177 {
178 expr: `'hello'.matches('??el*')`,
179 iss: `
180 ERROR: <input>:1:17: invalid matches argument
181 | 'hello'.matches('??el*')
182 | ................^`,
183 },
184 {
185 expr: `'hello'.matches(x)`,
186 },
187 }
188 for _, tst := range tests {
189 tc := tst
190 t.Run(tc.expr, func(t *testing.T) {
191 _, iss := env.Compile(tc.expr)
192 if tc.iss != "" {
193 if iss.Err() == nil {
194 t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss)
195 }
196 if !test.Compare(iss.Err().Error(), tc.iss) {
197 t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss)
198 }
199 return
200 }
201 if iss.Err() != nil {
202 t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err())
203 }
204 })
205 }

Callers

nothing calls this directly

Calls 8

CompileMethod · 0.95
CompareFunction · 0.92
ASTValidatorsFunction · 0.85
ValidateRegexLiteralsFunction · 0.85
ErrMethod · 0.80
NewEnvFunction · 0.70
VariableFunction · 0.70
ErrorMethod · 0.45

Tested by

no test coverage detected