1 package main
2
3 import (
4 "errors"
5 "flag"
6 "reflect"
7 "testing"
8 )
9
10 func TestParseReqParam(t *testing.T) {
11
12 testcases := []struct {
13 name string
14 expected map[string]string
15 request string
16 expectedError error
17 allowDeleteBodyV bool
18 allowMergeV bool
19 includePackageInTagsV bool
20 fileV string
21 importPathV string
22 mergeFileNameV string
23 useFQNForOpenAPINameV bool
24 openAPINamingStrategyV string
25 }{
26 {
27
28
29
30 name: "Test 0",
31 expected: map[string]string{},
32 request: "",
33 allowDeleteBodyV: false,
34 allowMergeV: false,
35 includePackageInTagsV: false,
36 fileV: "-",
37 importPathV: "",
38 mergeFileNameV: "apidocs",
39 },
40 {
41 name: "Test 1",
42 expected: map[string]string{"google/api/annotations.proto": "github.com/googleapis/googleapis/google/api"},
43 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/googleapis/googleapis/google/api",
44 allowDeleteBodyV: true,
45 allowMergeV: true,
46 includePackageInTagsV: true,
47 fileV: "./foo.pb",
48 importPathV: "/bar/baz",
49 mergeFileNameV: "apidocs",
50 },
51 {
52 name: "Test 2",
53 expected: map[string]string{"google/api/annotations.proto": "github.com/googleapis/googleapis/google/api"},
54 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/googleapis/googleapis/google/api",
55 allowDeleteBodyV: true,
56 allowMergeV: true,
57 includePackageInTagsV: true,
58 fileV: "./foo.pb",
59 importPathV: "/bar/baz",
60 mergeFileNameV: "test_name",
61 },
62 {
63 name: "Test 3",
64 expected: map[string]string{"a/b/c.proto": "github.com/x/y/z", "f/g/h.proto": "github.com/1/2/3/"},
65 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/",
66 allowDeleteBodyV: false,
67 allowMergeV: false,
68 includePackageInTagsV: false,
69 fileV: "stdin",
70 importPathV: "",
71 mergeFileNameV: "apidocs",
72 },
73 {
74 name: "Test 4",
75 expected: map[string]string{},
76 request: "",
77 allowDeleteBodyV: false,
78 allowMergeV: false,
79 includePackageInTagsV: false,
80 fileV: "stdin",
81 importPathV: "",
82 mergeFileNameV: "apidocs",
83 },
84 {
85 name: "Test 5",
86 expected: map[string]string{},
87 request: "unknown_param=17",
88 expectedError: errors.New("cannot set flag unknown_param=17: no such flag -unknown_param"),
89 allowDeleteBodyV: false,
90 allowMergeV: false,
91 includePackageInTagsV: false,
92 fileV: "stdin",
93 importPathV: "",
94 mergeFileNameV: "apidocs",
95 },
96 {
97 name: "Test 6",
98 expected: map[string]string{},
99 request: "Mfoo",
100 expectedError: errors.New("cannot set flag Mfoo: no such flag -Mfoo"),
101 allowDeleteBodyV: false,
102 allowMergeV: false,
103 includePackageInTagsV: false,
104 fileV: "stdin",
105 importPathV: "",
106 mergeFileNameV: "apidocs",
107 },
108 {
109 name: "Test 7",
110 expected: map[string]string{},
111 request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,merge_file_name",
112 allowDeleteBodyV: true,
113 allowMergeV: true,
114 includePackageInTagsV: true,
115 fileV: "",
116 importPathV: "",
117 mergeFileNameV: "",
118 },
119 {
120 name: "Test 8",
121 expected: map[string]string{},
122 request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body=3,merge_file_name",
123 expectedError: errors.New(`cannot set flag allow_repeated_fields_in_body=3: parse error`),
124 allowDeleteBodyV: true,
125 allowMergeV: true,
126 includePackageInTagsV: false,
127 fileV: "",
128 importPathV: "",
129 mergeFileNameV: "apidocs",
130 },
131 {
132 name: "Test 9",
133 expected: map[string]string{},
134 request: "include_package_in_tags=3",
135 expectedError: errors.New(`cannot set flag include_package_in_tags=3: parse error`),
136 allowDeleteBodyV: false,
137 allowMergeV: false,
138 includePackageInTagsV: false,
139 fileV: "stdin",
140 importPathV: "",
141 mergeFileNameV: "apidocs",
142 },
143 {
144 name: "Test 10",
145 expected: map[string]string{},
146 request: "fqn_for_openapi_name=3",
147 expectedError: errors.New(`cannot set flag fqn_for_openapi_name=3: parse error`),
148 allowDeleteBodyV: false,
149 allowMergeV: false,
150 includePackageInTagsV: false,
151 useFQNForOpenAPINameV: false,
152 fileV: "stdin",
153 importPathV: "",
154 mergeFileNameV: "apidocs",
155 },
156 {
157 name: "Test 11",
158 expected: map[string]string{},
159 request: "fqn_for_openapi_name=true",
160 allowDeleteBodyV: false,
161 allowMergeV: false,
162 includePackageInTagsV: false,
163 useFQNForOpenAPINameV: true,
164 fileV: "stdin",
165 importPathV: "",
166 mergeFileNameV: "apidocs",
167 },
168 {
169 name: "Test 12",
170 expected: map[string]string{},
171 request: "openapi_naming_strategy=simple",
172 allowDeleteBodyV: false,
173 allowMergeV: false,
174 includePackageInTagsV: false,
175 useFQNForOpenAPINameV: false,
176 openAPINamingStrategyV: "simple",
177 fileV: "stdin",
178 importPathV: "",
179 mergeFileNameV: "apidocs",
180 },
181 }
182
183 for i, tc := range testcases {
184 t.Run(tc.name, func(tt *testing.T) {
185 f := flag.CommandLine
186 pkgMap := make(map[string]string)
187 err := parseReqParam(tc.request, f, pkgMap)
188 if tc.expectedError == nil {
189 if err != nil {
190 tt.Errorf("unexpected parse error '%v'", err)
191 }
192 if !reflect.DeepEqual(pkgMap, tc.expected) {
193 tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
194 }
195 } else {
196 if err == nil {
197 tt.Error("expected parse error not returned")
198 }
199 if !reflect.DeepEqual(pkgMap, tc.expected) {
200 tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
201 }
202 if err.Error() != tc.expectedError.Error() {
203 tt.Errorf("expected error malformed, expected %q, got %q", tc.expectedError.Error(), err.Error())
204 }
205 }
206 checkFlags(tc.allowDeleteBodyV, tc.allowMergeV, tc.includePackageInTagsV, tc.useFQNForOpenAPINameV, tc.openAPINamingStrategyV, tc.fileV, tc.importPathV, tc.mergeFileNameV, tt, i)
207
208 clearFlags()
209 })
210 }
211 }
212
213 func checkFlags(
214 allowDeleteV,
215 allowMergeV,
216 includePackageInTagsV bool,
217 useFQNForOpenAPINameV bool,
218 openAPINamingStrategyV,
219 fileV,
220 importPathV,
221 mergeFileNameV string,
222 t *testing.T,
223 tid int,
224 ) {
225 if *importPrefix != importPathV {
226 t.Errorf("Test %v: import_prefix misparsed, expected '%v', got '%v'", tid, importPathV, *importPrefix)
227 }
228 if *file != fileV {
229 t.Errorf("Test %v: file misparsed, expected '%v', got '%v'", tid, fileV, *file)
230 }
231 if *allowDeleteBody != allowDeleteV {
232 t.Errorf("Test %v: allow_delete_body misparsed, expected '%v', got '%v'", tid, allowDeleteV, *allowDeleteBody)
233 }
234 if *allowMerge != allowMergeV {
235 t.Errorf("Test %v: allow_merge misparsed, expected '%v', got '%v'", tid, allowMergeV, *allowMerge)
236 }
237 if *mergeFileName != mergeFileNameV {
238 t.Errorf("Test %v: merge_file_name misparsed, expected '%v', got '%v'", tid, mergeFileNameV, *mergeFileName)
239 }
240 if *includePackageInTags != includePackageInTagsV {
241 t.Errorf("Test %v: include_package_in_tags misparsed, expected '%v', got '%v'", tid, includePackageInTagsV, *includePackageInTags)
242 }
243 if *useFQNForOpenAPIName != useFQNForOpenAPINameV {
244 t.Errorf("Test %v: fqn_for_openapi_name misparsed, expected '%v', got '%v'", tid, useFQNForOpenAPINameV, *useFQNForOpenAPIName)
245 }
246 if *openAPINamingStrategy != openAPINamingStrategyV {
247 t.Errorf("Test %v: openapi_naming_strategy misparsed, expected '%v', got '%v'", tid, openAPINamingStrategyV, *openAPINamingStrategy)
248 }
249 }
250
251 func clearFlags() {
252 *importPrefix = ""
253 *file = "stdin"
254 *allowDeleteBody = false
255 *allowMerge = false
256 *includePackageInTags = false
257 *mergeFileName = "apidocs"
258 *useFQNForOpenAPIName = false
259 *openAPINamingStrategy = ""
260 }
261
View as plain text