-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
testing.go
284 lines (242 loc) · 7.14 KB
/
testing.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package app
import (
"fmt"
"reflect"
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
// TestUIDescriptor represents a descriptor that describes a UI element and its
// location from its parents.
type TestUIDescriptor struct {
// The location of the node. It is used by the TestMatch to find the
// element to test.
//
// If empty, the expected UI element is compared with the root of the tree.
//
// Otherwise, each integer represents the index of the element to traverse,
// from the root's children to the element to compare
Path []int
// The element to compare with the element targeted by Path. Compare
// behavior varies depending on the element kind.
//
// Simple text elements only have their text value compared.
//
// HTML elements have their attribute compared and check if their event
// handlers are set.
//
// Components have their exported field values compared.
Expected UI
}
// TestPath is a helper function that returns a path to use in a
// TestUIDescriptor.
func TestPath(p ...int) []int {
return p
}
// TestMatch looks for the element targeted by the descriptor in the given tree
// and reports whether it matches with the expected element.
//
// Eg:
// tree := app.Div().Body(
// app.H2().Body(
// app.Text("foo"),
// ),
// app.P().Body(
// app.Text("bar"),
// ),
// )
//
// // Testing root:
// err := app.TestMatch(tree, app.TestUIDescriptor{
// Path: TestPath(),
// Expected: app.Div(),
// })
// // OK => err == nil
//
// // Testing h2:
// err := app.TestMatch(tree, app.TestUIDescriptor{
// Path: TestPath(0),
// Expected: app.H3(),
// })
// // KO => err != nil because we ask h2 to match with h3
//
// // Testing text from p:
// err = app.TestMatch(tree, app.TestUIDescriptor{
// Path: TestPath(1, 0),
// Expected: app.Text("bar"),
// })
// // OK => err == nil
func TestMatch(tree UI, d TestUIDescriptor) error {
if d.Expected != nil {
d.Expected.setSelf(d.Expected)
}
if len(d.Path) != 0 {
idx := d.Path[0]
if idx < 0 || idx >= len(tree.children()) {
// Check that the element does not exists.
if d.Expected == nil {
return nil
}
return errors.New("ui element to match is out of range").
Tag("name", d.Expected.name()).
Tag("kind", d.Expected.Kind()).
Tag("parent-name", tree.name()).
Tag("parent-kind", tree.Kind()).
Tag("parent-children-count", len(tree.children())).
Tag("index", idx)
}
c := tree.children()[idx]
p := c.parent()
if p != tree {
return errors.New("unexpected ui element parent").
Tag("name", d.Expected.name()).
Tag("kind", d.Expected.Kind()).
Tag("parent-name", p.name()).
Tag("parent-kind", p.Kind()).
Tag("parent-addr", fmt.Sprintf("%p", p)).
Tag("expected-parent-name", tree.name()).
Tag("expected-parent-kind", tree.Kind()).
Tag("expected-parent-addr", fmt.Sprintf("%p", tree))
}
d.Path = d.Path[1:]
return TestMatch(c, d)
}
if d.Expected.name() != tree.name() || d.Expected.Kind() != tree.Kind() {
return errors.New("the UI element is not matching the descriptor").
Tag("expected-name", d.Expected.name()).
Tag("expected-kind", d.Expected.Kind()).
Tag("current-name", tree.name()).
Tag("current-kind", tree.Kind())
}
switch d.Expected.Kind() {
case SimpleText:
return matchText(tree, d)
case HTML:
if err := matchHTMLElemAttrs(tree, d); err != nil {
return err
}
return matchHTMLElemEventHandlers(tree, d)
case Component:
return matchComponent(tree, d)
case RawHTML:
return matchRaw(tree, d)
default:
return errors.New("the UI element is not matching the descriptor").
Tag("reason", "unavailable matching for the kind").
Tag("kind", d.Expected.Kind())
}
}
func matchText(n UI, d TestUIDescriptor) error {
a := n.(*text)
b := d.Expected.(*text)
if a.value != b.value {
return errors.New("the text element is not matching the descriptor").
Tag("name", a.name()).
Tag("reason", "unexpected text value").
Tag("expected-value", b.value).
Tag("current-value", a.value)
}
return nil
}
func matchHTMLElemAttrs(n UI, d TestUIDescriptor) error {
aAttrs := n.attributes()
bAttrs := d.Expected.attributes()
if len(aAttrs) != len(bAttrs) {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "unexpected attributes length").
Tag("expected-attributes-length", len(bAttrs)).
Tag("current-attributes-length", len(aAttrs))
}
for k, b := range bAttrs {
a, exists := aAttrs[k]
if !exists {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "an attribute is missing").
Tag("attribute", k)
}
if a != b {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "unexpected attribute value").
Tag("attribute", k).
Tag("expected-value", b).
Tag("current-value", a)
}
}
for k := range bAttrs {
_, exists := bAttrs[k]
if !exists {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "an unexpected attribute is present").
Tag("attribute", k)
}
}
return nil
}
func matchHTMLElemEventHandlers(n UI, d TestUIDescriptor) error {
aevents := n.eventHandlers()
bevents := d.Expected.eventHandlers()
if len(aevents) != len(bevents) {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "unexpected event handlers length").
Tag("expected-event-handlers-length", len(bevents)).
Tag("current-event-handlers-length", len(aevents))
}
for k := range bevents {
_, exists := aevents[k]
if !exists {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "an event handler is missing").
Tag("event-handler", k)
}
}
for k := range bevents {
_, exists := aevents[k]
if !exists {
return errors.New("the html element is not matching the descriptor").
Tag("name", n.name()).
Tag("reason", "an unexpected event handler is present").
Tag("event-handler", k)
}
}
return nil
}
func matchComponent(n UI, d TestUIDescriptor) error {
aval := reflect.ValueOf(n).Elem()
bval := reflect.ValueOf(d.Expected).Elem()
compotype := reflect.TypeOf(Compo{})
for i := 0; i < bval.NumField(); i++ {
a := aval.Field(i)
b := bval.Field(i)
if a.Type() == compotype {
continue
}
if !a.CanSet() {
continue
}
if !reflect.DeepEqual(a.Interface(), b.Interface()) {
return errors.New("the component is not matching with the descriptor").
Tag("name", n.name()).
Tag("reason", "unexpected field value").
Tag("field", bval.Type().Field(i).Name).
Tag("expected-value", b.Interface()).
Tag("current-value", a.Interface())
}
}
return nil
}
func matchRaw(n UI, d TestUIDescriptor) error {
a := n.(*raw)
b := d.Expected.(*raw)
if a.value != b.value {
return errors.New("the raw html element is not matching with the descriptor").
Tag("name", n.name()).
Tag("reason", "unexpected value").
Tag("expected-value", b.value).
Tag("current-value", a.value)
}
return nil
}