-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement_test.go
More file actions
203 lines (180 loc) · 5.88 KB
/
Copy pathelement_test.go
File metadata and controls
203 lines (180 loc) · 5.88 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
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
package wui
import "testing"
func TestIfAndIfElse(t *testing.T) {
if _, isEmpty := If(false, Text("x")).(EmptyEl); !isEmpty {
t.Error("If(false, …) did not return EmptyEl")
}
if _, isText := If(true, Text("x")).(TextEl); !isText {
t.Error("If(true, …) did not return the element")
}
// A nil branch is normalised, so callers never get a nil Element.
if _, isEmpty := If(true, nil).(EmptyEl); !isEmpty {
t.Error("If(true, nil) did not return EmptyEl")
}
yes, no := Text("yes"), Text("no")
if got := IfElse(true, yes, no).(TextEl); got.Content != "yes" {
t.Errorf("IfElse(true) = %q", got.Content)
}
if got := IfElse(false, yes, no).(TextEl); got.Content != "no" {
t.Errorf("IfElse(false) = %q", got.Content)
}
}
// When must not call build on the false branch — that is the whole
// reason it exists alongside If.
func TestWhenIsLazy(t *testing.T) {
called := false
When(false, func() Element {
called = true
return Text("x")
})
if called {
t.Error("When(false, …) called build")
}
if _, isText := When(true, func() Element { return Text("x") }).(TextEl); !isText {
t.Error("When(true, …) did not return the built element")
}
if _, isEmpty := When(true, nil).(EmptyEl); !isEmpty {
t.Error("When(true, nil) did not return EmptyEl")
}
}
func TestMapBuildsOnePerItem(t *testing.T) {
got := Map([]string{"a", "b"}, func(i int, s string) Element {
return Text(s)
})
if len(got) != 2 {
t.Fatalf("Map returned %d elements, want 2", len(got))
}
if got[0].(TextEl).Content != "a" || got[1].(TextEl).Content != "b" {
t.Errorf("Map produced %+v", got)
}
if len(Map([]string{}, func(int, string) Element { return nil })) != 0 {
t.Error("Map over an empty slice returned elements")
}
}
func TestSpinnerGlyphWraps(t *testing.T) {
n := SpinnerFrames()
if got, want := spinnerGlyph(0), spinnerGlyph(n); got != want {
t.Errorf("frame 0 = %q, frame %d = %q, want equal", got, n, want)
}
// A counter that goes negative must not panic or index out of range.
if spinnerGlyph(-1) != spinnerGlyph(n-1) {
t.Error("negative frame did not wrap to the last glyph")
}
}
func TestClampUnit(t *testing.T) {
for _, tc := range []struct{ in, want float64 }{
{-1, 0}, {0, 0}, {0.5, 0.5}, {1, 1}, {2, 1},
} {
if got := clampUnit(tc.in); got != tc.want {
t.Errorf("clampUnit(%v) = %v, want %v", tc.in, got, tc.want)
}
}
}
func TestSelectLabelFallsBack(t *testing.T) {
opts := []SelectOption{{Value: "s", Label: "Small"}, {Value: "m", Label: "Medium"}}
if got := selectLabel(SelectEl{Options: opts, Value: "m"}); got != "Medium" {
t.Errorf("selectLabel = %q, want %q", got, "Medium")
}
// An unset value shows the first option, the way a browser
// <select> displays its first entry.
if got := selectLabel(SelectEl{Options: opts}); got != "Small" {
t.Errorf("selectLabel with no value = %q, want %q", got, "Small")
}
// A value with no matching option still displays.
if got := selectLabel(SelectEl{Options: opts, Value: "xl"}); got != "xl" {
t.Errorf("selectLabel with unknown value = %q, want %q", got, "xl")
}
if got := selectLabel(SelectEl{}); got != "" {
t.Errorf("selectLabel with no options = %q, want empty", got)
}
}
func TestSelectCycleWraps(t *testing.T) {
e := SelectEl{Options: Options("a", "b", "c"), Value: "c"}
if got := selectCycle(e, 1); got != "a" {
t.Errorf("cycle forward from last = %q, want %q", got, "a")
}
e.Value = "a"
if got := selectCycle(e, -1); got != "c" {
t.Errorf("cycle back from first = %q, want %q", got, "c")
}
if got := selectCycle(SelectEl{}, 1); got != "" {
t.Errorf("cycle with no options = %q, want empty", got)
}
}
func TestSelectChangeMsgFallsBackToInputMsg(t *testing.T) {
withCallback := SelectEl{ID: "size", OnChange: func(v string) Msg { return ClickMsg{TargetID: v} }}
if _, isClick := selectChangeMsg(withCallback, "m").(ClickMsg); !isClick {
t.Error("OnChange callback was not used")
}
msg, isInput := selectChangeMsg(SelectEl{ID: "size"}, "m").(InputMsg)
if !isInput {
t.Fatal("selectChangeMsg without OnChange did not produce InputMsg")
}
if msg.ID != "size" || msg.Value != "m" {
t.Errorf("InputMsg = %+v", msg)
}
}
func TestChildrenAndWalkCoverContainers(t *testing.T) {
tree := Box(Column,
Card("t", Text("in-card")),
Form(nil, Text("in-form")),
List(false, Text("in-list")),
Scroll(Text("in-scroll"), 5),
)
var texts []string
Walk(tree, func(e Element) bool {
if t, ok := e.(TextEl); ok {
texts = append(texts, t.Content)
}
return true
})
if len(texts) != 4 {
t.Errorf("Walk found %d texts, want 4: %v", len(texts), texts)
}
if Children(Text("leaf")) != nil {
t.Error("Children of a leaf returned non-nil")
}
// A container with a nil child must not produce a nil entry for
// walkers to trip over.
if got := Children(CardEl{}); got != nil {
t.Errorf("Children(CardEl{}) = %+v, want nil", got)
}
}
// Walk's false return prunes the subtree — used to stop descending once
// a match is found.
func TestWalkPrunesOnFalse(t *testing.T) {
tree := Box(Column, Box(Column, Text("deep")))
var seen int
Walk(tree, func(e Element) bool {
seen++
return false
})
if seen != 1 {
t.Errorf("Walk visited %d elements after returning false, want 1", seen)
}
}
func TestFindReturnsFirstMatch(t *testing.T) {
tree := Box(Column, Text("first"), Text("second"))
got, ok := Find(tree, func(e Element) bool {
_, isText := e.(TextEl)
return isText
})
if !ok {
t.Fatal("Find returned ok=false")
}
if got.(TextEl).Content != "first" {
t.Errorf("Find returned %q, want %q", got.(TextEl).Content, "first")
}
if _, ok := Find(tree, func(Element) bool { return false }); ok {
t.Error("Find returned ok=true with no match")
}
}
func TestOptionsHelper(t *testing.T) {
opts := Options("a", "b")
if len(opts) != 2 || opts[0].Value != "a" || opts[0].Label != "a" {
t.Errorf("Options = %+v", opts)
}
if got := Opt("v", ""); got.Value != "v" || got.Label != "" {
t.Errorf("Opt = %+v", got)
}
}