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

Function TestValidateRegexLiterals

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

Source from the content-addressed store, hash-verified

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

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