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

Method createTestInputFromPB

tools/celtest/test_runner.go:659–718  ·  view source on GitHub ↗
(t *testing.T, testCase *conformancepb.TestCase)

Source from the content-addressed store, hash-verified

657}
658
659func (tr *TestRunner) createTestInputFromPB(t *testing.T, testCase *conformancepb.TestCase) (cel.PartialActivation, error) {
660 t.Helper()
661 input := map[string]any{}
662 e, err := tr.CreateEnv()
663 if err != nil {
664 return nil, err
665 }
666 var activation cel.Activation
667 if testCase.GetInputContext() != nil {
668 if len(testCase.GetInput()) != 0 {
669 return nil, fmt.Errorf("only one of input and input_context can be provided at a time")
670 }
671 switch testInput := testCase.GetInputContext().GetInputContextKind().(type) {
672 case *conformancepb.InputContext_ContextExpr:
673 refVal, err := tr.eval(testInput.ContextExpr)
674 if err != nil {
675 return nil, fmt.Errorf("eval(%q) failed: %w", testInput.ContextExpr, err)
676 }
677 ctx, err := refVal.ConvertToNative(
678 reflect.TypeOf((*proto.Message)(nil)).Elem())
679 if err != nil {
680 return nil, fmt.Errorf("context variable is not a valid proto: %w", err)
681 }
682 activation, err = cel.ContextProtoVars(ctx.(proto.Message))
683 if err != nil {
684 return nil, fmt.Errorf("cel.ContextProtoVars() failed: %w", err)
685 }
686 case *conformancepb.InputContext_ContextMessage:
687 refVal := e.CELTypeAdapter().NativeToValue(testInput.ContextMessage)
688 ctx, err := refVal.ConvertToNative(reflect.TypeOf((*proto.Message)(nil)).Elem())
689 if err != nil {
690 return nil, fmt.Errorf("context variable is not a valid proto: %w", err)
691 }
692 activation, err = cel.ContextProtoVars(ctx.(proto.Message))
693 if err != nil {
694 return nil, fmt.Errorf("cel.ContextProtoVars() failed: %w", err)
695 }
696 }
697 return e.PartialVars(activation)
698 }
699 for k, v := range testCase.GetInput() {
700 switch v.GetKind().(type) {
701 case *conformancepb.InputValue_Value:
702 input[k], err = cel.ProtoAsValue(e.CELTypeAdapter(), v.GetValue())
703 if err != nil {
704 return nil, fmt.Errorf("cel.ProtoAsValue(%q) failed: %w", v, err)
705 }
706 case *conformancepb.InputValue_Expr:
707 input[k], err = tr.eval(v.GetExpr())
708 if err != nil {
709 return nil, fmt.Errorf("eval(%q) failed: %w", v.GetExpr(), err)
710 }
711 }
712 }
713 activation, err = tr.activationFactory(input)
714 if err != nil {
715 return nil, fmt.Errorf("activationFactory(%q) failed: %w", input, err)
716 }

Callers 1

Calls 9

evalMethod · 0.95
ContextProtoVarsFunction · 0.92
ProtoAsValueFunction · 0.92
CELTypeAdapterMethod · 0.80
PartialVarsMethod · 0.80
GetValueMethod · 0.80
CreateEnvMethod · 0.65
ConvertToNativeMethod · 0.65
NativeToValueMethod · 0.65

Tested by

no test coverage detected