-
Notifications
You must be signed in to change notification settings - Fork 8
/
main_test.go
105 lines (92 loc) · 2.03 KB
/
main_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
package funlen
import (
"go/parser"
"go/token"
"strings"
"testing"
)
func TestRunTable(t *testing.T) {
testcases := map[string]struct {
input string
expected string
lineLimit int
stmtLimit int
}{
"too-many-statements": {
input: `package main
func main() {
print("Hello, world!")
print("Hello, world!")}`,
expected: "Function 'main' has too many statements (2 > 1)",
lineLimit: 1,
stmtLimit: 1,
},
"too-many-lines": {
input: `package main
import "fmt"
func main() {
print("main!")
print("is!")
print("too!")
print("long")}`,
expected: "Function 'main' is too long (3 > 1)",
lineLimit: 1,
stmtLimit: 10,
},
"too-many-statements-inline-func": {
input: `package main
func main() {
print("Hello, world!")
if true {
y := []int{1,2,3,4}
for k, v := range y {
f := func() { print("test") }
f()
}
}
print("Hello, world!")}`,
expected: "Function 'main' has too many statements (8 > 1)",
lineLimit: 1,
stmtLimit: 1,
},
}
for name, test := range testcases {
t.Run(name, func(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", test.input, parser.ParseComments)
if err != nil {
t.Error("\nActual: ", err, "\n Did not expected error")
}
r := Run(f, fset, test.lineLimit, test.stmtLimit, false)
actual := r[0].Message
if !strings.Contains(actual, test.expected) {
t.Error("\nActual: ", actual, "\nExpected: ", test.expected)
}
})
}
}
func TestRunIgnoresComments(t *testing.T) {
input := `package main
func main() {
// Comment 1
// Comment 2
// Comment 3
print("Hello, world!")}
// Comment Doc
func unittest() {
// Comment 1
// Comment 2
print("Hello, world!")}
// Comment 3`
lineLimit := 2
stmtLimit := 2
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", input, parser.ParseComments)
if err != nil {
t.Error("\nActual: ", err, "\n Did not expected error")
}
r := Run(f, fset, lineLimit, stmtLimit, true)
if len(r) > 0 {
t.Error("\nActual: ", r, "\nExpected no lint errors")
}
}