forked from SoMuchForSubtlety/f1viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
224 lines (190 loc) Β· 5.47 KB
/
util_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
package main
import (
"errors"
"runtime"
"testing"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/stretchr/testify/assert"
)
func TestAvailableCommands(t *testing.T) {
t.Parallel()
_, s := newTestApp(t, 20, 5)
s.commands["test1"] = true
s.commands["test2"] = false
assert.True(t, s.commandAvailable("test1"))
assert.False(t, s.commandAvailable("test2"))
assert.False(t, s.commandAvailable("test3"))
}
func TestSanitizeFileName(t *testing.T) {
t.Parallel()
title := `file name: "with" <illegal> \/characters|`
var target string
title = sanitizeFileName(title)
if runtime.GOOS == "windows" {
target = `file name with illegal characters`
} else {
target = `file name: "with" <illegal> \ characters|`
}
assert.Equal(t, target, title)
}
var colorPairs = []struct {
hex string
name string
}{
{hex: "#bc8f8f", name: "rosybrown"},
{hex: "#fff5ee", name: "seashell"},
{hex: "#00ff7f", name: "springgreen"},
{hex: "#ffe4c4", name: "bisque"},
{hex: "#2f4f4f", name: "darkslategrey"},
{hex: "#b8860b", name: "darkgoldenrod"},
{hex: "#e0ffff", name: "lightcyan"},
{hex: "#66cdaa", name: "mediumaquamarine"},
{hex: "#ffdab9", name: "peachpuff"},
{hex: "#f4a460", name: "sandybrown"},
{hex: "#d8bfd8", name: "thistle"},
{hex: "#d3d3d3", name: "lightgrey"},
{hex: "#808080", name: "gray"},
{hex: "#a52a2a", name: "brown"},
{hex: "#e9967a", name: "darksalmon"},
{hex: "#dda0dd", name: "plum"},
{hex: "#708090", name: "slategray"},
{hex: "#ffffff", name: "white"},
}
func TestColorToHexString(t *testing.T) {
t.Parallel()
for _, s := range colorPairs {
hex := colortoHexString(tcell.GetColor(s.name))
assert.Equal(t, s.hex, hex)
}
}
func TestHexStringToColor(t *testing.T) {
t.Parallel()
for _, s := range colorPairs {
t.Run(s.name, func(t *testing.T) {
t.Parallel()
color := hexStringToColor(s.hex)
assert.Equal(t, color.Hex(), tcell.GetColor(s.name).Hex())
})
}
}
func TestWithBlink(t *testing.T) {
t.Parallel()
// TODO add check for colors
originalScreen := `
node titleββββββββββ
β β
β β
β β
ββββββββββ`
loadingScreen := `
loading...ββββββββββ
β β
β β
β β
ββββββββββ`
originalText := "node title"
originalColor := tcell.ColorViolet
node := tview.NewTreeNode(originalText)
node.SetColor(originalColor)
simScreen, s := newTestApp(t, 20, 5)
s.tree.GetRoot().AddChild(node)
go func() {
err := s.app.Run()
assert.NoError(t, err)
}()
go s.withBlink(node, func() {
time.Sleep(time.Millisecond * 200)
})()
time.Sleep(time.Millisecond * 100)
assert.Equal(t, loadingScreen, toTextScreen(simScreen))
time.Sleep(time.Millisecond * 1000)
assert.Equal(t, originalScreen, toTextScreen(simScreen))
assert.Equal(t, originalColor, node.GetColor())
assert.Equal(t, originalText, node.GetText())
}
func TestGetYearAndRace(t *testing.T) {
t.Parallel()
// TODO add checks for post 2020 events
year, race, err := getYearAndRace("1914_ITA_FP2_F1TV")
assert.Nil(t, err)
assert.Equal(t, "2019", year)
assert.Equal(t, "14", race)
year, race, err = getYearAndRace("9414_ABC")
assert.Nil(t, err)
assert.Equal(t, "1994", year)
assert.Equal(t, "14", race)
year, race, err = getYearAndRace("2018_TEST")
assert.Nil(t, err)
assert.Equal(t, "2018", year)
assert.Equal(t, "0", race)
_, _, err = getYearAndRace("abcde")
assert.EqualError(t, err, "not a valid YearRaceID")
_, _, err = getYearAndRace("123")
assert.EqualError(t, err, "not long enough")
}
func TestLog(t *testing.T) {
t.Parallel()
expectedInfo := `
βββββββββββββββ
βINFO: info β
β β
β β
βββββββββββββββ`
expectedError := `
βββββββββββββββ
βINFO: info β
βERROR: test β
β β
βββββββββββββββ`
simScreen, s := newTestApp(t, 30, 5)
go func() {
err := s.app.Run()
assert.NoError(t, err)
}()
s.logInfo("info")
time.Sleep(time.Millisecond * 100)
assert.Equal(t, expectedInfo, toTextScreen(simScreen))
s.logError(errors.New("test"))
time.Sleep(time.Millisecond * 100)
assert.Equal(t, expectedError, toTextScreen(simScreen))
}
func toTextScreen(screen tcell.SimulationScreen) string {
content := "\n"
contents, width, _ := screen.GetContents()
var cursor int
for _, cell := range contents {
if cursor >= width {
content += "\n"
cursor = 0
}
content += string(cell.Bytes)
cursor++
}
return content
}
func newTestApp(t *testing.T, x, y int) (tcell.SimulationScreen, viewerSession) {
simScreen := tcell.NewSimulationScreen("UTF-8")
err := simScreen.Init()
assert.NoError(t, err)
simScreen.SetSize(x, y)
app := tview.NewApplication()
app.SetScreen(simScreen)
text := tview.NewTextView().
SetWordWrap(false).
SetWrap(false).
SetDynamicColors(true).
SetChangedFunc(func() {
app.Draw()
})
text.SetBorder(true)
tree := tview.NewTreeView().
SetRoot(tview.NewTreeNode("root")).
SetTopLevel(1)
flex := tview.NewFlex()
flex.AddItem(tree, 0, 1, true)
flex.AddItem(text, 0, 1, false)
app.SetRoot(flex, true)
return simScreen, viewerSession{tree: tree, app: app, textWindow: text, commands: make(map[string]bool)}
}