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

Method createTestInput

tools/celtest/test_runner.go:900–943  ·  view source on GitHub ↗
(t *testing.T, testCase *test.Case)

Source from the content-addressed store, hash-verified

898}
899
900func (tr *TestRunner) createTestInput(t *testing.T, testCase *test.Case) (cel.PartialActivation, error) {
901 t.Helper()
902 e, err := tr.CreateEnv()
903 if err != nil {
904 return nil, err
905 }
906 var activation cel.Activation
907 if testCase.InputContext != nil && testCase.InputContext.ContextExpr != "" {
908 if len(testCase.Input) != 0 {
909 return nil, fmt.Errorf("only one of input and input_context can be provided at a time")
910 }
911 contextExpr := testCase.InputContext.ContextExpr
912 out, err := tr.eval(contextExpr)
913 if err != nil {
914 return nil, fmt.Errorf("eval(%q) failed: %w", contextExpr, err)
915 }
916 ctx, err := out.ConvertToNative(reflect.TypeOf((*proto.Message)(nil)).Elem())
917 if err != nil {
918 return nil, fmt.Errorf("context variable is not a valid proto: %w", err)
919 }
920 activation, err = cel.ContextProtoVars(ctx.(proto.Message))
921 if err != nil {
922 return nil, fmt.Errorf("cel.ContextProtoVars() failed: %w", err)
923 }
924 return e.PartialVars(activation)
925 }
926 input := map[string]any{}
927 for k, v := range testCase.Input {
928 if v.Expr != "" {
929 val, err := tr.eval(v.Expr)
930 if err != nil {
931 return nil, fmt.Errorf("eval(%q) failed: %w", v.Expr, err)
932 }
933 input[k] = val
934 continue
935 }
936 input[k] = v.Value
937 }
938 activation, err = tr.activationFactory(input)
939 if err != nil {
940 return nil, fmt.Errorf("activationFactory(%q) failed: %w", input, err)
941 }
942 return e.PartialVars(activation)
943}
944
945func (tr *TestRunner) createResultMatcher(t *testing.T, testOutput *test.Output) (func(ref.Val, error) TestResult, error) {
946 t.Helper()

Callers 1

createTestsFromYAMLMethod · 0.95

Calls 5

evalMethod · 0.95
ContextProtoVarsFunction · 0.92
PartialVarsMethod · 0.80
CreateEnvMethod · 0.65
ConvertToNativeMethod · 0.65

Tested by

no test coverage detected