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

Function TestDynamicProto

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

Source from the content-addressed store, hash-verified

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