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

Function TestDynamicProtoFileDescriptors

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

Source from the content-addressed store, hash-verified

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

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