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

Function TestConfigAddFunctionDecls

common/env/env_test.go:518–582  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

516}
517
518func TestConfigAddFunctionDecls(t *testing.T) {
519 tests := []struct {
520 name string
521 in *decls.FunctionDecl
522 out *Function
523 }{
524 {
525 name: "nil function decl",
526 },
527 {
528 name: "global function decl",
529 in: mustNewFunction(t, "size",
530 decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType),
531 ),
532 out: NewFunction("size",
533 NewOverload("size_string", []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("int")),
534 ),
535 },
536 {
537 name: "global function decl - nullable arg",
538 in: mustNewFunction(t, "size",
539 decls.Overload("size_wrapper_string", []*types.Type{types.NewNullableType(types.StringType)}, types.IntType),
540 ),
541 out: NewFunction("size",
542 NewOverload("size_wrapper_string", []*TypeDesc{NewTypeDesc("google.protobuf.StringValue")}, NewTypeDesc("int")),
543 ),
544 },
545 {
546 name: "member function decl - nullable arg",
547 in: mustNewFunction(t, "size",
548 decls.MemberOverload("list_size", []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType),
549 decls.MemberOverload("string_size", []*types.Type{types.StringType}, types.IntType),
550 ),
551 out: NewFunction("size",
552 NewMemberOverload("list_size", NewTypeDesc("list", NewTypeParam("T")), []*TypeDesc{}, NewTypeDesc("int")),
553 NewMemberOverload("string_size", NewTypeDesc("string"), []*TypeDesc{}, NewTypeDesc("int")),
554 ),
555 },
556 {
557 name: "global function decl - with doc",
558 in: mustNewFunction(t, "size",
559 decls.FunctionDocs("return the number of unicode code points", "in a string"),
560 decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType,
561 decls.OverloadExamples(`'hello'.size() // 5`)),
562 ),
563 out: NewFunctionWithDoc("size",
564 "return the number of unicode code points\nin a string",
565 NewOverload("size_string", []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("int"),
566 `'hello'.size() // 5`),
567 ),
568 },
569 }
570 for _, tst := range tests {
571 tc := tst
572 t.Run(tc.name, func(t *testing.T) {
573 conf := NewConfig(tc.name).AddFunctionDecls(tc.in)
574 if len(conf.Functions) != 1 {
575 t.Fatalf("AddFunctionDecls() did not add declaration to conf: %v", conf)

Callers

nothing calls this directly

Calls 15

OverloadFunction · 0.92
NewNullableTypeFunction · 0.92
MemberOverloadFunction · 0.92
NewListTypeFunction · 0.92
NewTypeParamTypeFunction · 0.92
FunctionDocsFunction · 0.92
OverloadExamplesFunction · 0.92
mustNewFunctionFunction · 0.85
NewTypeDescFunction · 0.85
NewMemberOverloadFunction · 0.85
NewTypeParamFunction · 0.85
NewConfigFunction · 0.85

Tested by

no test coverage detected