From c47d1fb26b479ac156c395b3a630084199cc51f7 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 18:33:38 +0300 Subject: [PATCH] [#302] pkg/netmap: Convert nil `Selector` to nil message Document that `Selector.ToV2` method return `nil` when called on `nil`. Document that `NewSelectorFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/netmap/selector.go | 4 ++++ pkg/netmap/selector_test.go | 38 ++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pkg/netmap/selector.go b/pkg/netmap/selector.go index 7a7f2972..72ae857c 100644 --- a/pkg/netmap/selector.go +++ b/pkg/netmap/selector.go @@ -161,11 +161,15 @@ func NewSelector() *Selector { } // NewSelectorFromV2 converts v2 Selector to Selector. +// +// Nil netmap.Selector converts to nil. func NewSelectorFromV2(f *netmap.Selector) *Selector { return (*Selector)(f) } // ToV2 converts Selector to v2 Selector. +// +// Nil Selector converts to nil. func (s *Selector) ToV2() *netmap.Selector { return (*netmap.Selector)(s) } diff --git a/pkg/netmap/selector_test.go b/pkg/netmap/selector_test.go index e8e1f32b..f628d0b5 100644 --- a/pkg/netmap/selector_test.go +++ b/pkg/netmap/selector_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/nspcc-dev/neofs-api-go/v2/netmap" + testv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap/test" "github.com/stretchr/testify/require" ) @@ -414,19 +415,6 @@ func testSelector() *Selector { return s } -func TestSelectorFromV2(t *testing.T) { - sV2 := new(netmap.Selector) - sV2.SetName("name") - sV2.SetCount(3) - sV2.SetClause(netmap.Distinct) - sV2.SetAttribute("attribute") - sV2.SetFilter("filter") - - s := NewSelectorFromV2(sV2) - - require.Equal(t, sV2, s.ToV2()) -} - func TestSelector_Name(t *testing.T) { s := NewSelector() name := "some name" @@ -495,3 +483,27 @@ func TestSelectorEncoding(t *testing.T) { require.Equal(t, s, s2) }) } + +func TestSelector_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *Selector + + require.Nil(t, x.ToV2()) + }) +} + +func TestNewSelectorFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *netmap.Selector + + require.Nil(t, NewSelectorFromV2(x)) + }) + + t.Run("from non-nil", func(t *testing.T) { + sV2 := testv2.GenerateSelector(false) + + s := NewSelectorFromV2(sV2) + + require.Equal(t, sV2, s.ToV2()) + }) +}