[#302] pkg/owner: Document default values set in

`NewTrust`, `NewPeerToPeerTrust` and `NewGlobalTrust`

Document field values of instance constructed via
`NewTrust`, `NewPeerToPeerTrust` and `NewGlobalTrust`.
Assert the values in corresponding unit test.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 20:21:03 +03:00 committed by Alex Vanin
parent 8ab3abab4b
commit 2d7a658059
2 changed files with 61 additions and 1 deletions

View file

@ -14,6 +14,10 @@ import (
type Trust reputation.Trust type Trust reputation.Trust
// NewTrust creates and returns blank Trust. // NewTrust creates and returns blank Trust.
//
// Defaults:
// - value: 0;
// - PeerID: nil.
func NewTrust() *Trust { func NewTrust() *Trust {
return TrustFromV2(new(reputation.Trust)) return TrustFromV2(new(reputation.Trust))
} }
@ -109,6 +113,10 @@ func (x *Trust) UnmarshalJSON(data []byte) error {
type PeerToPeerTrust reputation.PeerToPeerTrust type PeerToPeerTrust reputation.PeerToPeerTrust
// NewPeerToPeerTrust creates and returns blank PeerToPeerTrust. // NewPeerToPeerTrust creates and returns blank PeerToPeerTrust.
//
// Defaults:
// - trusting: nil;
// - trust: nil.
func NewPeerToPeerTrust() *PeerToPeerTrust { func NewPeerToPeerTrust() *PeerToPeerTrust {
return PeerToPeerTrustFromV2(new(reputation.PeerToPeerTrust)) return PeerToPeerTrustFromV2(new(reputation.PeerToPeerTrust))
} }
@ -193,7 +201,10 @@ type GlobalTrust reputation.GlobalTrust
// NewGlobalTrust creates and returns blank GlobalTrust. // NewGlobalTrust creates and returns blank GlobalTrust.
// //
// Version is initialized to current library version. // Defaults:
// - version: pkg.SDKVersion();
// - manager: nil;
// - trust: nil.
func NewGlobalTrust() *GlobalTrust { func NewGlobalTrust() *GlobalTrust {
gt := GlobalTrustFromV2(new(reputation.GlobalTrust)) gt := GlobalTrustFromV2(new(reputation.GlobalTrust))
gt.SetVersion(pkg.SDKVersion()) gt.SetVersion(pkg.SDKVersion())

View file

@ -199,3 +199,52 @@ func TestGlobalTrust_ToV2(t *testing.T) {
require.Nil(t, x.ToV2()) require.Nil(t, x.ToV2())
}) })
} }
func TestNewTrust(t *testing.T) {
t.Run("default values", func(t *testing.T) {
trust := reputation.NewTrust()
// check initial values
require.Zero(t, trust.Value())
require.Nil(t, trust.Peer())
// convert to v2 message
trustV2 := trust.ToV2()
require.Zero(t, trustV2.GetValue())
require.Nil(t, trustV2.GetPeer())
})
}
func TestNewPeerToPeerTrust(t *testing.T) {
t.Run("default values", func(t *testing.T) {
trust := reputation.NewPeerToPeerTrust()
// check initial values
require.Nil(t, trust.Trust())
require.Nil(t, trust.TrustingPeer())
// convert to v2 message
trustV2 := trust.ToV2()
require.Nil(t, trustV2.GetTrust())
require.Nil(t, trustV2.GetTrustingPeer())
})
}
func TestNewGlobalTrust(t *testing.T) {
t.Run("default values", func(t *testing.T) {
trust := reputation.NewGlobalTrust()
// check initial values
require.Nil(t, trust.Manager())
require.Nil(t, trust.Trust())
require.Equal(t, pkg.SDKVersion().String(), trust.Version().String())
// convert to v2 message
trustV2 := trust.ToV2()
require.Nil(t, trustV2.GetBody())
})
}