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
|
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package doc
import (
"go/parser"
"go/token"
"reflect"
"strconv"
"strings"
"testing"
)
func TestImportGroupStarts(t *testing.T) {
for _, test := range []struct {
name string
in string
want []string // paths of group-starting imports
}{
{
name: "one group",
in: `package p
import (
"a"
"b"
"c"
"d"
)
`,
want: []string{"a"},
},
{
name: "several groups",
in: `package p
import (
"a"
"b"
"c"
"d"
)
`,
want: []string{"a", "b", "d"},
},
{
name: "extra space",
in: `package p
import (
"a"
"b"
"c"
"d"
)
`,
want: []string{"a", "b", "d"},
},
{
name: "line comment",
in: `package p
import (
"a" // comment
"b" // comment
"c"
)`,
want: []string{"a", "c"},
},
{
name: "named import",
in: `package p
import (
"a"
n "b"
m "c"
"d"
)`,
want: []string{"a", "c"},
},
{
name: "blank import",
in: `package p
import (
"a"
_ "b"
_ "c"
"d"
)`,
want: []string{"a", "b", "c"},
},
} {
t.Run(test.name, func(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", strings.NewReader(test.in), parser.ParseComments)
if err != nil {
t.Fatal(err)
}
imps := findImportGroupStarts1(file.Imports)
got := make([]string, len(imps))
for i, imp := range imps {
got[i], err = strconv.Unquote(imp.Path.Value)
if err != nil {
t.Fatal(err)
}
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("got %v, want %v", got, test.want)
}
})
}
}
|