From 768adfdd1592392143df59d80e1e4ce9a18d0a6d Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 21:22:33 +0300 Subject: [PATCH] [#302] pkg/version: Convert nil `Version` to nil message Document that `Version.ToV2` method return `nil` when called on `nil`. Document that `NewVersionFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/version.go | 4 ++++ pkg/version_test.go | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/version.go b/pkg/version.go index 7e2e645..68fd8e7 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -12,6 +12,8 @@ type Version refs.Version const sdkMjr, sdkMnr = 2, 6 // NewVersionFromV2 wraps v2 Version message to Version. +// +// Nil refs.Version converts to nil. func NewVersionFromV2(v *refs.Version) *Version { return (*Version)(v) } @@ -58,6 +60,8 @@ func (v *Version) SetMinor(val uint32) { } // ToV2 converts Version to v2 Version message. +// +// Nil Version converts to nil. func (v *Version) ToV2() *refs.Version { return (*refs.Version)(v) } diff --git a/pkg/version_test.go b/pkg/version_test.go index 2f5ee28..fa75655 100644 --- a/pkg/version_test.go +++ b/pkg/version_test.go @@ -3,10 +3,11 @@ package pkg import ( "testing" + "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/stretchr/testify/require" ) -func TestNewVersionFromV2(t *testing.T) { +func TestNewVersion(t *testing.T) { v := NewVersion() var mjr, mnr uint32 = 1, 2 @@ -88,3 +89,19 @@ func TestVersionEncoding(t *testing.T) { require.Equal(t, v, v2) }) } + +func TestNewVersionFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *refs.Version + + require.Nil(t, NewVersionFromV2(x)) + }) +} + +func TestVersion_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *Version + + require.Nil(t, x.ToV2()) + }) +}