[#302] pkg/checksum: Convert nil Checksum to nil message

Document that `Checksum.ToV2` method return `nil`
when called on `nil`. Document that `NewChecksumFromV2`
function return `nil` when called on `nil`. Write
corresponding unit tests.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 21:02:30 +03:00 committed by Alex Vanin
parent 245271bb65
commit 35b6629e1c
2 changed files with 21 additions and 0 deletions

View file

@ -28,6 +28,8 @@ const (
)
// NewChecksumFromV2 wraps v2 Checksum message to Checksum.
//
// Nil refs.Checksum converts to nil.
func NewChecksumFromV2(cV2 *refs.Checksum) *Checksum {
return (*Checksum)(cV2)
}
@ -72,6 +74,9 @@ func (c *Checksum) SetTillichZemor(v [64]byte) {
checksum.SetSum(v[:])
}
// ToV2 converts Checksum to v2 Checksum message.
//
// Nil Checksum converts to nil.
func (c *Checksum) ToV2() *refs.Checksum {
return (*refs.Checksum)(c)
}

View file

@ -99,3 +99,19 @@ func TestChecksumEncoding(t *testing.T) {
require.Equal(t, cs, cs2)
})
}
func TestNewChecksumFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *refs.Checksum
require.Nil(t, NewChecksumFromV2(x))
})
}
func TestChecksum_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Checksum
require.Nil(t, x.ToV2())
})
}