[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 21:22:33 +03:00 committed by Alex Vanin
parent bb0cd8e4c0
commit 768adfdd15
2 changed files with 22 additions and 1 deletions

View file

@ -12,6 +12,8 @@ type Version refs.Version
const sdkMjr, sdkMnr = 2, 6 const sdkMjr, sdkMnr = 2, 6
// NewVersionFromV2 wraps v2 Version message to Version. // NewVersionFromV2 wraps v2 Version message to Version.
//
// Nil refs.Version converts to nil.
func NewVersionFromV2(v *refs.Version) *Version { func NewVersionFromV2(v *refs.Version) *Version {
return (*Version)(v) return (*Version)(v)
} }
@ -58,6 +60,8 @@ func (v *Version) SetMinor(val uint32) {
} }
// ToV2 converts Version to v2 Version message. // ToV2 converts Version to v2 Version message.
//
// Nil Version converts to nil.
func (v *Version) ToV2() *refs.Version { func (v *Version) ToV2() *refs.Version {
return (*refs.Version)(v) return (*refs.Version)(v)
} }

View file

@ -3,10 +3,11 @@ package pkg
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestNewVersionFromV2(t *testing.T) { func TestNewVersion(t *testing.T) {
v := NewVersion() v := NewVersion()
var mjr, mnr uint32 = 1, 2 var mjr, mnr uint32 = 1, 2
@ -88,3 +89,19 @@ func TestVersionEncoding(t *testing.T) {
require.Equal(t, v, v2) 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())
})
}