-
Notifications
You must be signed in to change notification settings - Fork 7
/
getters_test.go
265 lines (255 loc) · 5.79 KB
/
getters_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
package godot
import (
"errors"
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"regexp"
"strings"
"testing"
)
func TestGetComments(t *testing.T) {
testFile := filepath.Join("testdata", "get", "main.go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}
pf, err := newParsedFile(file, fset)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}
testCases := []struct {
name string
scope Scope
contains []string
}{
{
name: "scope: decl",
scope: DeclScope,
contains: []string{"[DECL]"},
},
{
name: "scope: top",
scope: TopLevelScope,
contains: []string{"[DECL]", "[TOP]"},
},
{
name: "scope: all",
scope: AllScope,
contains: []string{"[DECL]", "[TOP]", "[ALL]"},
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
comments := pf.getComments(tt.scope, nil)
var expected int
for _, c := range comments {
if linesContain(c.lines, "[NONE]") {
continue
}
for _, s := range tt.contains {
if strings.Contains(c.text, s) {
expected++
break
}
}
}
if len(comments) != expected {
t.Fatalf(
"Got wrong number of comments:\n expected: %d\n got: %d",
expected, len(comments),
)
}
})
}
t.Run("try to get comments from cgo generated file", func(t *testing.T) {
testFile := filepath.Join("testdata", "get", "cgo.go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}
pf, err := newParsedFile(file, fset)
if pf != nil {
t.Fatalf("Unexpected file content")
}
if !errors.Is(err, errUnsuitableInput) {
t.Fatalf(
"Unexpected error:\n expected: %v\n got: %v",
errUnsuitableInput, err,
)
}
})
}
func TestGetText(t *testing.T) {
testCases := []struct {
name string
comment *ast.CommentGroup
text string
exclude *regexp.Regexp
}{
{
name: "regular text",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "// Hello, world"},
}},
text: " Hello, world",
},
{
name: "regular text without indentation",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "//Hello, world"},
}},
text: "Hello, world",
},
{
name: "empty singleline comment",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "//"},
}},
text: "",
},
{
name: "empty multiline comment",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "/**/"},
}},
text: "",
},
{
name: "regular text in multiline block",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "/*\nHello, world\n*/"},
}},
text: "\nHello, world\n",
},
{
name: "block of singleline comments with regular text",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "// One"},
{Text: "// Two"},
{Text: "// Three"},
}},
text: " One\n Two\n Three",
},
{
name: "block of singleline comments with empty and special lines",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "// One"},
{Text: "//"},
{Text: "// fmt.Println(s)"},
{Text: "// Two"},
{Text: "// #nosec"},
{Text: "// Three"},
{Text: "// +k8s:deepcopy-gen=package"},
{Text: "// +nolint: gosec"},
}},
text: " One\n" +
"\n" +
"<godotSpecialReplacer>\n" +
" Two\n" +
"<godotSpecialReplacer>\n" +
" Three\n" +
"<godotSpecialReplacer>\n" +
"<godotSpecialReplacer>",
},
{
name: "block of singleline comments with a url at the end",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "// Read more"},
{Text: "// http://example.com"},
}},
text: " Read more\n<godotSpecialReplacer>",
},
{
name: "cgo block",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "/*\n" +
"#include <stdio.h>\n" +
"#include <stdlib.h>\n" +
"void myprint(char* s) {\n" +
"\tprintf(s);\n" +
"}\n" +
"*/"},
}},
text: "",
},
{
name: "multiline block with a code example",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "/*\n" +
"Example:\n" +
"\tn := rand.Int()\n" +
"\tfmt.Println(n)\n" +
"*/"},
}},
text: "\n" +
"Example:\n" +
"<godotSpecialReplacer>\n" +
"<godotSpecialReplacer>\n" +
"",
},
{
name: "empty comment group",
comment: &ast.CommentGroup{List: []*ast.Comment{}},
text: "",
},
{
name: "single excluded line",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "// Hello, world."},
}},
text: "<godotSpecialReplacer>",
exclude: regexp.MustCompile("Hello"),
},
{
name: "excluded line in the middle",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "/*\n" +
"Read more:\n" +
"@intenal.link\n" +
"Thanks." +
"*/"},
}},
text: "\n" +
"Read more:\n" +
"<godotSpecialReplacer>\n" +
"Thanks." +
"",
exclude: regexp.MustCompile("^@.+"),
},
{
name: "excluded line at the end",
comment: &ast.CommentGroup{List: []*ast.Comment{
{Text: "/* Read more:\n" +
"@intenal.link */"},
}},
text: " Read more:\n" +
"<godotSpecialReplacer>",
exclude: regexp.MustCompile("^@.+"),
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var re []*regexp.Regexp
if tt.exclude != nil {
re = []*regexp.Regexp{tt.exclude}
}
if text := getText(tt.comment, re); text != tt.text {
t.Fatalf("Wrong text\n expected: '%s'\n got: '%s'", tt.text, text)
}
})
}
}
func linesContain(lines []string, s string) bool {
for _, ln := range lines {
if strings.Contains(ln, s) {
return true
}
}
return false
}