-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmenu_test.go
235 lines (202 loc) · 5.54 KB
/
menu_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
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
package menu
import (
"os"
"reflect"
"testing"
"github.com/libretro/ludo/state"
"github.com/libretro/ludo/video"
"github.com/tanema/gween"
"github.com/tanema/gween/ease"
)
func Test_WarpToQuickMenu(t *testing.T) {
m := Init(&video.Video{})
t.Run("Starts with a single scene if no game is running", func(t *testing.T) {
got := len(menu.stack)
want := 1
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
t.Run("Starts on the tabs scene if no game is running", func(t *testing.T) {
got := menu.stack[0].Entry().label
want := "Ludo"
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
state.CoreRunning = true
m.WarpToQuickMenu()
t.Run("Warps at the quick menu if a game is launched", func(t *testing.T) {
got := len(menu.stack)
want := 3
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
t.Run("Warps at the quick menu if a game is launched", func(t *testing.T) {
got := menu.stack[len(menu.stack)-1].Entry().label
want := "Quick Menu"
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
t.Run("No tweens are left after warping to quick menu", func(t *testing.T) {
got := len(menu.tweens)
want := 0
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
}
func Test_fastForwardTweens(t *testing.T) {
foo := float32(5)
menu.tweens[&foo] = gween.New(foo, 25, 0.015, ease.OutSine)
bar := float32(-10)
menu.tweens[&bar] = gween.New(bar, 0, 0.9, ease.OutSine)
menu.tweens.FastForward()
t.Run("No tweens are left", func(t *testing.T) {
got := len(menu.tweens)
want := 0
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
t.Run("Tweened vars have the right value", func(t *testing.T) {
got := foo
want := float32(25)
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
t.Run("Tweened vars have the right value", func(t *testing.T) {
got := bar
want := float32(0)
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v, want %v", got, want)
}
})
}
func Test_buildExplorer(t *testing.T) {
menu.stack = []Scene{}
exec := 0
cbMock := func(str string) {
exec++
}
dirActionMock := &entry{
label: "<Scan this directory>",
icon: "scan",
}
tmp := os.TempDir() + "/Test_buildExplorer/"
os.RemoveAll(tmp)
os.Mkdir(tmp, 0777)
defer os.RemoveAll(tmp)
os.Create(tmp + "File 1.txt")
os.Create(tmp + "File 2.img")
os.Create(tmp + "File 3.txt")
os.Create(tmp + "File 4.img") // this entry will be pushed at the end by sort+prettify
os.Create(tmp + "File 5.txt")
os.Create(tmp + "File 6.txt")
os.Create(tmp + "File 7.txt")
os.Mkdir(tmp+"Folder 1", 0777)
prettify := func(in string) string {
if in == "File 4" {
return "IMAGE 4"
}
return in
}
scene := buildExplorer(os.TempDir()+"/Test_buildExplorer/", []string{".img"}, cbMock, dirActionMock, prettify)
menu.Push(scene)
children := scene.Entry().children
t.Run("Should display the right number of menu entries", func(t *testing.T) {
// txt files are ignored because we filter on .img
if len(children) != 5 {
t.Errorf("buildExplorer = %v, want %v", len(children), 5)
}
})
t.Run("Inserts the directory Action as first entry", func(t *testing.T) {
if children[0].label != dirActionMock.label {
t.Errorf("buildExplorer = %v, want %v", children[0].label, dirActionMock.label)
}
})
t.Run("Inserts the directory .. as second entry", func(t *testing.T) {
if children[1].label != ".." {
t.Errorf("buildExplorer = %v, want %v", children[1].label, "..")
}
})
t.Run("Files have file icon", func(t *testing.T) {
if children[2].icon != "file" {
t.Errorf("buildExplorer = %v, want %v", children[2].icon, "file")
}
})
t.Run("Targeted files have OK callbacks", func(t *testing.T) {
children[2].callbackOK()
if exec != 1 {
t.Errorf("buildExplorer = %v, want %v", exec, 1)
}
})
t.Run("Folders have folder icon", func(t *testing.T) {
if children[3].icon != "folder" {
t.Errorf("buildExplorer = %v, want %v", children[4].icon, "folder")
}
})
t.Run("Folder callback opens the folder", func(t *testing.T) {
if len(menu.stack) != 1 {
t.Errorf("buildExplorer = %v, want %v", len(menu.stack), 1)
}
children[3].callbackOK()
if len(menu.stack) != 2 {
t.Errorf("buildExplorer = %v, want %v", len(menu.stack), 2)
}
})
t.Run("Prettifier should work", func(t *testing.T) {
want := "IMAGE 4"
if children[4].label != want {
t.Errorf("buildExplorer = %v, want %v", children[3].label, want)
}
})
}
func TestExtractTags(t *testing.T) {
var empty []string
tests := []struct {
name string
args string
want1 interface{}
want2 interface{}
}{
{
name: "No tags",
args: "My Awesome Game",
want1: "My Awesome Game",
want2: empty,
},
{
name: "One tag",
args: "My Awesome Game (France)",
want1: "My Awesome Game",
want2: []string{"France"},
},
{
name: "Multiple tags",
args: "My Awesome Game (France) (v1.0)",
want1: "My Awesome Game",
want2: []string{"France", "v1.0"},
},
{
name: "Nested tags",
args: "My Awesome Game (Europe) (Fr,De,En)",
want1: "My Awesome Game",
want2: []string{"Europe", "Fr", "De", "En"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1, got2 := extractTags(tt.args)
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("got1 = %v, want %v", got1, tt.want1)
}
if !reflect.DeepEqual(got2, tt.want2) {
t.Errorf("got2 = %v, want %v", got2, tt.want2)
}
})
}
}