mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-02-15 07:30:07 +08:00
57 lines
937 B
Go
57 lines
937 B
Go
package test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
)
|
|
|
|
func AssertNilError(t *testing.T, err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func ExpectError(t *testing.T, err error, msg string) {
|
|
t.Helper()
|
|
if err == nil {
|
|
t.Error(msg)
|
|
}
|
|
}
|
|
|
|
func GenerateTestAddrs(n int) []ma.Multiaddr {
|
|
out := make([]ma.Multiaddr, n)
|
|
for i := 0; i < n; i++ {
|
|
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/1.2.3.4/tcp/%d", i))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
out[i] = a
|
|
}
|
|
return out
|
|
}
|
|
|
|
func AssertAddressesEqual(t *testing.T, exp, act []ma.Multiaddr) {
|
|
t.Helper()
|
|
if len(exp) != len(act) {
|
|
t.Fatalf("lengths not the same. expected %d, got %d\n", len(exp), len(act))
|
|
}
|
|
|
|
for _, a := range exp {
|
|
found := false
|
|
|
|
for _, b := range act {
|
|
if a.Equal(b) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
t.Fatalf("expected address %s not found", a)
|
|
}
|
|
}
|
|
}
|