-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathwidgets_test.go
More file actions
315 lines (254 loc) · 7.21 KB
/
widgets_test.go
File metadata and controls
315 lines (254 loc) · 7.21 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package xlog
import (
"html/template"
"testing"
)
// Test priorityList Add and sorting
func TestPriorityListAdd(t *testing.T) {
pl := &priorityList[string]{}
// Add items with different priorities
pl.Add("low", 10.0)
pl.Add("high", 1.0)
pl.Add("medium", 5.0)
// Collect items in order
var result []string
for item := range pl.All() {
result = append(result, item)
}
// Should be sorted by priority: high (1.0), medium (5.0), low (10.0)
expected := []string{"high", "medium", "low"}
if len(result) != len(expected) {
t.Fatalf("expected %d items, got %d", len(expected), len(result))
}
for i, v := range expected {
if result[i] != v {
t.Errorf("at position %d: expected %q, got %q", i, v, result[i])
}
}
}
// Test priorityList with equal priorities
func TestPriorityListEqualPriorities(t *testing.T) {
pl := &priorityList[int]{}
pl.Add(1, 5.0)
pl.Add(2, 5.0)
pl.Add(3, 5.0)
var result []int
for item := range pl.All() {
result = append(result, item)
}
// Should maintain stable sort order
if len(result) != 3 {
t.Fatalf("expected 3 items, got %d", len(result))
}
// All items should be present
found := make(map[int]bool)
for _, v := range result {
found[v] = true
}
for i := 1; i <= 3; i++ {
if !found[i] {
t.Errorf("item %d not found in result", i)
}
}
}
// Test priorityList with negative priorities
func TestPriorityListNegativePriorities(t *testing.T) {
pl := &priorityList[string]{}
pl.Add("zero", 0.0)
pl.Add("positive", 10.0)
pl.Add("negative", -5.0)
var result []string
for item := range pl.All() {
result = append(result, item)
}
expected := []string{"negative", "zero", "positive"}
if len(result) != len(expected) {
t.Fatalf("expected %d items, got %d", len(expected), len(result))
}
for i, v := range expected {
if result[i] != v {
t.Errorf("at position %d: expected %q, got %q", i, v, result[i])
}
}
}
// Test priorityList All iterator early termination
func TestPriorityListAllEarlyTermination(t *testing.T) {
pl := &priorityList[int]{}
for i := 1; i <= 10; i++ {
pl.Add(i, float32(i))
}
var result []int
for item := range pl.All() {
result = append(result, item)
if item == 5 {
break
}
}
if len(result) != 5 {
t.Errorf("expected early termination at 5 items, got %d", len(result))
}
for i, v := range result {
if v != i+1 {
t.Errorf("at position %d: expected %d, got %d", i, i+1, v)
}
}
}
// Test RegisterWidget and RenderWidget
func TestRegisterAndRenderWidget(t *testing.T) {
// Clean up widgets map after test
defer func() {
widgets = map[WidgetSpace]*priorityList[WidgetFunc]{}
}()
testSpace := WidgetSpace("test_space")
testPage := NewPage("test")
// Register widgets with different priorities
RegisterWidget(testSpace, 10.0, func(p Page) template.HTML {
return template.HTML("<div>low priority</div>")
})
RegisterWidget(testSpace, 1.0, func(p Page) template.HTML {
return template.HTML("<div>high priority</div>")
})
RegisterWidget(testSpace, 5.0, func(p Page) template.HTML {
return template.HTML("<div>medium priority</div>")
})
// Render widgets
result := RenderWidget(testSpace, testPage)
expected := template.HTML("<div>high priority</div><div>medium priority</div><div>low priority</div>")
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
// Test RenderWidget with non-existent space
func TestRenderWidgetNonExistent(t *testing.T) {
// Clean up widgets map after test
defer func() {
widgets = map[WidgetSpace]*priorityList[WidgetFunc]{}
}()
testSpace := WidgetSpace("non_existent")
testPage := NewPage("test")
result := RenderWidget(testSpace, testPage)
if result != "" {
t.Errorf("expected empty HTML for non-existent space, got %q", result)
}
}
// Test RegisterWidget creates space if not exists
func TestRegisterWidgetCreatesSpace(t *testing.T) {
// Clean up widgets map after test
defer func() {
widgets = map[WidgetSpace]*priorityList[WidgetFunc]{}
}()
testSpace := WidgetSpace("new_space")
testPage := NewPage("test")
// Register widget to non-existent space
RegisterWidget(testSpace, 1.0, func(p Page) template.HTML {
return template.HTML("<div>test</div>")
})
// Verify space was created
if _, ok := widgets[testSpace]; !ok {
t.Error("expected widget space to be created")
}
// Verify widget can be rendered
result := RenderWidget(testSpace, testPage)
if result != template.HTML("<div>test</div>") {
t.Errorf("expected widget to render, got %q", result)
}
}
// Test WidgetFunc receives correct page
func TestWidgetFuncReceivesPage(t *testing.T) {
// Clean up widgets map after test
defer func() {
widgets = map[WidgetSpace]*priorityList[WidgetFunc]{}
}()
testSpace := WidgetSpace("page_test")
testPage := NewPage("my-test-page")
var receivedName string
RegisterWidget(testSpace, 1.0, func(p Page) template.HTML {
receivedName = p.Name()
return template.HTML("")
})
RenderWidget(testSpace, testPage)
if receivedName != "my-test-page" {
t.Errorf("expected page name %q, got %q", "my-test-page", receivedName)
}
}
// Test multiple widgets in same space accumulate
func TestMultipleWidgetsAccumulate(t *testing.T) {
// Clean up widgets map after test
defer func() {
widgets = map[WidgetSpace]*priorityList[WidgetFunc]{}
}()
testSpace := WidgetSpace("accumulate_test")
testPage := NewPage("test")
// Register multiple widgets
for i := 1; i <= 5; i++ {
priority := float32(i)
RegisterWidget(testSpace, priority, func(p Page) template.HTML {
return template.HTML("<span>widget</span>")
})
}
result := RenderWidget(testSpace, testPage)
// Should have 5 widgets rendered
expected := template.HTML("<span>widget</span><span>widget</span><span>widget</span><span>widget</span><span>widget</span>")
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
// Test predefined widget spaces
func TestPredefinedWidgetSpaces(t *testing.T) {
tests := []struct {
space WidgetSpace
expected string
}{
{WidgetAfterView, "after_view"},
{WidgetBeforeView, "before_view"},
{WidgetHead, "head"},
}
for _, tt := range tests {
if string(tt.space) != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, string(tt.space))
}
}
}
// Test empty priorityList
func TestEmptyPriorityList(t *testing.T) {
pl := &priorityList[string]{}
count := 0
for range pl.All() {
count++
}
if count != 0 {
t.Errorf("expected 0 items from empty list, got %d", count)
}
}
// Test priorityList with single item
func TestPriorityListSingleItem(t *testing.T) {
pl := &priorityList[string]{}
pl.Add("only", 1.0)
var result []string
for item := range pl.All() {
result = append(result, item)
}
if len(result) != 1 || result[0] != "only" {
t.Errorf("expected [only], got %v", result)
}
}
// Test priorityList with float precision
func TestPriorityListFloatPrecision(t *testing.T) {
pl := &priorityList[string]{}
pl.Add("a", 1.1)
pl.Add("b", 1.2)
pl.Add("c", 1.15)
var result []string
for item := range pl.All() {
result = append(result, item)
}
expected := []string{"a", "c", "b"}
if len(result) != len(expected) {
t.Fatalf("expected %d items, got %d", len(expected), len(result))
}
for i, v := range expected {
if result[i] != v {
t.Errorf("at position %d: expected %q, got %q", i, v, result[i])
}
}
}