MCPcopy Index your code
hub / github.com/expr-lang/expr / TestWithContext_with_env_Function

Function TestWithContext_with_env_Function

patcher/with_context_test.go:34–63  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32}
33
34func TestWithContext_with_env_Function(t *testing.T) {
35 env := map[string]any{
36 "ctx": context.TODO(),
37 }
38
39 fn := expr.Function("fn",
40 func(params ...any) (any, error) {
41 ctx := params[0].(context.Context)
42 a := params[1].(int)
43
44 return ctx.Value("value").(int) + a, nil
45 },
46 new(func(context.Context, int) int),
47 )
48
49 program, err := expr.Compile(
50 `fn(40)`,
51 expr.Env(env),
52 expr.WithContext("ctx"),
53 fn,
54 )
55 require.NoError(t, err)
56
57 ctx := context.WithValue(context.Background(), "value", 2)
58 env["ctx"] = ctx
59
60 output, err := expr.Run(program, env)
61 require.NoError(t, err)
62 require.Equal(t, 42, output)
63}
64
65type testEnvContext struct {
66 Context context.Context `expr:"ctx"`

Callers

nothing calls this directly

Calls 7

FunctionFunction · 0.92
CompileFunction · 0.92
EnvStruct · 0.92
WithContextFunction · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92
EqualFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…