8 func TestSplitFirstLast(t *testing.T) {
9 ipStr := "/ip4/0.0.0.0"
12 ipfsStr := "/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7"
14 for _, x := range [][]string{
15 []string{ipStr, tcpStr, quicStr, ipfsStr},
16 []string{ipStr, tcpStr, ipfsStr},
17 []string{ipStr, tcpStr},
21 addr := StringCast(strings.Join(x, ""))
22 head, tail := SplitFirst(addr)
23 rest, last := SplitLast(addr)
26 t.Error("expected head to be nil")
29 t.Error("expected tail to be nil")
32 t.Error("expected rest to be nil")
35 t.Error("expected last to be nil")
39 if !head.Equal(StringCast(x[0])) {
40 t.Errorf("expected %s to be %s", head, x[0])
42 if !last.Equal(StringCast(x[len(x)-1])) {
43 t.Errorf("expected %s to be %s", head, x[len(x)-1])
47 t.Error("expected tail to be nil")
50 t.Error("expected rest to be nil")
54 tailExp := strings.Join(x[1:], "")
55 if !tail.Equal(StringCast(tailExp)) {
56 t.Errorf("expected %s to be %s", tail, tailExp)
58 restExp := strings.Join(x[:len(x)-1], "")
59 if !rest.Equal(StringCast(restExp)) {
60 t.Errorf("expected %s to be %s", rest, restExp)
64 c, err := NewComponent("ip4", "127.0.0.1")
69 ci, m := SplitFirst(c)
70 if !ci.Equal(c) || m != nil {
71 t.Error("split first on component failed")
74 if !ci.Equal(c) || m != nil {
75 t.Error("split last on component failed")
78 if len(cis) != 1 || !cis[0].Equal(c) {
79 t.Error("split on component failed")
81 m1, m2 := SplitFunc(c, func(c Component) bool {
84 if m1 != nil || !m2.Equal(c) {
85 t.Error("split func(true) on component failed")
87 m1, m2 = SplitFunc(c, func(c Component) bool {
90 if !m1.Equal(c) || m2 != nil {
91 t.Error("split func(false) on component failed")
95 ForEach(c, func(ci Component) bool {
97 t.Error("expected exactly one component")
101 t.Error("foreach on component failed")
107 func TestSplitFunc(t *testing.T) {
108 ipStr := "/ip4/0.0.0.0"
111 ipfsStr := "/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7"
113 for _, x := range [][]string{
114 []string{ipStr, tcpStr, quicStr, ipfsStr},
115 []string{ipStr, tcpStr, ipfsStr},
116 []string{ipStr, tcpStr},
119 addr := StringCast(strings.Join(x, ""))
120 for i, cs := range x {
121 target := StringCast(cs)
122 a, b := SplitFunc(addr, func(c Component) bool {
123 return c.Equal(target)
127 t.Error("expected nil addr")
130 if !a.Equal(StringCast(strings.Join(x[:i], ""))) {
131 t.Error("split failed")
133 if !b.Equal(StringCast(strings.Join(x[i:], ""))) {
134 t.Error("split failed")
138 a, b := SplitFunc(addr, func(_ Component) bool { return false })
139 if !a.Equal(addr) || b != nil {
140 t.Error("should not have split")