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

Function TestLateBoundFunctionComprehensionActivations

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

Source from the content-addressed store, hash-verified

6567}
6568
6569func TestLateBoundFunctionComprehensionActivations(t *testing.T) {
6570 env := lateBoundComprehensionEnv(t)
6571 incFuncs := map[string]LateBoundFunction{"inc": lateBoundInc}
6572
6573 tests := []struct {
6574 name string
6575 // vars builds the activation supplied to Eval.
6576 vars func(t *testing.T) any
6577 // opts are additional program options, e.g. default variables or partial evaluation.
6578 opts []ProgramOption
6579 want ref.Val
6580 }{
6581 {
6582 name: "function bindings wrapped in a partial activation",
6583 vars: func(t *testing.T) any {
6584 t.Helper()
6585 fnVars, err := FunctionVars(map[string]any{"x": 10}, incFuncs)
6586 if err != nil {
6587 t.Fatalf("FunctionVars() failed: %v", err)
6588 }
6589 partial, err := PartialVars(fnVars, AttributePattern("unused"))
6590 if err != nil {
6591 t.Fatalf("PartialVars() failed: %v", err)
6592 }
6593 return partial
6594 },
6595 opts: []ProgramOption{EvalOptions(OptPartialEval)},
6596 want: types.DefaultTypeAdapter.NativeToValue([]int64{12, 13}),
6597 },
6598 {
6599 name: "function bindings layered over a partial activation",
6600 vars: func(t *testing.T) any {
6601 t.Helper()
6602 partial, err := PartialVars(map[string]any{"x": 10}, AttributePattern("unused"))
6603 if err != nil {
6604 t.Fatalf("PartialVars() failed: %v", err)
6605 }
6606 fnVars, err := FunctionVars(partial, incFuncs)
6607 if err != nil {
6608 t.Fatalf("FunctionVars() failed: %v", err)
6609 }
6610 return fnVars
6611 },
6612 opts: []ProgramOption{EvalOptions(OptPartialEval)},
6613 want: types.DefaultTypeAdapter.NativeToValue([]int64{12, 13}),
6614 },
6615 {
6616 name: "function bindings supplied as program default variables",
6617 vars: func(t *testing.T) any {
6618 t.Helper()
6619 // The default variables become the parent of the evaluation-time activation, so
6620 // the comprehension scope must resolve the function through both layers.
6621 return map[string]any{"x": 10}
6622 },
6623 opts: func() []ProgramOption {
6624 defaults, err := FunctionVars(map[string]any{}, incFuncs)
6625 if err != nil {
6626 t.Fatalf("FunctionVars() failed: %v", err)

Callers

nothing calls this directly

Calls 12

FunctionVarsFunction · 0.85
PartialVarsFunction · 0.85
EvalOptionsFunction · 0.85
GlobalsFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
AttributePatternFunction · 0.70
NativeToValueMethod · 0.65
CompileMethod · 0.65
EvalMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected