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

Function TestDynamicProto

cel/cel_test.go:624–677  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

622}
623
624func TestDynamicProto(t *testing.T) {
625 b, err := os.ReadFile("testdata/team.fds")
626 if err != nil {
627 t.Fatalf("os.ReadFile() failed: %v", err)
628 }
629 var fds descpb.FileDescriptorSet
630 if err = proto.Unmarshal(b, &fds); err != nil {
631 t.Fatalf("proto.Unmarshal() failed: %v", err)
632 }
633 files := (&fds).GetFile()
634 fileCopy := make([]any, len(files))
635 for i := 0; i < len(files); i++ {
636 fileCopy[i] = files[i]
637 }
638 pbFiles, err := protodesc.NewFiles(&fds)
639 if err != nil {
640 t.Fatalf("protodesc.NewFiles() failed: %v", err)
641 }
642 e := testEnv(t,
643 Container("cel"),
644 // The following is identical to registering the FileDescriptorSet;
645 // however, it tests a different code path which aggregates individual
646 // FileDescriptorProto values together.
647 TypeDescs(fileCopy...),
648 // Additionally, demonstrate that double registration of files doesn't
649 // cause any problems.
650 TypeDescs(pbFiles),
651 )
652 src := `testdata.Team{name: 'X-Men', members: [
653 testdata.Mutant{name: 'Jean Grey', level: 20},
654 testdata.Mutant{name: 'Cyclops', level: 7},
655 testdata.Mutant{name: 'Storm', level: 7},
656 testdata.Mutant{name: 'Wolverine', level: 11}
657 ]}`
658 ast, iss := e.Compile(src)
659 if iss.Err() != nil {
660 t.Fatalf("env.Compile(%s) failed: %v", src, iss.Err())
661 }
662 prg, err := e.Program(ast, EvalOptions(OptOptimize))
663 if err != nil {
664 t.Fatalf("env.Program() failed: %v", err)
665 }
666 out, _, err := prg.Eval(NoVars())
667 if err != nil {
668 t.Fatalf("program.Eval() failed: %v", err)
669 }
670 obj, ok := out.(traits.Indexer)
671 if !ok {
672 t.Fatalf("unable to convert output to object: %v", out)
673 }
674 if obj.Get(types.String("name")).Equal(types.String("X-Men")) == types.False {
675 t.Fatalf("got field 'name' %v, wanted X-Men", obj.Get(types.String("name")))
676 }
677}
678
679func TestDynamicProtoFileDescriptors(t *testing.T) {
680 b, err := os.ReadFile("testdata/team.fds")

Callers

nothing calls this directly

Calls 12

StringTypeAlias · 0.92
TypeDescsFunction · 0.85
EvalOptionsFunction · 0.85
NoVarsFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
ContainerFunction · 0.70
CompileMethod · 0.65
EvalMethod · 0.65
EqualMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected