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

Method run

policy/compiler_test.go:355–418  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

353}
354
355func (r *runner) run(t *testing.T) {
356 tests := readTestSuite(t, fmt.Sprintf("testdata/%s/tests.yaml", r.name))
357 for _, s := range tests.Sections {
358 section := s.Name
359 for _, tst := range s.Tests {
360 tc := tst
361 t.Run(fmt.Sprintf("%s/%s/%s", r.name, section, tc.Name), func(t *testing.T) {
362 input := map[string]any{}
363 var err error
364 var activation interpreter.Activation
365 if tc.InputContext != nil && tc.InputContext.ContextExpr != "" {
366 ctxExpr := tc.InputContext.ContextExpr
367 ctx, err := r.eval(t, ctxExpr).ConvertToNative(
368 reflect.TypeOf(((*proto.Message)(nil))).Elem())
369 if err != nil {
370 t.Fatalf("context variable is not a valid proto: %v", err)
371 }
372 activation, err = cel.ContextProtoVars(ctx.(proto.Message))
373 if err != nil {
374 t.Fatalf("cel.ContextProtoVars() failed: %v", err)
375 }
376 } else if len(tc.Input) != 0 {
377 for k, v := range tc.Input {
378 if v.Expr != "" {
379 input[k] = r.eval(t, v.Expr)
380 continue
381 }
382 input[k] = v.Value
383 }
384 }
385 if activation == nil {
386 activation, err = interpreter.NewActivation(input)
387 if err != nil {
388 t.Fatalf("interpreter.NewActivation(input) failed: %v", err)
389 }
390 }
391 out, _, err := r.prg.Eval(activation)
392 if err != nil {
393 t.Fatalf("prg.Eval(input) failed: %v", err)
394 }
395 var testOut ref.Val
396 if tc.Output.Expr != "" {
397 testOut = r.eval(t, tc.Output.Expr)
398 } else if tc.Output.Value != nil {
399 testOut = r.env.CELTypeAdapter().NativeToValue(tc.Output.Value)
400 }
401 if testOut.Equal(out) == types.True {
402 return
403 }
404 if optOut, ok := out.(*types.Optional); ok {
405 if optOut.Equal(types.OptionalNone) == types.True {
406 if testOut.Equal(types.OptionalNone) != types.True {
407 t.Errorf("policy eval got %v, wanted %v", out, testOut)
408 }
409 } else if testOut.Equal(optOut.GetValue()) != types.True {
410 t.Errorf("policy eval got %v, wanted %v", out, testOut)
411 }
412 } else {

Callers 2

TestCompileFunction · 0.80
TestRuleComposerUnnestFunction · 0.80

Calls 10

evalMethod · 0.95
EqualMethod · 0.95
ContextProtoVarsFunction · 0.92
NewActivationFunction · 0.92
readTestSuiteFunction · 0.85
CELTypeAdapterMethod · 0.80
GetValueMethod · 0.80
ConvertToNativeMethod · 0.65
EvalMethod · 0.65
NativeToValueMethod · 0.65

Tested by

no test coverage detected