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

Function TestValidateHomogeneousAggregateLiterals

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

Source from the content-addressed store, hash-verified

204}
205
206func TestValidateHomogeneousAggregateLiterals(t *testing.T) {
207 env, err := NewCustomEnv(
208 Variable("name", StringType),
209 Function(operators.In,
210 Overload(overloads.InList, []*Type{StringType, ListType(StringType)}, BoolType,
211 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
212 return rhs.(traits.Container).Contains(lhs)
213 }),
214 ),
215 Overload(overloads.InMap, []*Type{StringType, MapType(StringType, BoolType)}, BoolType,
216 BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
217 return rhs.(traits.Container).Contains(lhs)
218 }),
219 ),
220 ),
221 OptionalTypes(),
222 HomogeneousAggregateLiterals(),
223 ASTValidators(ValidateHomogeneousAggregateLiterals()),
224 )
225 if err != nil {
226 t.Fatalf("NewCustomEnv() failed: %v", err)
227 }
228
229 tests := []struct {
230 expr string
231 iss string
232 }{
233 {
234 expr: `name in ['hello', 0]`,
235 iss: `
236 ERROR: <input>:1:19: expected type 'string' but found 'int'
237 | name in ['hello', 0]
238 | ..................^`,
239 },
240 {
241 expr: `{'hello':'world', 1:'!'}`,
242 iss: `
243 ERROR: <input>:1:19: expected type 'string' but found 'int'
244 | {'hello':'world', 1:'!'}
245 | ..................^`,
246 },
247 {
248 expr: `name in {'hello':'world', 'goodbye':true}`,
249 iss: `
250 ERROR: <input>:1:37: expected type 'string' but found 'bool'
251 | name in {'hello':'world', 'goodbye':true}
252 | ....................................^`,
253 },
254 {
255 expr: `name in ['hello', 'world']`,
256 },
257 {
258 expr: `name in ['hello', ?optional.ofNonZeroValue('')]`,
259 },
260 {
261 expr: `name in [?optional.ofNonZeroValue(''), 'hello', ?optional.of('')]`,
262 },
263 {

Callers

nothing calls this directly

Calls 14

CompareFunction · 0.92
NewCustomEnvFunction · 0.85
OptionalTypesFunction · 0.85
ASTValidatorsFunction · 0.85
ErrMethod · 0.80
VariableFunction · 0.70
FunctionFunction · 0.70
OverloadFunction · 0.70
BinaryBindingFunction · 0.70
ContainsMethod · 0.65

Tested by

no test coverage detected