This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main_test.go
136 lines (123 loc) · 4.96 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
package main
import (
"flag"
"reflect"
"testing"
)
func TestParseReqParam(t *testing.T) {
f := flag.CommandLine
// this one must be first - with no leading clearFlags call it
// verifies our expectation of default values as we reset by
// clearFlags
pkgMap := make(map[string]string)
expected := map[string]string{}
err := parseReqParam("", f, pkgMap)
if err != nil {
t.Errorf("Test 0: unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 0: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(false, "stdin", "", false, "apidocs", t, 0)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"}
err = parseReqParam("allow_delete_body,allow_merge,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api", f, pkgMap)
if err != nil {
t.Errorf("Test 1: unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 1: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(true, "./foo.pb", "/bar/baz", true, "apidocs", t, 1)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{"google/api/annotations.proto": "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"}
err = parseReqParam("allow_delete_body=true,allow_merge=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", f, pkgMap)
if err != nil {
t.Errorf("Test 2: unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 2: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(true, "./foo.pb", "/bar/baz", true, "test_name", t, 2)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{"a/b/c.proto": "github.com/x/y/z", "f/g/h.proto": "github.com/1/2/3/"}
err = parseReqParam("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/", f, pkgMap)
if err != nil {
t.Errorf("Test 3: unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 3: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(false, "stdin", "", false, "apidocs", t, 3)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{}
err = parseReqParam("", f, pkgMap)
if err != nil {
t.Errorf("Test 4: unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 4: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(false, "stdin", "", false, "apidocs", t, 4)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{}
err = parseReqParam("unknown_param=17", f, pkgMap)
if err == nil {
t.Error("Test 5: expected parse error not returned")
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 5: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(false, "stdin", "", false, "apidocs", t, 5)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{}
err = parseReqParam("Mfoo", f, pkgMap)
if err == nil {
t.Error("Test 6: expected parse error not returned")
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 6: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(false, "stdin", "", false, "apidocs", t, 6)
clearFlags()
pkgMap = make(map[string]string)
expected = map[string]string{}
err = parseReqParam("allow_delete_body,allow_merge,file,import_prefix,merge_file_name", f, pkgMap)
if err != nil {
t.Errorf("Test 7: unexpected parse error '%v'", err)
}
if !reflect.DeepEqual(pkgMap, expected) {
t.Errorf("Test 7: pkgMap parse error, expected '%v', got '%v'", expected, pkgMap)
}
checkFlags(true, "", "", true, "", t, 7)
}
func checkFlags(allowDeleteV bool, fileV, importPathV string, allowMergeV bool, 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)
}
}
func clearFlags() {
*importPrefix = ""
*file = "stdin"
*allowDeleteBody = false
*allowMerge = false
*mergeFileName= "apidocs"
}