MCPcopy
hub / github.com/expr-lang/expr / TestIssue823

Function TestIssue823

test/issues/823/issue_test.go:19–57  ·  view source on GitHub ↗

TestIssue823 verifies that WithContext injects context into nested custom function calls. The bug was that date2() nested as an argument to After() didn't receive the context because its callee type was unknown.

(t *testing.T)

Source from the content-addressed store, hash-verified

17// function calls. The bug was that date2() nested as an argument to After()
18// didn't receive the context because its callee type was unknown.
19func TestIssue823(t *testing.T) {
20 now2Called := false
21 date2Called := false
22
23 p, err := expr.Compile(
24 "now2().After(date2())",
25 expr.Env(env{}),
26 expr.WithContext("ctx"),
27 expr.Function(
28 "now2",
29 func(params ...any) (any, error) {
30 require.Len(t, params, 1, "now2 should receive context")
31 _, ok := params[0].(context.Context)
32 require.True(t, ok, "now2 first param should be context.Context")
33 now2Called = true
34 return time.Now(), nil
35 },
36 new(func(context.Context) time.Time),
37 ),
38 expr.Function(
39 "date2",
40 func(params ...any) (any, error) {
41 require.Len(t, params, 1, "date2 should receive context")
42 _, ok := params[0].(context.Context)
43 require.True(t, ok, "date2 first param should be context.Context")
44 date2Called = true
45 return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), nil
46 },
47 new(func(context.Context) time.Time),
48 ),
49 )
50 require.NoError(t, err)
51
52 r, err := expr.Run(p, &env{Ctx: context.Background()})
53 require.NoError(t, err)
54 require.True(t, r.(bool))
55 require.True(t, now2Called, "now2 should have been called")
56 require.True(t, date2Called, "date2 should have been called")
57}
58
59// envWithMethods tests that Env methods with context.Context work correctly
60// when nested in method chains (similar to TestIssue823 but with Env methods).

Callers

nothing calls this directly

Calls 8

CompileFunction · 0.92
EnvStruct · 0.92
WithContextFunction · 0.92
FunctionFunction · 0.92
LenFunction · 0.92
TrueFunction · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…