| 11 | ) |
| 12 | |
| 13 | func TestWithContext(t *testing.T) { |
| 14 | env := map[string]any{ |
| 15 | "fn": func(ctx context.Context, a int) int { |
| 16 | return ctx.Value("value").(int) + a |
| 17 | }, |
| 18 | "ctx": context.TODO(), |
| 19 | } |
| 20 | |
| 21 | withContext := patcher.WithContext{Name: "ctx"} |
| 22 | |
| 23 | program, err := expr.Compile(`fn(40)`, expr.Env(env), expr.Patch(withContext)) |
| 24 | require.NoError(t, err) |
| 25 | |
| 26 | ctx := context.WithValue(context.Background(), "value", 2) |
| 27 | env["ctx"] = ctx |
| 28 | |
| 29 | output, err := expr.Run(program, env) |
| 30 | require.NoError(t, err) |
| 31 | require.Equal(t, 42, output) |
| 32 | } |
| 33 | |
| 34 | func TestWithContext_with_env_Function(t *testing.T) { |
| 35 | env := map[string]any{ |