From 6142a352daba9261d37dca30de8be603a9059ab7 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 17:07:04 +0300 Subject: [PATCH] [#302] pkg/netmap: Convert nil `NetworkInfo` to nil message Document that `NetworkInfo.ToV2` method return `nil` when called on `nil`. Document that `NewNetworkInfoFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/netmap/network_info.go | 4 ++++ pkg/netmap/network_info_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/netmap/network_info.go b/pkg/netmap/network_info.go index ba3f204a..d4a6bdbc 100644 --- a/pkg/netmap/network_info.go +++ b/pkg/netmap/network_info.go @@ -9,6 +9,8 @@ import ( type NetworkInfo netmap.NetworkInfo // NewNetworkInfoFromV2 wraps v2 NetworkInfo message to NetworkInfo. +// +// Nil netmap.NetworkInfo converts to nil. func NewNetworkInfoFromV2(iV2 *netmap.NetworkInfo) *NetworkInfo { return (*NetworkInfo)(iV2) } @@ -19,6 +21,8 @@ func NewNetworkInfo() *NetworkInfo { } // ToV2 converts NetworkInfo to v2 NetworkInfo. +// +// Nil NetworkInfo converts to nil. func (i *NetworkInfo) ToV2() *netmap.NetworkInfo { return (*netmap.NetworkInfo)(i) } diff --git a/pkg/netmap/network_info_test.go b/pkg/netmap/network_info_test.go index bb8643fe..04234e8b 100644 --- a/pkg/netmap/network_info_test.go +++ b/pkg/netmap/network_info_test.go @@ -51,3 +51,17 @@ func TestNetworkInfoEncoding(t *testing.T) { require.Equal(t, i, i2) }) } + +func TestNewNetworkInfoFromV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + require.Nil(t, NewNetworkInfoFromV2(nil)) + }) +} + +func TestNetworkInfo_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *NetworkInfo + + require.Nil(t, x.ToV2()) + }) +}