This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpression_test.go
166 lines (140 loc) · 3.78 KB
/
expression_test.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package el_test
import (
"testing"
"encoding/json"
"github.com/lysu/go-el"
"github.com/stretchr/testify/assert"
"strconv"
)
type User struct {
Name string
BizState map[string]int
ImgIDList []int
Images []*Image
ImgIdx map[string]*Image
}
type Image struct {
Content string
}
func (t User) FindImage(i int) *Image {
return t.Images[i]
}
func (t User) LocateImage(i int) *Image {
return t.ImgIdx[strconv.Itoa(i)]
}
func TestLocate(t *testing.T) {
data := User{
Name: "ほん",
ImgIDList: []int{0, 1, 2},
Images: []*Image{{"1.jpg"}, {"2.jpg"}, {"3.jpg"}},
ImgIdx: map[string]*Image{
"0": {"しゃしん1.jpg"},
"1": {"写真2.jpg"},
"2": {"写真3.jpg"},
},
}
exp := el.Expression("Name")
v, err := exp.Execute(&data)
assert.NoError(t, err)
err = v.SetValue("zzzz")
assert.NoError(t, err)
assert.Equal(t, "zzzz", data.Name)
exp = el.Expression("ImgIDList.0")
v, err = exp.Execute(&data)
assert.NoError(t, err)
err = v.SetValue(json.Number("9"))
assert.NoError(t, err)
assert.Equal(t, 9, data.ImgIDList[0])
exp = el.Expression("ImgIDList[0]")
v, err = exp.Execute(&data)
assert.NoError(t, err)
assert.Equal(t, 9, data.ImgIDList[0])
exp = el.Expression("Images[ImgIDList[2]].Content")
v, err = exp.Execute(&data)
assert.NoError(t, err)
err = v.SetValue("しゃ")
assert.NoError(t, err)
assert.Equal(t, "しゃ", v.String())
}
func TestFunctionCall(t *testing.T) {
data := User{
Name: "ほん",
ImgIDList: []int{0, 1, 2},
Images: []*Image{{"1.jpg"}, {"2.jpg"}, {"3.jpg"}},
ImgIdx: map[string]*Image{
"0": {"しゃしん1.jpg"},
"1": {"写真2.jpg"},
"2": {"写真3.jpg"},
},
}
exp := el.Expression("FindImage(ImgIDList.1).Content")
v, err := exp.Execute(&data)
assert.NoError(t, err)
err = v.SetValue("なに")
assert.NoError(t, err)
assert.Equal(t, "なに", data.Images[1].Content)
exp = el.Expression("LocateImage(ImgIDList.2).Content")
v, err = exp.Execute(&data)
assert.NoError(t, err)
err = v.SetValue("なん")
assert.NoError(t, err)
assert.Equal(t, "なん", data.ImgIdx["2"].Content)
}
func TestIndexAccess(t *testing.T) {
user := User{
Name: "ほん",
ImgIDList: []int{0, 1, 2},
Images: []*Image{{"1.jpg"}, {"2.jpg"}, {"3.jpg"}},
ImgIdx: map[string]*Image{
"0": {"しゃしん1.jpg"},
"1": {"しゃしん2.jpg"},
"2": {"しゃしん3.jpg"},
},
}
exp := el.Expression("ImgIDList[2]")
v, err := exp.Execute(&user)
assert.NoError(t, err)
assert.Equal(t, 2, v.Integer())
err = v.SetValue(7)
assert.NoError(t, err)
assert.Equal(t, 7, user.ImgIDList[2])
exp = el.Expression("ImgIdx[2].Content")
v, err = exp.Execute(&user)
assert.NoError(t, err)
assert.Equal(t, "しゃしん3.jpg", v.String())
err = v.SetValue("しゃしん4.jpg")
assert.NoError(t, err)
assert.Equal(t, "しゃしん4.jpg", user.ImgIdx["2"].Content)
exp = el.Expression("ImgIdx[ImgIDList[0]].Content")
v, err = exp.Execute(&user)
assert.NoError(t, err)
assert.Equal(t, "しゃしん1.jpg", v.String())
err = v.SetValue("しゃしん233.jpg")
assert.NoError(t, err)
assert.Equal(t, "しゃしん233.jpg", user.ImgIdx["0"].Content)
}
func TestIndexSet(t *testing.T) {
user := User{
Name: "ほん",
ImgIDList: []int{0, 1, 2},
Images: []*Image{{"1.jpg"}, {"2.jpg"}, {"3.jpg"}},
ImgIdx: map[string]*Image{
"0": {"しゃしん1.jpg"},
"1": {"しゃしん2.jpg"},
"2": {"しゃしん3.jpg"},
},
BizState: map[string]int{},
}
exp := el.Expression("BizState[3]")
v, err := exp.Execute(&user)
assert.NoError(t, err)
assert.True(t, v.IsNil())
err = v.SetValue(3)
assert.NoError(t, err)
assert.Equal(t, 3, user.BizState["3"])
exp = el.Expression("ImgIDList[99]")
v, err = exp.Execute(&user)
assert.Nil(t, err)
v.SetValue(99)
assert.Equal(t, 99, user.ImgIDList[99])
}