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

Function TestLateBoundFunctionSingleActivation

cel/cel_test.go:6731–6798  ·  view source on GitHub ↗

TestLateBoundFunctionSingleActivation covers the requirement that only one activation in a hierarchy supplies late-bound function implementations. Layering them would make the bindings which apply depend on the order of composition, which is reported instead.

(t *testing.T)

Source from the content-addressed store, hash-verified

6729// hierarchy supplies late-bound function implementations. Layering them would make the bindings
6730// which apply depend on the order of composition, which is reported instead.
6731func TestLateBoundFunctionSingleActivation(t *testing.T) {
6732 env := lateBoundComprehensionEnv(t)
6733 fnA := map[string]LateBoundFunction{"inc": lateBoundInc}
6734 fnB := map[string]LateBoundFunction{"inc": lateBoundInc}
6735 ast, iss := env.Compile("inc(1)")
6736 if iss.Err() != nil {
6737 t.Fatalf("Compile() failed: %v", iss.Err())
6738 }
6739
6740 t.Run("FunctionVars over FunctionVars", func(t *testing.T) {
6741 inner, err := FunctionVars(map[string]any{}, fnA)
6742 if err != nil {
6743 t.Fatalf("FunctionVars() failed: %v", err)
6744 }
6745 if _, err := FunctionVars(inner, fnB); err == nil {
6746 t.Error("FunctionVars() over existing bindings wanted error, got nil")
6747 }
6748 })
6749 t.Run("Globals applied twice", func(t *testing.T) {
6750 defaultsA, err := FunctionVars(map[string]any{}, fnA)
6751 if err != nil {
6752 t.Fatalf("FunctionVars() failed: %v", err)
6753 }
6754 defaultsB, err := FunctionVars(map[string]any{}, fnB)
6755 if err != nil {
6756 t.Fatalf("FunctionVars() failed: %v", err)
6757 }
6758 if _, err := env.Program(ast, Globals(defaultsA), Globals(defaultsB)); err == nil {
6759 t.Error("Program() with two sources of bindings wanted error, got nil")
6760 }
6761 })
6762 t.Run("Globals and evaluation-time bindings", func(t *testing.T) {
6763 defaults, err := FunctionVars(map[string]any{}, fnA)
6764 if err != nil {
6765 t.Fatalf("FunctionVars() failed: %v", err)
6766 }
6767 prg, err := env.Program(ast, Globals(defaults))
6768 if err != nil {
6769 t.Fatalf("Program() failed: %v", err)
6770 }
6771 evalVars, err := FunctionVars(map[string]any{}, fnB)
6772 if err != nil {
6773 t.Fatalf("FunctionVars() failed: %v", err)
6774 }
6775 // The conflict only exists once the two hierarchies are combined, so it is reported by
6776 // Eval rather than by Program.
6777 if _, _, err := prg.Eval(evalVars); err == nil {
6778 t.Error("Eval() with bindings from defaults and input wanted error, got nil")
6779 }
6780 })
6781 t.Run("Globals alone remains valid", func(t *testing.T) {
6782 defaults, err := FunctionVars(map[string]any{}, fnA)
6783 if err != nil {
6784 t.Fatalf("FunctionVars() failed: %v", err)
6785 }
6786 prg, err := env.Program(ast, Globals(defaults))
6787 if err != nil {
6788 t.Fatalf("Program() failed: %v", err)

Callers

nothing calls this directly

Calls 10

IntTypeAlias · 0.92
FunctionVarsFunction · 0.85
GlobalsFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
CompileMethod · 0.65
EvalMethod · 0.65
EqualMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected