From fb591f5faca4387b4b21e2c6a27b0849dd8e9244 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 18:26:20 +0300 Subject: [PATCH] [#302] pkg/netmap: Convert nil `Replica` to nil message Document that `Replica.ToV2` method return `nil` when called on `nil`. Document that `ReplicaFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/netmap/replica.go | 4 ++++ pkg/netmap/replica_test.go | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/netmap/replica.go b/pkg/netmap/replica.go index 95349e6..b9bae5d 100644 --- a/pkg/netmap/replica.go +++ b/pkg/netmap/replica.go @@ -13,11 +13,15 @@ func NewReplica() *Replica { } // NewReplicaFromV2 converts v2 Replica to Replica. +// +// Nil netmap.Replica converts to nil. func NewReplicaFromV2(f *netmap.Replica) *Replica { return (*Replica)(f) } // ToV2 converts Replica to v2 Replica. +// +// Nil Replica converts to nil. func (r *Replica) ToV2() *netmap.Replica { return (*netmap.Replica)(r) } diff --git a/pkg/netmap/replica_test.go b/pkg/netmap/replica_test.go index e366c2e..9c6d5a9 100644 --- a/pkg/netmap/replica_test.go +++ b/pkg/netmap/replica_test.go @@ -4,6 +4,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" ) @@ -16,13 +17,19 @@ func testReplica() *Replica { } func TestReplicaFromV2(t *testing.T) { - rV2 := new(netmap.Replica) - rV2.SetCount(3) - rV2.SetSelector("selector") + t.Run("from nil", func(t *testing.T) { + var x *netmap.Replica - r := NewReplicaFromV2(rV2) + require.Nil(t, NewReplicaFromV2(x)) + }) - require.Equal(t, rV2, r.ToV2()) + t.Run("from non-nil", func(t *testing.T) { + rV2 := testv2.GenerateReplica(false) + + r := NewReplicaFromV2(rV2) + + require.Equal(t, rV2, r.ToV2()) + }) } func TestReplica_Count(t *testing.T) { @@ -66,3 +73,11 @@ func TestReplicaEncoding(t *testing.T) { require.Equal(t, r, r2) }) } + +func TestReplica_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *Replica + + require.Nil(t, x.ToV2()) + }) +}