-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathg_docs_test.go
198 lines (186 loc) · 4.44 KB
/
g_docs_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
package main
import (
"errors"
"go/ast"
"go/parser"
"go/token"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConsumes(t *testing.T) {
tests := []struct {
desc string
a string
expected []string
}{
{
desc: "accept json, returns json",
a: "json",
expected: []string{ajson},
},
{
desc: "accept xml, returns xml",
a: "xml",
expected: []string{axml},
},
{
desc: "accept plain, returns plain",
a: "plain",
expected: []string{aplain},
},
{
desc: "accept html, returns html",
a: "html",
expected: []string{ahtml},
},
{
desc: "accept thrift_binary, returns thrift binary",
a: "thrift_binary",
expected: []string{content_type_thrift_binary},
},
{
desc: "accept thrift_json, returns thrift json",
a: "thrift_json",
expected: []string{content_type_thrift_json},
},
{
desc: "accept thrift_webcontent_binary, returns thrift web content binary",
a: "thrift_webcontent_binary",
expected: []string{content_type_thrift_binary_webcontent_v1},
},
{
desc: "accept thrift_webcontent_json, returns thrift web content json",
a: "thrift_webcontent_json",
expected: []string{content_type_thrift_json_webcontent_v1},
},
{
desc: "accept multipart, returns multipart/form-data",
a: "form",
expected: []string{contentTypeMultipartFormData},
},
{
desc: "unknown accept, returns empty slice",
a: "skippy-chunky",
expected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
actual := consumes(tt.a)
assert.Equal(t, tt.expected, actual)
})
}
}
func TestGetGoFilesInPackage(t *testing.T) {
tests := []struct {
desc string
pkg string
mockGetwd func() (dir string, err error)
expected map[string]*ast.Package
isError assert.ErrorAssertionFunc
}{
{
desc: "Test package is not ignored, return list of parsed go files",
pkg: "testdata/router",
mockGetwd: func() (string, error) {
return "/a/test/root/package", nil
},
expected: func() map[string]*ast.Package {
pkg := "testdata/router"
pkgs, err := parser.
ParseDir(token.NewFileSet(), pkg, func(info os.FileInfo) bool {
return true
}, parser.ParseComments)
assert.NoError(t, err)
return pkgs
}(),
isError: assert.NoError,
},
{
desc: "Test package is ignored, returns nil without error",
pkg: "/a/test/root/package/handlers",
mockGetwd: func() (string, error) {
return "/a/test/root/package", nil
},
expected: nil,
isError: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
getwd = tt.mockGetwd
actual, err := getGoFilesInPackage(tt.pkg)
assert.Equal(t, tt.expected, actual)
tt.isError(t, err)
})
}
}
func TestIsPackageIgnored(t *testing.T) {
tests := []struct {
desc string
pkg string
mockGetwd func() (dir string, err error)
expected bool
}{
{
desc: "Test Getwd returns error, returns false",
pkg: "",
mockGetwd: func() (string, error) {
return "", errors.New("error getting current dir")
},
expected: false,
},
{
desc: "Test package is not ignored, returns false",
pkg: "/a/test/root/package/notignored",
mockGetwd: func() (string, error) {
return "/a/test/root/package", nil
},
expected: false,
},
{
desc: "Test package is ignored, returns true",
pkg: "/a/test/root/package/handlers",
mockGetwd: func() (string, error) {
return "/a/test/root/package", nil
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
getwd = tt.mockGetwd
actual := isPackageIgnored(tt.pkg)
assert.Equal(t, tt.expected, actual)
})
}
}
func TestGetProjectFromImportPath(t *testing.T) {
tests := []struct {
desc string
path string
expected string
isError assert.ErrorAssertionFunc
}{
{
desc: "Input path is not an assumed path returns error",
path: "go.uber.org/zap",
expected: "",
isError: assert.Error,
},
{
desc: "Input path is an assumed path returns the project name",
path: "github.com/uber/zap",
expected: "zap",
isError: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
actual, err := getProjectFromImportPath(tt.path)
assert.Equal(t, tt.expected, actual)
tt.isError(t, err)
})
}
}