MCPcopy Create free account
hub / github.com/expr-lang/expr / TestIssue836

Function TestIssue836

test/issues/836/issue_test.go:14–101  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12}
13
14func TestIssue836(t *testing.T) {
15 str := "foo"
16 ptrStr := &str
17 b := true
18 ptrBool := &b
19 i := 1
20 ptrInt := &i
21
22 env := map[string]interface{}{
23 "ptrStr": ptrStr,
24 "ptrBool": ptrBool,
25 "ptrInt": ptrInt,
26 "arr": []int{1, 2, 3},
27 "mapPtr": map[*int]int{ptrInt: 42},
28 }
29
30 t.Run("map access with pointer key", func(t *testing.T) {
31 program, err := expr.Compile(`{"foo": "bar"}[ptrStr]`, expr.Env(env))
32 require.NoError(t, err)
33
34 output, err := expr.Run(program, env)
35 require.NoError(t, err)
36 require.Equal(t, "bar", output)
37 })
38
39 t.Run("conditional with pointer condition", func(t *testing.T) {
40 program, err := expr.Compile(`ptrBool ? 1 : 0`, expr.Env(env))
41 require.NoError(t, err)
42
43 output, err := expr.Run(program, env)
44 require.NoError(t, err)
45 require.Equal(t, 1, output)
46 })
47
48 t.Run("get() with pointer key", func(t *testing.T) {
49 program, err := expr.Compile(`get({"foo": "bar"}, ptrStr)`, expr.Env(env))
50 require.NoError(t, err)
51
52 output, err := expr.Run(program, env)
53 require.NoError(t, err)
54 require.Equal(t, "bar", output)
55 })
56
57 t.Run("struct field pointer check in ternary", func(t *testing.T) {
58 var v InputStruct
59 // v.Enabled is nil
60
61 env := map[string]any{
62 "v": v,
63 }
64
65 code := `v.Enabled == nil ? 'default' : ( v.Enabled ? 'enabled' : 'disabled' )`
66
67 program, err := expr.Compile(code, expr.Env(env))
68 require.NoError(t, err)
69
70 output, err := expr.Run(program, env)
71 require.NoError(t, err)

Callers

nothing calls this directly

Calls 6

CompileFunction · 0.92
EnvStruct · 0.92
NoErrorFunction · 0.92
RunFunction · 0.92
EqualFunction · 0.92
RunMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…