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

Function TestDynamicProtoFileDescriptors

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

Source from the content-addressed store, hash-verified

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

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