This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods_test.go
116 lines (106 loc) · 2.15 KB
/
methods_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
package nit_test
import (
"go/ast"
"go/token"
"testing"
"github.com/MarioCarrion/nit"
)
//nolint:dupl
func TestMethodsValidator_Validate(t *testing.T) {
tests := [...]struct {
name string
filename string
expectedError bool
}{
{
"OK",
"methods_valid.go",
false,
},
{
"OK: sorted",
"methods_sorted.go",
false,
},
{
"OK: sorted type",
"methods_sorted_type_ok.go",
false,
},
{
"OK: sorted exported/unexported",
"methods_sorted_unexported_ok.go",
false,
},
{
"Error: not defined in file",
"methods_not_defined.go",
true,
},
{
"Error: sorted",
"methods_sorted_error.go",
true,
},
{
"Error: sorted type",
"methods_sorted_type_error.go",
true,
},
{
"Error: sorted type comments",
"methods_sorted_type_error1.go",
true,
},
}
t.Run("no types found", func(ts *testing.T) {
_, err := nit.NewMethodsValidator(nil, nil)
if err == nil {
ts.Fatalf("expected error, got nil")
}
})
for _, tt := range tests {
t.Run(tt.name, func(ts *testing.T) {
var (
err error
f *ast.File
fset *token.FileSet
tvalidator *nit.TypesValidator
validator *nit.MethodsValidator
)
f, fset = newParserFile(ts, tt.filename)
comments := nit.NewBreakComments(fset, f.Comments)
for _, s := range f.Decls {
switch g := s.(type) {
case *ast.GenDecl:
section, err1 := nit.NewGenDeclFileSection(g)
if err1 != nil {
ts.Fatalf("expected no error, got %s", err1)
}
if section == nit.FileSectionTypes {
tvalidator = nit.NewTypesValidator(comments)
if err1 := tvalidator.Validate(g, fset); err1 != nil {
ts.Fatalf("expected no error, got %s", err1)
}
}
case *ast.FuncDecl:
if g.Recv == nil {
continue
}
if validator == nil {
validator, err = nit.NewMethodsValidator(comments, tvalidator)
if err != nil {
break
}
}
if err = validator.Validate(g, fset); err != nil {
break
}
}
}
if tt.expectedError != (err != nil) {
ts.Fatalf("expected error %t, got %s", tt.expectedError, err)
}
})
}
}