[#302] pkg/xheader: Convert nil XHeader to nil message

Document that `XHeader.ToV2` method return `nil`
when called on `nil`. Document that `NewXHeaderFromV2`
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:28:36 +03:00 committed by Alex Vanin
parent dc99d4edce
commit 41307a5e00
2 changed files with 21 additions and 0 deletions

View file

@ -8,6 +8,8 @@ import (
type XHeader session.XHeader
// NewXHeaderFromV2 wraps v2 XHeader message to XHeader.
//
// Nil session.XHeader converts to nil.
func NewXHeaderFromV2(v *session.XHeader) *XHeader {
return (*XHeader)(v)
}
@ -18,6 +20,8 @@ func NewXHeader() *XHeader {
}
// ToV2 converts XHeader to v2 XHeader message.
//
// Nil XHeader converts to nil.
func (x *XHeader) ToV2() *session.XHeader {
return (*session.XHeader)(x)
}

View file

@ -3,6 +3,7 @@ package pkg
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/stretchr/testify/require"
)
@ -23,3 +24,19 @@ func TestXHeader(t *testing.T) {
require.Equal(t, key, xV2.GetKey())
require.Equal(t, val, xV2.GetValue())
}
func TestNewXHeaderFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *session.XHeader
require.Nil(t, NewXHeaderFromV2(x))
})
}
func TestXHeader_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *XHeader
require.Nil(t, x.ToV2())
})
}