1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Implement peer String() method

This commit is contained in:
Jesse Li
2020-01-03 13:04:16 -05:00
parent a8225373e7
commit 09818dfa78
3 changed files with 22 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"net"
"strconv"
)
// Peer encodes connection information for a peer
@@ -28,3 +29,7 @@ func Unmarshal(peersBin []byte) ([]Peer, error) {
}
return peers, nil
}
func (p Peer) String() string {
return net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
}

View File

@@ -37,3 +37,19 @@ func TestUnmarshal(t *testing.T) {
assert.Equal(t, test.output, peers)
}
}
func TestString(t *testing.T) {
tests := []struct {
input Peer
output string
}{
{
input: Peer{IP: net.IP{127, 0, 0, 1}, Port: 8080},
output: "127.0.0.1:8080",
},
}
for _, test := range tests {
s := test.input.String()
assert.Equal(t, test.output, s)
}
}