-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathparse_test.go
More file actions
286 lines (269 loc) · 6.47 KB
/
parse_test.go
File metadata and controls
286 lines (269 loc) · 6.47 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
package main
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
var parseCmpOpts = cmp.AllowUnexported(measurement{}, measurementEntry{})
func TestParseOK(t *testing.T) {
tmpfile, err := os.CreateTemp("", "__test_parse_ok")
if err != nil {
panic(err)
}
tmpname := tmpfile.Name()
tmpfile.Close()
defer func() {
os.Remove(tmpname)
}()
header := `
times in msec
clock self+sourced self: sourced script
clock elapsed: other lines
`
for _, tc := range []struct {
what string
lines []string
expected *measurement
}{
{
"system only",
[]string{
"000.008 000.008: --- VIM STARTING ---",
"000.190 000.182: Allocated generic buffers",
},
&measurement{
time.Duration(190) * time.Microsecond,
[]*measurementEntry{
{
false,
time.Duration(8) * time.Microsecond,
time.Duration(8) * time.Microsecond,
time.Duration(0),
"--- VIM STARTING ---",
},
{
false,
time.Duration(190) * time.Microsecond,
time.Duration(182) * time.Microsecond,
time.Duration(0),
"Allocated generic buffers",
},
},
},
},
{
"script only",
[]string{
"012.696 000.429 000.429: sourcing /foo/bar.vim",
"013.270 001.106 000.677: sourcing $VIM/vimrc",
},
&measurement{
time.Duration(13270) * time.Microsecond,
[]*measurementEntry{
{
true,
time.Duration(12696) * time.Microsecond,
time.Duration(429) * time.Microsecond,
time.Duration(429) * time.Microsecond,
"/foo/bar.vim",
},
{
true,
time.Duration(13270) * time.Microsecond,
time.Duration(1106) * time.Microsecond,
time.Duration(677) * time.Microsecond,
"$VIM/vimrc",
},
},
},
},
{
"mixed",
[]string{
"198.369 000.161 000.161: sourcing /foo/bar.vim",
"198.465 001.679: BufEnter autocommands",
"198.467 000.002: editing files in windows",
"200.107 001.135 001.135: sourcing $VIM/vimrc",
},
&measurement{
time.Duration(200107) * time.Microsecond,
[]*measurementEntry{
{
true,
time.Duration(198369) * time.Microsecond,
time.Duration(161) * time.Microsecond,
time.Duration(161) * time.Microsecond,
"/foo/bar.vim",
},
{
false,
time.Duration(198465) * time.Microsecond,
time.Duration(1679) * time.Microsecond,
time.Duration(0),
"BufEnter autocommands",
},
{
false,
time.Duration(198467) * time.Microsecond,
time.Duration(2) * time.Microsecond,
time.Duration(0),
"editing files in windows",
},
{
true,
time.Duration(200107) * time.Microsecond,
time.Duration(1135) * time.Microsecond,
time.Duration(1135) * time.Microsecond,
"$VIM/vimrc",
},
},
},
},
{
"lua for neovim (#2)",
[]string{
"021.963 000.003: parsing arguments",
"023.515 000.278 000.278: require('vim.shared')",
"023.699 000.101 000.101: require('vim._meta')",
},
&measurement{
time.Duration(23699) * time.Microsecond,
[]*measurementEntry{
{
false,
time.Duration(21963) * time.Microsecond,
time.Duration(3) * time.Microsecond,
time.Duration(0),
"parsing arguments",
},
{
true,
time.Duration(23515) * time.Microsecond,
time.Duration(278) * time.Microsecond,
time.Duration(278) * time.Microsecond,
"require('vim.shared')",
},
{
true,
time.Duration(23699) * time.Microsecond,
time.Duration(101) * time.Microsecond,
time.Duration(101) * time.Microsecond,
"require('vim._meta')",
},
},
},
},
} {
t.Run(tc.what, func(t *testing.T) {
content := []byte(header + strings.Join(tc.lines, "\n") + "\n")
if err := os.WriteFile(tmpname, content, 0644); err != nil {
panic(err)
}
f, err := os.Open(tmpname)
if err != nil {
panic(err)
}
defer f.Close()
m, err := parseStartuptime(f)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(m, tc.expected, parseCmpOpts) {
t.Fatal(cmp.Diff(m, tc.expected, parseCmpOpts))
}
})
}
}
func TestParseErrors(t *testing.T) {
tmpfile, err := os.CreateTemp("", "__test_parse_errors")
if err != nil {
panic(err)
}
tmpname := tmpfile.Name()
tmpfile.Close()
defer func() {
os.Remove(tmpname)
}()
header := []string{
"",
"",
"times in msec",
"clock self+sourced self: sourced script",
"clock elapsed: other lines",
"",
}
for _, tc := range []struct {
what string
lines []string
msg string
line uint
}{
{
what: "empty file",
lines: []string{""},
msg: "broken --startuptime output while parsing file",
},
{
what: "invalid float at elapsed time",
lines: append(header, "00-.008 000.008: --- VIM STARTING ---"),
msg: "cannot parse field '00-.008' as millisec duration",
line: 7,
},
{
what: "invalid float at self+source",
lines: append(header, "000.008 000.a08: --- VIM STARTING ---"),
msg: "cannot parse field '000.a08' as millisec duration",
line: 7,
},
{
what: "invalid float at self",
lines: append(header, "198.369 000.161 000.!61: sourcing /foo/bar.vim"),
msg: "cannot parse field '000.!61' as millisec duration",
line: 7,
},
{
what: "script name is not existing",
lines: append(header, "198.369 000.161 000.161: sourcing"),
msg: "script name is missing",
line: 7,
},
{
what: "empty description",
lines: append(header, "198.369 000.161 000.161:"),
msg: "too few fields",
line: 7,
},
{
what: "'sourcing' is missing",
lines: append(header, "198.369 000.161 000.161: /foo/bar.vim foo"),
msg: "'sourcing' token or 'require(...)' token is expected but got '/foo/bar.vim'",
line: 7,
},
} {
t.Run(tc.what, func(t *testing.T) {
content := []byte(strings.Join(tc.lines, "\n") + "\n")
if err := os.WriteFile(tmpname, content, 0644); err != nil {
panic(err)
}
f, err := os.Open(tmpname)
if err != nil {
panic(err)
}
defer f.Close()
_, err = parseStartuptime(f)
if err == nil {
t.Fatal("Error did not happen:", tc.msg)
}
msg := err.Error()
if !strings.Contains(msg, tc.msg) {
t.Fatalf("Unexpected error. '%s' is not in '%s'", tc.msg, msg)
}
if tc.line != 0 && !strings.Contains(msg, fmt.Sprintf("parse error at line:%d:", tc.line)) {
t.Fatal("Error should occur at line", tc.line, "(error:", msg, ")")
}
})
}
}