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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
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
includePackageInTagsV bool
fileV string
importPathV string
mergeFileNameV string
useFQNForOpenAPINameV bool
openAPINamingStrategyV string
}{
{
// 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,
includePackageInTagsV: false,
fileV: "-",
importPathV: "",
mergeFileNameV: "apidocs",
},
{
name: "Test 1",
expected: map[string]string{"google/api/annotations.proto": "github.com/googleapis/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/googleapis/googleapis/google/api",
allowDeleteBodyV: true,
allowMergeV: true,
includePackageInTagsV: true,
fileV: "./foo.pb",
importPathV: "/bar/baz",
mergeFileNameV: "apidocs",
},
{
name: "Test 2",
expected: map[string]string{"google/api/annotations.proto": "github.com/googleapis/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/googleapis/googleapis/google/api",
allowDeleteBodyV: true,
allowMergeV: 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,
includePackageInTagsV: false,
fileV: "stdin",
importPathV: "",
mergeFileNameV: "apidocs",
},
{
name: "Test 4",
expected: map[string]string{},
request: "",
allowDeleteBodyV: false,
allowMergeV: 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,
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,
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,
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,
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,
includePackageInTagsV: false,
fileV: "stdin",
importPathV: "",
mergeFileNameV: "apidocs",
},
{
name: "Test 10",
expected: map[string]string{},
request: "fqn_for_openapi_name=3",
expectedError: errors.New(`cannot set flag fqn_for_openapi_name=3: parse error`),
allowDeleteBodyV: false,
allowMergeV: false,
includePackageInTagsV: false,
useFQNForOpenAPINameV: false,
fileV: "stdin",
importPathV: "",
mergeFileNameV: "apidocs",
},
{
name: "Test 11",
expected: map[string]string{},
request: "fqn_for_openapi_name=true",
allowDeleteBodyV: false,
allowMergeV: false,
includePackageInTagsV: false,
useFQNForOpenAPINameV: true,
fileV: "stdin",
importPathV: "",
mergeFileNameV: "apidocs",
},
{
name: "Test 12",
expected: map[string]string{},
request: "openapi_naming_strategy=simple",
allowDeleteBodyV: false,
allowMergeV: false,
includePackageInTagsV: false,
useFQNForOpenAPINameV: false,
openAPINamingStrategyV: "simple",
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.includePackageInTagsV, tc.useFQNForOpenAPINameV, tc.openAPINamingStrategyV, tc.fileV, tc.importPathV, tc.mergeFileNameV, tt, i)
clearFlags()
})
}
}
func checkFlags(
allowDeleteV,
allowMergeV,
includePackageInTagsV bool,
useFQNForOpenAPINameV bool,
openAPINamingStrategyV,
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 *includePackageInTags != includePackageInTagsV {
t.Errorf("Test %v: include_package_in_tags misparsed, expected '%v', got '%v'", tid, includePackageInTagsV, *includePackageInTags)
}
if *useFQNForOpenAPIName != useFQNForOpenAPINameV {
t.Errorf("Test %v: fqn_for_openapi_name misparsed, expected '%v', got '%v'", tid, useFQNForOpenAPINameV, *useFQNForOpenAPIName)
}
if *openAPINamingStrategy != openAPINamingStrategyV {
t.Errorf("Test %v: openapi_naming_strategy misparsed, expected '%v', got '%v'", tid, openAPINamingStrategyV, *openAPINamingStrategy)
}
}
func clearFlags() {
*importPrefix = ""
*file = "stdin"
*allowDeleteBody = false
*allowMerge = false
*includePackageInTags = false
*mergeFileName = "apidocs"
*useFQNForOpenAPIName = false
*openAPINamingStrategy = ""
}
|