-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry_test.go
131 lines (119 loc) · 2.74 KB
/
entry_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
package desktop
import (
"io"
"os"
"reflect"
"testing"
)
type testData struct {
Filename string
Entry *Entry
}
var testCases = []*testData{
{
Filename: "app-alacritty.desktop",
Entry: &Entry{
Type: Application,
Actions: []string{"New"},
ActionEntries: []ActionEntry{
{
Name: "New Terminal",
Exec: "alacritty",
},
},
Name: "Alacritty",
GenericName: "Terminal",
Comment: "A cross-platform, GPU enhanced terminal emulator",
Icon: "Alacritty",
Path: "/home/test",
Exec: "alacritty",
},
},
{
Filename: "app-vivaldi.desktop",
Entry: &Entry{
Type: Application,
Name: "Vivaldi",
GenericName: "Web Browser",
Comment: "Access the Internet",
Exec: "/usr/bin/vivaldi-stable %U",
Terminal: false,
Icon: "vivaldi",
Actions: []string{"new-window", "new-private-window"},
ActionEntries: []ActionEntry{
{
Name: "New Window",
Exec: "/usr/bin/vivaldi-stable --new-window",
},
{
Name: "New Private Window",
Exec: "/usr/bin/vivaldi-stable --incognito",
},
},
},
},
{Filename: "app-vim.desktop", Entry: &Entry{Type: Application, Name: "Vim", GenericName: "Text Editor", Comment: "Edit text files", Icon: "gvim", Exec: "vim %F", Terminal: true}},
{Filename: "app-vim-nodisplay.desktop", Entry: nil},
{Filename: "link-google.desktop", Entry: &Entry{Type: Link, Name: "Link to Google", Icon: "text-html", URL: "https://google.com"}},
}
func TestParse(t *testing.T) {
var (
buf = make([]byte, bufferSize)
f *os.File
entry *Entry
err error
)
for _, c := range testCases {
f, err = os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
if err != nil {
t.Fatal(err)
}
entry, err = Parse(f, buf)
f.Close()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(entry, c.Entry) {
t.Fatalf("%s: entry incorrect: got %#v, want %#v", f.Name(), entry, c.Entry)
}
}
}
func BenchmarkParse(b *testing.B) {
var (
buf = make([]byte, bufferSize)
files = make([]*os.File, len(testCases))
entry *Entry
err error
)
defer func() {
for _, f := range files {
f.Close()
}
}()
for i, c := range testCases {
f, err := os.OpenFile("test/"+c.Filename, os.O_RDONLY, 0644)
if err != nil {
b.Fatal(err)
}
files[i] = f
}
b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for ci, f := range files {
b.StartTimer()
entry, err = Parse(f, buf)
if err != nil {
b.Fatal(err)
}
b.StopTimer()
if !reflect.DeepEqual(entry, testCases[ci].Entry) {
b.Fatalf("%s: entry incorrect: got %#v, want %#v", f.Name(), entry, testCases[ci].Entry)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
b.Fatal(err)
}
}
}
}