-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
86 lines (79 loc) · 1.52 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
package main
import "testing"
func TestParseAddressPositive(t *testing.T) {
testsPositive := []struct {
address string
want string
}{
{
"tcp://localhost:8080",
"tcp://localhost:8080",
},
{
"unix://var/lib/socket.sock",
"unix://var/lib/socket.sock",
},
{
"tcp4://user:pass@127.0.0.1:9000",
"tcp4://user:pass@127.0.0.1:9000",
},
{
"user:pass@127.0.0.1:8123",
"tcp://user:pass@127.0.0.1:8123",
},
{
"udp6://srv1525:3306",
"udp6://srv1525:3306",
},
{
"localhost:8080",
"tcp://localhost:8080",
},
{
":8080",
"tcp://localhost:8080",
},
}
for _, tt := range testsPositive {
t.Run(tt.address, func(t *testing.T) {
addr, err := parseAddress(tt.address)
if err != nil {
t.Errorf("want: 'no err', got err: '%v'", err)
}
got := addr.String()
if tt.want != got {
t.Errorf("want: '%s', got: '%s'", tt.want, got)
}
})
}
}
func TestParseAddressNegative(t *testing.T) {
testsNegative := []struct {
address string
want string
}{
{
"",
"address cannot be empty",
},
{
"tcp://localhost://localhost",
"the address cannot contain more than one '://'",
},
{
"://localhost",
"network not specified",
},
}
for _, tt := range testsNegative {
t.Run(tt.address, func(t *testing.T) {
addr, err := parseAddress(tt.address)
if err == nil {
got := addr.String()
t.Errorf("want: 'err', got: '%s'", got)
} else if tt.want != err.Error() {
t.Errorf("want: '%s', got: '%s'", tt.want, err.Error())
}
})
}
}