-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_test.go
186 lines (178 loc) · 8.31 KB
/
main_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
package main
import (
"errors"
"flag"
"reflect"
"testing"
)
func TestParseReqParam(t *testing.T) {
testcases := []struct {
name string
expected map[string]string
request string
expectedError error
allowDeleteBodyV bool
allowMergeV bool
allowRepeatedFieldsInBodyV bool
includePackageInTagsV bool
fileV string
importPathV string
mergeFileNameV string
useFQNForSwaggerNameV bool
}{
{
// this one must be first - with no leading clearFlags call it
// verifies our expectation of default values as we reset by
// clearFlags
name: "Test 0",
expected: map[string]string{},
request: "",
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "-", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 1",
expected: map[string]string{"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"},
request: "allow_delete_body,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api",
allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
fileV: "./foo.pb", importPathV: "/bar/baz", mergeFileNameV: "apidocs",
},
{
name: "Test 2",
expected: map[string]string{"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"},
request: "allow_delete_body=true,allow_merge=true,allow_repeated_fields_in_body=true,include_package_in_tags=true,merge_file_name=test_name,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api",
allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
fileV: "./foo.pb", importPathV: "/bar/baz", mergeFileNameV: "test_name",
},
{
name: "Test 3",
expected: map[string]string{"a/b/c.proto": "github.com/x/y/z", "f/g/h.proto": "github.com/1/2/3/"},
request: "allow_delete_body=false,allow_merge=false,Ma/b/c.proto=github.com/x/y/z,Mf/g/h.proto=github.com/1/2/3/",
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 4",
expected: map[string]string{},
request: "",
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 5",
expected: map[string]string{},
request: "unknown_param=17",
expectedError: errors.New("Cannot set flag unknown_param=17: no such flag -unknown_param"),
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 6",
expected: map[string]string{},
request: "Mfoo",
expectedError: errors.New("Cannot set flag Mfoo: no such flag -Mfoo"),
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 7",
expected: map[string]string{},
request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,merge_file_name",
allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
fileV: "", importPathV: "", mergeFileNameV: "",
},
{
name: "Test 8",
expected: map[string]string{},
request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body=3,merge_file_name",
expectedError: errors.New(`Cannot set flag allow_repeated_fields_in_body=3: parse error`),
allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 9",
expected: map[string]string{},
request: "include_package_in_tags=3",
expectedError: errors.New(`Cannot set flag include_package_in_tags=3: parse error`),
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 10",
expected: map[string]string{},
request: "fqn_for_swagger_name=3",
expectedError: errors.New(`Cannot set flag fqn_for_swagger_name=3: parse error`),
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false, useFQNForSwaggerNameV: false,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
{
name: "Test 11",
expected: map[string]string{},
request: "fqn_for_swagger_name=true",
allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false, useFQNForSwaggerNameV: true,
fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
},
}
for i, tc := range testcases {
t.Run(tc.name, func(tt *testing.T) {
f := flag.CommandLine
pkgMap := make(map[string]string)
err := parseReqParam(tc.request, f, pkgMap)
if tc.expectedError == nil {
if err != nil {
tt.Errorf("unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, tc.expected) {
tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
}
} else {
if err == nil {
tt.Error("expected parse error not returned")
}
if !reflect.DeepEqual(pkgMap, tc.expected) {
tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
}
if err.Error() != tc.expectedError.Error() {
tt.Errorf("expected error malformed, expected %q, got %q", tc.expectedError.Error(), err.Error())
}
}
checkFlags(tc.allowDeleteBodyV, tc.allowMergeV, tc.allowRepeatedFieldsInBodyV, tc.includePackageInTagsV, tc.useFQNForSwaggerNameV, tc.fileV, tc.importPathV, tc.mergeFileNameV, tt, i)
clearFlags()
})
}
}
func checkFlags(allowDeleteV, allowMergeV, allowRepeatedFieldsInBodyV, includePackageInTagsV bool, useFQNForSwaggerNameV bool, fileV, importPathV, mergeFileNameV string, t *testing.T, tid int) {
if *importPrefix != importPathV {
t.Errorf("Test %v: import_prefix misparsed, expected '%v', got '%v'", tid, importPathV, *importPrefix)
}
if *file != fileV {
t.Errorf("Test %v: file misparsed, expected '%v', got '%v'", tid, fileV, *file)
}
if *allowDeleteBody != allowDeleteV {
t.Errorf("Test %v: allow_delete_body misparsed, expected '%v', got '%v'", tid, allowDeleteV, *allowDeleteBody)
}
if *allowMerge != allowMergeV {
t.Errorf("Test %v: allow_merge misparsed, expected '%v', got '%v'", tid, allowMergeV, *allowMerge)
}
if *mergeFileName != mergeFileNameV {
t.Errorf("Test %v: merge_file_name misparsed, expected '%v', got '%v'", tid, mergeFileNameV, *mergeFileName)
}
if *allowRepeatedFieldsInBody != allowRepeatedFieldsInBodyV {
t.Errorf("Test %v: allow_repeated_fields_in_body misparsed, expected '%v', got '%v'", tid, allowRepeatedFieldsInBodyV, *allowRepeatedFieldsInBody)
}
if *includePackageInTags != includePackageInTagsV {
t.Errorf("Test %v: include_package_in_tags misparsed, expected '%v', got '%v'", tid, includePackageInTagsV, *includePackageInTags)
}
if *useFQNForSwaggerName != useFQNForSwaggerNameV {
t.Errorf("Test %v: fqn_for_swagger_name misparsed, expected '%v', got '%v'", tid, useFQNForSwaggerNameV, *useFQNForSwaggerName)
}
}
func clearFlags() {
*importPrefix = ""
*file = "stdin"
*allowDeleteBody = false
*allowMerge = false
*allowRepeatedFieldsInBody = false
*includePackageInTags = false
*mergeFileName = "apidocs"
}