-
-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathissue_test.go
More file actions
71 lines (63 loc) · 1.75 KB
/
issue_test.go
File metadata and controls
71 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package issue936
import (
"reflect"
"testing"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/internal/testify/require"
)
// TestIssue936 tests that dynamic struct types created with reflect.StructOf
// compile and evaluate correctly even when fields have lowercase names (which
// require PkgPath to be set, making them appear "unexported" to reflect).
func TestIssue936(t *testing.T) {
dynType := reflect.StructOf([]reflect.StructField{
{
Name: "value",
Type: reflect.TypeFor[bool](),
PkgPath: "github.com/some/package",
},
})
env := reflect.New(dynType).Elem().Interface()
// Compilation should succeed.
program, err := expr.Compile("value", expr.Env(env))
require.NoError(t, err)
// Evaluation should also succeed and return the zero value (false).
result, err := expr.Run(program, env)
require.NoError(t, err)
require.Equal(t, false, result)
}
// TestIssue936MultipleFields tests a dynamic struct with multiple field types.
func TestIssue936MultipleFields(t *testing.T) {
dynType := reflect.StructOf([]reflect.StructField{
{
Name: "name",
Type: reflect.TypeFor[string](),
PkgPath: "github.com/some/package",
},
{
Name: "count",
Type: reflect.TypeFor[int](),
PkgPath: "github.com/some/package",
},
{
Name: "active",
Type: reflect.TypeFor[bool](),
PkgPath: "github.com/some/package",
},
})
env := reflect.New(dynType).Elem().Interface()
for _, tc := range []struct {
expr string
}{
{`name == ""`},
{`count == 0`},
{`active == false`},
} {
t.Run(tc.expr, func(t *testing.T) {
program, err := expr.Compile(tc.expr, expr.Env(env))
require.NoError(t, err)
result, err := expr.Run(program, env)
require.NoError(t, err)
require.Equal(t, true, result)
})
}
}