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

Function TestDynamicProtoFileDescriptors

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

Source from the content-addressed store, hash-verified

761}
762
763func TestDynamicProtoFileDescriptors(t *testing.T) {
764 b, err := os.ReadFile("testdata/team.fds")
765 if err != nil {
766 t.Fatalf("os.ReadFile() failed: %v", err)
767 }
768 var fds descpb.FileDescriptorSet
769 if err = proto.Unmarshal(b, &fds); err != nil {
770 t.Fatalf("proto.Unmarshal() failed: %v", err)
771 }
772 files := (&fds).GetFile()
773 fileCopy := make([]any, len(files))
774 for i := 0; i < len(files); i++ {
775 fileCopy[i] = files[i]
776 }
777 pbFiles, err := protodesc.NewFiles(&fds)
778 if err != nil {
779 t.Fatalf("protodesc.NewFiles() failed: %v", err)
780 }
781 desc, err := pbFiles.FindDescriptorByName("cel.testdata.Mutant")
782 if err != nil {
783 t.Fatalf("pbFiles.FindDescriptorByName() could not find Mutant: %v", err)
784 }
785 msgDesc, ok := desc.(protoreflect.MessageDescriptor)
786 if !ok {
787 t.Fatalf("desc not convertible to MessageDescriptor: %T", desc)
788 }
789 wolverine := dynamicpb.NewMessage(msgDesc)
790 wolverine.ProtoReflect().Set(msgDesc.Fields().ByName("name"), protoreflect.ValueOfString("Wolverine"))
791 env := testEnv(t,
792 // The following is identical to registering the FileDescriptorSet;
793 // however, it tests a different code path which aggregates individual
794 // FileDescriptorProto values together.
795 TypeDescs(fileCopy...),
796 Variable("mutant", ObjectType("cel.testdata.Mutant")),
797 )
798 src := `has(mutant.name) && mutant.name == 'Wolverine'`
799 ast, iss := env.Compile(src)
800 if iss.Err() != nil {
801 t.Fatalf("env.Compile(%s) failed: %v", src, iss.Err())
802 }
803 prg, err := env.Program(ast, EvalOptions(OptOptimize))
804 if err != nil {
805 t.Fatalf("env.Program() failed: %v", err)
806 }
807 out, _, err := prg.Eval(map[string]any{
808 "mutant": wolverine,
809 })
810 if err != nil {
811 t.Fatalf("program.Eval() failed: %v", err)
812 }
813 obj, ok := out.(types.Bool)
814 if !ok {
815 t.Fatalf("unable to convert output to object: %v", out)
816 }
817 if obj != types.True {
818 t.Errorf("got %v, wanted true", out)
819 }
820}

Callers

nothing calls this directly

Calls 11

TypeDescsFunction · 0.85
EvalOptionsFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
VariableFunction · 0.70
SetMethod · 0.65
FieldsMethod · 0.65
CompileMethod · 0.65
EvalMethod · 0.65
ProtoReflectMethod · 0.45

Tested by

no test coverage detected