From cce7ecbc00423e2c58c2e013b25a5e2592b5b8bc Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 19:57:28 +0300 Subject: [PATCH] [#302] pkg/reputation: Document default values set in `NewPeerID` Document field values of instance constructed via `NewPeerID`. Assert the values in corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/reputation/peer.go | 3 +++ pkg/reputation/peer_test.go | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/pkg/reputation/peer.go b/pkg/reputation/peer.go index af7bec13..cdbfce1c 100644 --- a/pkg/reputation/peer.go +++ b/pkg/reputation/peer.go @@ -12,6 +12,9 @@ import ( type PeerID reputation.PeerID // NewPeerID creates and returns blank PeerID. +// +// Defaults: +// - publicKey: nil. func NewPeerID() *PeerID { return PeerIDFromV2(new(reputation.PeerID)) } diff --git a/pkg/reputation/peer_test.go b/pkg/reputation/peer_test.go index b226cf94..d3463a51 100644 --- a/pkg/reputation/peer_test.go +++ b/pkg/reputation/peer_test.go @@ -24,16 +24,24 @@ func TestPeerID_ToV2(t *testing.T) { } func TestPeerID_String(t *testing.T) { - id := reputationtest.GeneratePeerID() + t.Run("Parse/String", func(t *testing.T) { + id := reputationtest.GeneratePeerID() - strID := id.String() + strID := id.String() - id2 := reputation.NewPeerID() + id2 := reputation.NewPeerID() - err := id2.Parse(strID) - require.NoError(t, err) + err := id2.Parse(strID) + require.NoError(t, err) - require.Equal(t, id, id2) + require.Equal(t, id, id2) + }) + + t.Run("nil", func(t *testing.T) { + id := reputation.NewPeerID() + + require.Empty(t, id.String()) + }) } func TestPeerIDEncoding(t *testing.T) { @@ -67,3 +75,14 @@ func TestPeerIDFromV2(t *testing.T) { require.Nil(t, reputation.PeerIDFromV2(x)) }) } + +func TestNewPeerID(t *testing.T) { + t.Run("default values", func(t *testing.T) { + id := reputation.NewPeerID() + + // convert to v2 message + idV2 := id.ToV2() + + require.Nil(t, idV2.GetPublicKey()) + }) +}