forked from multiformats/go-multiaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
142 lines (134 loc) · 3.1 KB
/
util_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
137
138
139
140
141
142
package multiaddr
import (
"strings"
"testing"
)
func TestSplitFirstLast(t *testing.T) {
ipStr := "/ip4/0.0.0.0"
tcpStr := "/tcp/123"
quicStr := "/quic"
ipfsStr := "/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7"
for _, x := range [][]string{
{ipStr, tcpStr, quicStr, ipfsStr},
{ipStr, tcpStr, ipfsStr},
{ipStr, tcpStr},
{ipStr},
} {
addr := StringCast(strings.Join(x, ""))
head, tail := SplitFirst(addr)
rest, last := SplitLast(addr)
if len(x) == 0 {
if head != nil {
t.Error("expected head to be nil")
}
if tail != nil {
t.Error("expected tail to be nil")
}
if rest != nil {
t.Error("expected rest to be nil")
}
if last != nil {
t.Error("expected last to be nil")
}
continue
}
if !head.Equal(StringCast(x[0])) {
t.Errorf("expected %s to be %s", head, x[0])
}
if !last.Equal(StringCast(x[len(x)-1])) {
t.Errorf("expected %s to be %s", head, x[len(x)-1])
}
if len(x) == 1 {
if tail != nil {
t.Error("expected tail to be nil")
}
if rest != nil {
t.Error("expected rest to be nil")
}
continue
}
tailExp := strings.Join(x[1:], "")
if !tail.Equal(StringCast(tailExp)) {
t.Errorf("expected %s to be %s", tail, tailExp)
}
restExp := strings.Join(x[:len(x)-1], "")
if !rest.Equal(StringCast(restExp)) {
t.Errorf("expected %s to be %s", rest, restExp)
}
}
c, err := NewComponent("ip4", "127.0.0.1")
if err != nil {
t.Fatal(err)
}
ci, m := SplitFirst(c)
if !ci.Equal(c) || m != nil {
t.Error("split first on component failed")
}
m, ci = SplitLast(c)
if !ci.Equal(c) || m != nil {
t.Error("split last on component failed")
}
cis := Split(c)
if len(cis) != 1 || !cis[0].Equal(c) {
t.Error("split on component failed")
}
m1, m2 := SplitFunc(c, func(c Component) bool {
return true
})
if m1 != nil || !m2.Equal(c) {
t.Error("split func(true) on component failed")
}
m1, m2 = SplitFunc(c, func(c Component) bool {
return false
})
if !m1.Equal(c) || m2 != nil {
t.Error("split func(false) on component failed")
}
i := 0
ForEach(c, func(ci Component) bool {
if i != 0 {
t.Error("expected exactly one component")
}
i++
if !ci.Equal(c) {
t.Error("foreach on component failed")
}
return true
})
}
func TestSplitFunc(t *testing.T) {
ipStr := "/ip4/0.0.0.0"
tcpStr := "/tcp/123"
quicStr := "/quic"
ipfsStr := "/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7"
for _, x := range [][]string{
{ipStr, tcpStr, quicStr, ipfsStr},
{ipStr, tcpStr, ipfsStr},
{ipStr, tcpStr},
{ipStr},
} {
addr := StringCast(strings.Join(x, ""))
for i, cs := range x {
target := StringCast(cs)
a, b := SplitFunc(addr, func(c Component) bool {
return c.Equal(target)
})
if i == 0 {
if a != nil {
t.Error("expected nil addr")
}
} else {
if !a.Equal(StringCast(strings.Join(x[:i], ""))) {
t.Error("split failed")
}
if !b.Equal(StringCast(strings.Join(x[i:], ""))) {
t.Error("split failed")
}
}
}
a, b := SplitFunc(addr, func(_ Component) bool { return false })
if !a.Equal(addr) || b != nil {
t.Error("should not have split")
}
}
}