From 41307a5e00d7fc7a71240e8efce9aff299b2b494 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 21:28:36 +0300 Subject: [PATCH] [#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 --- pkg/xheader.go | 4 ++++ pkg/xheader_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/xheader.go b/pkg/xheader.go index 84d645c..7878677 100644 --- a/pkg/xheader.go +++ b/pkg/xheader.go @@ -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) } diff --git a/pkg/xheader_test.go b/pkg/xheader_test.go index 8f65227..66535be 100644 --- a/pkg/xheader_test.go +++ b/pkg/xheader_test.go @@ -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()) + }) +}