-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain_test.go
More file actions
147 lines (129 loc) · 3.69 KB
/
main_test.go
File metadata and controls
147 lines (129 loc) · 3.69 KB
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
package main
import (
"flag"
"os"
"testing"
)
func TestSplitArgsResourceAndCommand(t *testing.T) {
os.Args = []string{"./await", "true", "--", "echo"}
flag.Parse()
res, cmd := splitArgs(flag.Args())
if len(res) != 1 || res[0] != "true" {
t.Errorf("no resources found")
}
if len(cmd) != 1 || cmd[0] != "echo" {
t.Errorf("no command found")
}
}
func TestSplitArgsResourceAndNoCommand(t *testing.T) {
os.Args = []string{"./await", "true", "--"}
flag.Parse()
res, cmd := splitArgs(flag.Args())
if len(res) != 1 || res[0] != "true" {
t.Errorf("no resources found")
}
if len(cmd) != 0 {
t.Errorf("unexpected command found")
}
os.Args = []string{"./await", "true"}
flag.Parse()
res, cmd = splitArgs(flag.Args())
if len(res) != 1 || res[0] != "true" {
t.Errorf("no resources found")
}
if len(cmd) != 0 {
t.Errorf("unexpected command found")
}
}
func TestSplitArgsNoResourceAndCommand(t *testing.T) {
os.Args = []string{"./await", "--", "echo"}
flag.Parse()
res, cmd := splitArgs(flag.Args())
if len(res) != 0 {
t.Errorf("unexpected resource found")
}
if len(cmd) != 1 || cmd[0] != "echo" {
t.Errorf("no command found")
}
}
func TestSplitArgsNoResourceNoCommand(t *testing.T) {
os.Args = []string{"./await", "--"}
flag.Parse()
res, cmd := splitArgs(flag.Args())
if len(res) != 0 {
t.Errorf("unexpected resource found")
}
if len(cmd) != 0 {
t.Errorf("unexpected command found")
}
os.Args = []string{"./await"}
flag.Parse()
res, cmd = splitArgs(flag.Args())
if len(res) != 0 {
t.Errorf("unexpected resource found")
}
if len(cmd) != 0 {
t.Errorf("unexpected command found")
}
}
func TestParseResourcesSuccess(t *testing.T) {
ress := []string{
"http://user:pass@localhost:foo/path?query=val#fragment",
"https://user:pass@localhost:foo/path?query=val#fragment",
"ws://user:pass@localhost:42/path?query=val#fragment",
"wss://user:pass@localhost:42/path?query=val#fragment",
"tcp://localhost:42",
"tcp4://localhost:42",
"tcp6://[::1]:42",
"file://relative/path/to/file",
"file://relative/path/to/file#absent",
"file:///absolute/path/to/file",
"file://absolute/path/to/file#absent",
"postgres://user:pass@localhost:5432/dbname?query=val#fragment",
"mysql://user:pass@localhost:3306/dbname?query=val#fragment",
"command",
"command with args",
"relative/path/to/command",
"relative/path/to/command with args",
"/absolute/path/to/command",
"/absolute/path/to/command with args",
"",
}
actualRess, err := parseResources(ress)
if err != nil {
t.Errorf("failed to parse ressources")
}
if len(actualRess) != len(ress) {
t.Errorf("missing parsed ressources")
}
}
func TestParseResourcesFailure(t *testing.T) {
_, err := parseResources([]string{"//foo test"})
if err == nil {
t.Errorf("expected error parsing invalid ressource")
}
}
func TestParseTags(t *testing.T) {
tests := map[string]map[string]string{
"": map[string]string{},
"=": map[string]string{}, // invalid format, should be skipped
"=ignore": map[string]string{}, // invalid format, should be skipped
"foo": map[string]string{"foo": ""},
"foo=": map[string]string{"foo": ""},
"foo=bar": map[string]string{"foo": "bar"},
"foo=bar&baz": map[string]string{"foo": "bar", "baz": ""},
"foo=bar&baz=1": map[string]string{"foo": "bar", "baz": "1"},
}
for given, expected := range tests {
actual := parseTags(given)
if len(actual) != len(expected) {
t.Errorf("unexpected parsed tags count %d != %d: %#v",
len(expected), len(actual), actual)
}
for actualK, actualV := range actual {
if expectedV, ok := expected[actualK]; !ok || actualV != expectedV {
t.Errorf("unexpected parsed tags k/v pair")
}
}
}
}