(t *testing.T)
| 677 | } |
| 678 | |
| 679 | func 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 | } |
nothing calls this directly
no test coverage detected