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

Function TestDynamicProtoFileDescriptors

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

Source from the content-addressed store, hash-verified

657}
658
659func TestDynamicProtoFileDescriptors(t *testing.T) {
660 b, err := os.ReadFile("testdata/team.fds")
661 if err != nil {
662 t.Fatalf("os.ReadFile() failed: %v", err)
663 }
664 var fds descpb.FileDescriptorSet
665 if err = proto.Unmarshal(b, &fds); err != nil {
666 t.Fatalf("proto.Unmarshal() failed: %v", err)
667 }
668 files := (&fds).GetFile()
669 fileCopy := make([]any, len(files))
670 for i := 0; i < len(files); i++ {
671 fileCopy[i] = files[i]
672 }
673 pbFiles, err := protodesc.NewFiles(&fds)
674 if err != nil {
675 t.Fatalf("protodesc.NewFiles() failed: %v", err)
676 }
677 desc, err := pbFiles.FindDescriptorByName("cel.testdata.Mutant")
678 if err != nil {
679 t.Fatalf("pbFiles.FindDescriptorByName() could not find Mutant: %v", err)
680 }
681 msgDesc, ok := desc.(protoreflect.MessageDescriptor)
682 if !ok {
683 t.Fatalf("desc not convertible to MessageDescriptor: %T", desc)
684 }
685 wolverine := dynamicpb.NewMessage(msgDesc)
686 wolverine.ProtoReflect().Set(msgDesc.Fields().ByName("name"), protoreflect.ValueOfString("Wolverine"))
687 env := testEnv(t,
688 // The following is identical to registering the FileDescriptorSet;
689 // however, it tests a different code path which aggregates individual
690 // FileDescriptorProto values together.
691 TypeDescs(fileCopy...),
692 Variable("mutant", ObjectType("cel.testdata.Mutant")),
693 )
694 src := `has(mutant.name) && mutant.name == 'Wolverine'`
695 ast, iss := env.Compile(src)
696 if iss.Err() != nil {
697 t.Fatalf("env.Compile(%s) failed: %v", src, iss.Err())
698 }
699 prg, err := env.Program(ast, EvalOptions(OptOptimize))
700 if err != nil {
701 t.Fatalf("env.Program() failed: %v", err)
702 }
703 out, _, err := prg.Eval(map[string]any{
704 "mutant": wolverine,
705 })
706 if err != nil {
707 t.Fatalf("program.Eval() failed: %v", err)
708 }
709 obj, ok := out.(types.Bool)
710 if !ok {
711 t.Fatalf("unable to convert output to object: %v", out)
712 }
713 if obj != types.True {
714 t.Errorf("got %v, wanted true", out)
715 }
716}

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