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

Function TestValidateRegexLiterals

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

Source from the content-addressed store, hash-verified

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

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