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

Function TestDynamicProto

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

Source from the content-addressed store, hash-verified

707}
708
709func TestDynamicProto(t *testing.T) {
710 b, err := os.ReadFile("testdata/team.fds")
711 if err != nil {
712 t.Fatalf("os.ReadFile() failed: %v", err)
713 }
714 var fds descpb.FileDescriptorSet
715 if err = proto.Unmarshal(b, &fds); err != nil {
716 t.Fatalf("proto.Unmarshal() failed: %v", err)
717 }
718 files := (&fds).GetFile()
719 fileCopy := make([]any, len(files))
720 for i := 0; i < len(files); i++ {
721 fileCopy[i] = files[i]
722 }
723 pbFiles, err := protodesc.NewFiles(&fds)
724 if err != nil {
725 t.Fatalf("protodesc.NewFiles() failed: %v", err)
726 }
727 e := testEnv(t,
728 Container("cel"),
729 // The following is identical to registering the FileDescriptorSet;
730 // however, it tests a different code path which aggregates individual
731 // FileDescriptorProto values together.
732 TypeDescs(fileCopy...),
733 // Additionally, demonstrate that double registration of files doesn't
734 // cause any problems.
735 TypeDescs(pbFiles),
736 )
737 src := `testdata.Team{name: 'X-Men', members: [
738 testdata.Mutant{name: 'Jean Grey', level: 20},
739 testdata.Mutant{name: 'Cyclops', level: 7},
740 testdata.Mutant{name: 'Storm', level: 7},
741 testdata.Mutant{name: 'Wolverine', level: 11}
742 ]}`
743 ast, iss := e.Compile(src)
744 if iss.Err() != nil {
745 t.Fatalf("env.Compile(%s) failed: %v", src, iss.Err())
746 }
747 prg, err := e.Program(ast, EvalOptions(OptOptimize))
748 if err != nil {
749 t.Fatalf("env.Program() failed: %v", err)
750 }
751 out, _, err := prg.Eval(NoVars())
752 if err != nil {
753 t.Fatalf("program.Eval() failed: %v", err)
754 }
755 obj, ok := out.(traits.Indexer)
756 if !ok {
757 t.Fatalf("unable to convert output to object: %v", out)
758 }
759 if obj.Get(types.String("name")).Equal(types.String("X-Men")) == types.False {
760 t.Fatalf("got field 'name' %v, wanted X-Men", obj.Get(types.String("name")))
761 }
762}
763
764func TestDynamicProtoFileDescriptors(t *testing.T) {
765 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