Repository navigation
Expand file tree
/
Copy pathcomment_wrap_test.go
More file actions
200 lines (185 loc) · 4.97 KB
/
Copy pathcomment_wrap_test.go
File metadata and controls
200 lines (185 loc) · 4.97 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
package main
import (
"go/parser"
"go/token"
"testing"
)
func Test_processComments(t *testing.T) {
type args struct {
src string
maxCommentLength int
}
tests := []struct {
name string
args args
wantComments [][]string
}{
{name: "single line long block comment",
args: args{
src: `
package main
/* i am longer than 5 characters */
func main() {
println("Hello, World!")
}
`,
maxCommentLength: 5,
},
wantComments: [][]string{
{
// This comment be ignored (it's a block comment)
"/* i am longer than 5 characters */",
},
},
},
{name: "second line long block comment",
args: args{
src: `
package main
/* short
i am longer than 10 characters */
func main() {
println("Hello, World!")
}
`,
maxCommentLength: 10,
},
wantComments: [][]string{
{
// This comment be ignored (it's a block comment)
`/* short
i am longer than 10 characters */`,
},
},
},
{name: "monolithic comment with separate wrappable group",
args: args{
src: `
package main
//1234567891011121314
func main() {
// i should still be fixed
println("Hello, World!")
}
`,
maxCommentLength: 12,
},
wantComments: [][]string{
{
// text in a long conglomerate string generally hints towards a
// diagram that shouldn't be formatted
"//1234567891011121314",
},
{
"// i should",
"// still be fixed\n",
},
},
},
{name: "comment with another comment to wrap to",
args: args{
src: `
package main
// i am long long long long long
// i am short
func main() {
println("Hello, World!")
}
`,
maxCommentLength: 25,
},
wantComments: [][]string{
{
// Comment should wrap
"// i am long long long",
"// long long i am short",
},
},
},
{name: "single line comment with no beginning space",
args: args{
src: `
package main
//i am a long comment with no beginning space
func main() {
println("Hello, World!")
}
`,
maxCommentLength: 25,
},
wantComments: [][]string{
{
// This comment be ignored (it's a block comment)
"//i am a long comment",
"//with no beginning space\n",
},
},
},
{name: "large comment group (ignored)",
args: args{
src: `
package main
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
//i am a long comment with no beginning space
func main() {
println("Hello, World!")
}
`,
maxCommentLength: 25,
},
wantComments: [][]string{
{
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
"//i am a long comment with no beginning space",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", tt.args.src, parser.ParseComments)
if err != nil {
t.Fatalf("Did not expect error parsing file, %v", err)
}
processComments(fset, f.Comments, tt.args.maxCommentLength, false)
if len(tt.wantComments) != len(f.Comments) {
t.Fatalf("Expected group length %v, got %v", len(tt.wantComments), len(f.Comments))
}
for j, commentGroup := range f.Comments {
if len(tt.wantComments[j]) != len(commentGroup.List) {
t.Fatalf("Expected comment group length %v, got %v", len(tt.wantComments[j]), len(commentGroup.List))
}
for i, actual := range commentGroup.List {
if tt.wantComments[j][i] != actual.Text {
t.Errorf("Mismatched comments.\nwant %v\n got %v\n", tt.wantComments[j][i], actual.Text)
}
}
}
})
}
}