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

Function TestRegexRuntimeErrors

ext/regex_test.go:169–231  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

167}
168
169func TestRegexRuntimeErrors(t *testing.T) {
170 tests := []struct {
171 expr string
172 err string
173 }{
174 {
175 expr: "regex.extract('foo bar', '(')",
176 err: "error parsing regexp: missing closing ): `(`",
177 },
178 {
179 expr: "regex.extractAll('foo bar', '[a-z')",
180 err: "error parsing regexp: missing closing ]: `[a-z`",
181 },
182 {
183 expr: `regex.replace('id=123', r'id=(?P<value>\d+)', r'value: \values')`,
184 err: `invalid replacement string: 'value: \values' \ must be followed by a digit`,
185 },
186 {
187 expr: `regex.replace('test', '(.)', r'\2')`,
188 err: "replacement string references group 2 but regex has only 1 group(s)",
189 },
190 {
191 expr: `regex.replace('id=123', r'id=(?P<value>\d+)', r'value: \')`,
192 err: `invalid replacement string: 'value: \' \ not allowed at end`,
193 },
194 {
195 expr: `regex.extract('phone: 415-5551212', r'phone: ((\d{3})-)?')`,
196 err: `regular expression has more than one capturing group: "phone: ((\\d{3})-)?"`,
197 },
198 {
199 expr: `regex.extractAll('Name: John Doe, Age:321', r'Name: (?P<Name>.*), Age:(?P<Age>\d+)')`,
200 err: `regular expression has more than one capturing group: "Name: (?P<Name>.*), Age:(?P<Age>\\d+)"`,
201 },
202 {
203 expr: `regex.extractAll('testuser@testdomain', '(.*)@([^.]*)')`,
204 err: `regular expression has more than one capturing group: "(.*)@([^.]*)"`,
205 },
206 {
207 expr: `regex.extractAll('The user testuser belongs to testdomain', 'The (user|domain) (?P<Username>.*) belongs (to) (?P<Domain>.*)')`,
208 err: `regular expression has more than one capturing group: "The (user|domain) (?P<Username>.*) belongs (to) (?P<Domain>.*)"`,
209 },
210 }
211
212 env := testRegexEnv(t)
213 for i, tst := range tests {
214 tr := tst
215 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
216 ast, iss := env.Compile(tr.expr)
217 if iss.Err() != nil {
218 t.Fatalf("env.Compile(%q) failed with error %v", tr.expr, iss.Err())
219 }
220 prg, err := env.Program(ast)
221 if err != nil {
222 t.Fatalf("env.Program(ast) failed: %v", err)
223 }
224 in := cel.NoVars()
225 _, _, err = prg.Eval(in)
226 if err == nil || !strings.Contains(err.Error(), tr.err) {

Callers

nothing calls this directly

Calls 8

NoVarsFunction · 0.92
testRegexEnvFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
CompileMethod · 0.65
EvalMethod · 0.65
ContainsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected