forked from TrueCloudLab/frostfs-api-go
768adfdd15
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>
107 lines
1.8 KiB
Go
107 lines
1.8 KiB
Go
package pkg
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewVersion(t *testing.T) {
|
|
v := NewVersion()
|
|
|
|
var mjr, mnr uint32 = 1, 2
|
|
|
|
v.SetMajor(mjr)
|
|
v.SetMinor(mnr)
|
|
|
|
require.Equal(t, mjr, v.Major())
|
|
require.Equal(t, mnr, v.Minor())
|
|
|
|
ver := v.ToV2()
|
|
|
|
require.Equal(t, mjr, ver.GetMajor())
|
|
require.Equal(t, mnr, ver.GetMinor())
|
|
}
|
|
|
|
func TestSDKVersion(t *testing.T) {
|
|
v := SDKVersion()
|
|
|
|
require.Equal(t, uint32(sdkMjr), v.Major())
|
|
require.Equal(t, uint32(sdkMnr), v.Minor())
|
|
}
|
|
|
|
func TestIsSupportedVersion(t *testing.T) {
|
|
require.Error(t, IsSupportedVersion(nil))
|
|
|
|
v := NewVersion()
|
|
|
|
v.SetMajor(1)
|
|
require.Error(t, IsSupportedVersion(v))
|
|
|
|
v.SetMajor(3)
|
|
require.Error(t, IsSupportedVersion(v))
|
|
|
|
for _, item := range []struct {
|
|
mjr, maxMnr uint32
|
|
}{
|
|
{
|
|
mjr: 2,
|
|
maxMnr: sdkMnr,
|
|
},
|
|
} {
|
|
v.SetMajor(item.mjr)
|
|
|
|
for i := uint32(0); i <= item.maxMnr; i++ {
|
|
v.SetMinor(i)
|
|
|
|
require.NoError(t, IsSupportedVersion(v))
|
|
}
|
|
|
|
v.SetMinor(item.maxMnr + 1)
|
|
|
|
require.Error(t, IsSupportedVersion(v))
|
|
}
|
|
}
|
|
|
|
func TestVersionEncoding(t *testing.T) {
|
|
v := NewVersion()
|
|
v.SetMajor(1)
|
|
v.SetMinor(2)
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
data, err := v.Marshal()
|
|
require.NoError(t, err)
|
|
|
|
v2 := NewVersion()
|
|
require.NoError(t, v2.Unmarshal(data))
|
|
|
|
require.Equal(t, v, v2)
|
|
})
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
data, err := v.MarshalJSON()
|
|
require.NoError(t, err)
|
|
|
|
v2 := NewVersion()
|
|
require.NoError(t, v2.UnmarshalJSON(data))
|
|
|
|
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())
|
|
})
|
|
}
|