forked from tetafro/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks_test.go
471 lines (455 loc) · 9.34 KB
/
checks_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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package godot
import "testing"
func TestCheckPeriod(t *testing.T) {
testCases := []struct {
name string
text string
ok bool
issue position
}{
{
name: "singleline text with period",
text: "Hello, world.",
ok: true,
},
{
name: "singleline text with period and indentation",
text: " Hello, world.",
ok: true,
},
{
name: "multiple text with period",
text: "Hello,\nworld.",
ok: true,
},
{
name: "multiple text with period and empty lines",
text: "\nHello, world.\n",
ok: true,
},
{
name: "singleline text with no period",
text: "Hello, world",
issue: position{line: 1, column: 13},
},
{
name: "multiple text with no period",
text: "\nHello,\nworld\n",
issue: position{line: 3, column: 6},
},
{
name: "question mark",
text: "Hello, world?",
ok: true,
},
{
name: "exclamation mark",
text: "Hello, world!",
ok: true,
},
{
name: "empty line",
text: "",
ok: true,
},
{
name: "empty lines",
text: "\n\n",
ok: true,
},
{
name: "only spaces",
text: " ",
ok: true,
},
{
name: "mixed spaces",
text: "\t\t ",
ok: true,
},
{
name: "mixed spaces and newlines",
text: " \t\t \n\n\n \n\t ",
ok: true,
},
{
name: "cyrillic, with period",
text: "Кириллица.",
ok: true,
},
{
name: "cyrillic, without period",
text: "Кириллица",
issue: position{line: 1, column: 19},
},
{
name: "parenthesis, with period",
text: "Hello. (World.)",
ok: true,
},
{
name: "parenthesis, without period",
text: "Hello. (World)",
issue: position{line: 1, column: 15},
},
{
name: "single closing parenthesis with period",
text: ").",
ok: true,
},
{
name: "single closing parenthesis without period",
text: ")",
issue: position{line: 1, column: 2},
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
pos, ok := checkPeriod(tt.text)
if ok != tt.ok {
t.Fatalf("Wrong result\n expected: %v\n got: %v", tt.ok, ok)
}
if pos.line != tt.issue.line {
t.Fatalf("Wrong line\n expected: %d\n got: %d", tt.issue.line, pos.line)
}
if pos.column != tt.issue.column {
t.Fatalf("Wrong column\n expected: %d\n got: %d", tt.issue.column, pos.column)
}
})
}
}
func TestCheckCapital(t *testing.T) {
testCases := []struct {
name string
text string
skipFirst bool
issues []position
}{
{
name: "single sentence starting with a capital letter",
text: "Hello, world.",
skipFirst: false,
},
{
name: "single sentence starting with a lowercase letter",
text: "hello, world.",
skipFirst: false,
issues: []position{
{line: 1, column: 1},
},
},
{
name: "multiple sentences with mixed cases",
text: "hello, world. Hello, world. hello? hello!\n\nhello, world.",
skipFirst: false,
issues: []position{
{line: 1, column: 1},
{line: 1, column: 29},
{line: 1, column: 36},
{line: 3, column: 1},
},
},
{
name: "multiple sentences with mixed cases, first letter skipped",
text: "hello, world. Hello, world. hello? hello!\n\nhello, world.",
skipFirst: true,
issues: []position{
{line: 1, column: 29},
{line: 1, column: 36},
{line: 3, column: 1},
},
},
{
name: "multiple sentences with cyrillic letters",
text: "Кириллица? кириллица!",
skipFirst: false,
issues: []position{
{line: 1, column: 21},
},
},
{
name: "sentence with leading spaces",
text: " hello, world",
skipFirst: false,
issues: []position{
{line: 1, column: 5},
},
},
{
name: "sentence with abbreviations",
text: "One two, i.e. hello, world, e.g. e. g. word and etc. word",
skipFirst: false,
issues: nil,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
issues := checkCapital(tt.text, tt.skipFirst)
if len(issues) != len(tt.issues) {
t.Fatalf("Wrong number of issues\n expected: %d\n got: %d",
len(tt.issues), len(issues))
}
for i := range issues {
if issues[i].line != tt.issues[i].line {
t.Fatalf("Wrong line\n expected: %d\n got: %d",
tt.issues[i].line, issues[i].line)
}
if issues[i].column != tt.issues[i].column {
t.Fatalf("Wrong column\n expected: %d\n got: %d",
tt.issues[i].column, issues[i].column)
}
}
})
}
}
func TestIsSpecialBlock(t *testing.T) {
testCases := []struct {
name string
comment string
isSpecial bool
}{
{
name: "regular text",
comment: "Hello, world.",
isSpecial: false,
},
{
name: "comment",
comment: "// Hello, world",
isSpecial: false,
},
{
name: "empty string",
comment: "",
isSpecial: false,
},
{
name: "Multiline comment",
comment: "/*\nHello, world\n*/",
isSpecial: false,
},
{
name: "CGO block",
comment: `/*
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
*/`,
isSpecial: true,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if isSpecialBlock(tt.comment) != tt.isSpecial {
t.Fatalf("Wrong result")
}
})
}
}
func TestIsSpecialLine(t *testing.T) {
testCases := []struct {
name string
comment string
isSpecial bool
}{
{
name: "regular text",
comment: "// Hello, world.",
isSpecial: false,
},
{
name: "regular text without period",
comment: "// Hello, world",
isSpecial: false,
},
{
name: "code example (two spaces indentation)",
comment: "// x == y",
isSpecial: true,
},
{
name: "code example (many spaces indentation)",
comment: "// x == y",
isSpecial: true,
},
{
name: "code example (single tab indentation)",
comment: "//\tx == y",
isSpecial: true,
},
{
name: "code example (many tabs indentation)",
comment: "// \t\t\tx == y",
isSpecial: true,
},
{
name: "code example (mixed indentation)",
comment: "// \t \tx == y",
isSpecial: true,
},
{
name: "nolint tag",
comment: "// nolint: test",
isSpecial: true,
},
{
name: "nolint tag without indentation",
comment: "//nolint: test",
isSpecial: true,
},
{
name: "build tags",
comment: "// +build !linux",
isSpecial: true,
},
{
name: "build tags without indentation",
comment: "//+build !linux",
isSpecial: true,
},
{
name: "kubernetes tag",
comment: "// +k8s:deepcopy-gen=package",
isSpecial: true,
},
{
name: "cgo exported function",
comment: "//export FuncName",
isSpecial: true,
},
{
name: "cgo exported function with indentation (wrong format)",
comment: "// export FuncName",
isSpecial: false,
},
{
name: "url at the end of line",
comment: "// Read more: http://example.com/",
isSpecial: true,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if isSpecialLine(tt.comment) != tt.isSpecial {
t.Fatalf("Wrong result")
}
})
}
}
func TestHasSuffix(t *testing.T) {
testCases := []struct {
name string
text string
suffixes []string
result bool
}{
{
name: "has",
text: "hello, world.",
suffixes: []string{",", "?", "."},
result: true,
},
{
name: "has not",
text: "hello, world.",
suffixes: []string{",", "?", ":"},
result: false,
},
{
name: "has, long suffixes",
text: "hello, world.",
suffixes: []string{"x.", "m.", "d."},
result: true,
},
{
name: "has not, long suffixes",
text: "hello, world.",
suffixes: []string{"x.", "m.", "k."},
result: false,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if hasSuffix(tt.text, tt.suffixes) != tt.result {
t.Fatalf("Wrong result")
}
})
}
}
func TestByteToRuneColumn(t *testing.T) {
testCases := []struct {
name string
str string
index int
out int
}{
{
name: "ascii symbols",
str: "hello, world",
index: 5,
out: 5,
},
{
name: "cyrillic symbols at the end",
str: "hello, мир",
index: 5,
out: 5,
},
{
name: "cyrillic symbols at the beginning",
str: "привет, world",
index: 15,
out: 9,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if out := byteToRuneColumn(tt.str, tt.index); out != tt.out {
t.Fatalf("Wrong column\n expected: %d\n got: %d", tt.out, out)
}
})
}
}
func TestRuneToByteColumn(t *testing.T) {
testCases := []struct {
name string
str string
index int
out int
}{
{
name: "ascii symbols",
str: "hello, world",
index: 5,
out: 5,
},
{
name: "cyrillic symbols at the end",
str: "hello, мир",
index: 5,
out: 5,
},
{
name: "cyrillic symbols at the beginning",
str: "привет, world",
index: 9,
out: 15,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if out := runeToByteColumn(tt.str, tt.index); out != tt.out {
t.Fatalf("Wrong column\n expected: %d\n got: %d", tt.out, out)
}
})
}
}