channeldb/addr_test: inspect error string

Lets us match on more than static errors.
This commit is contained in:
Johan T. Halseth 2019-01-17 14:11:56 +01:00
parent 6ae55168b8
commit 6f605b2de2
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -3,6 +3,7 @@ package channeldb
import (
"bytes"
"net"
"strings"
"testing"
"github.com/lightningnetwork/lnd/tor"
@ -15,7 +16,7 @@ func (t unknownAddrType) String() string { return "unknown" }
var addrTests = []struct {
expAddr net.Addr
serErr error
serErr string
}{
{
expAddr: &net.TCPAddr{
@ -43,7 +44,7 @@ var addrTests = []struct {
},
{
expAddr: unknownAddrType{},
serErr: ErrUnknownAddressType,
serErr: ErrUnknownAddressType.Error(),
},
}
@ -55,11 +56,21 @@ func TestAddrSerialization(t *testing.T) {
var b bytes.Buffer
for _, test := range addrTests {
err := serializeAddr(&b, test.expAddr)
if err != test.serErr {
switch {
case err == nil && test.serErr != "":
t.Fatalf("expected serialization err for addr %v",
test.expAddr)
case err != nil && test.serErr == "":
t.Fatalf("unexpected serialization err for addr %v: %v",
test.expAddr, err)
case err != nil && !strings.Contains(err.Error(), test.serErr):
t.Fatalf("unexpected serialization err for addr %v, "+
"want: %v, got %v",
test.expAddr, test.serErr, err)
} else if test.serErr != nil {
"want: %v, got %v", test.expAddr, test.serErr,
err)
case err != nil:
continue
}