From d1e36691b3eede6da2595a9fb4ba513c035586d1 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Fri, 26 Jul 2024 07:44:07 +0100 Subject: [PATCH] Subscriptions: Add List.Clone (#1592) --- exchanges/subscription/list.go | 9 +++++++++ exchanges/subscription/list_test.go | 13 +++++++++++++ exchanges/subscription/subscription_test.go | 3 +-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/exchanges/subscription/list.go b/exchanges/subscription/list.go index 30b3cd5c..51d6edfa 100644 --- a/exchanges/subscription/list.go +++ b/exchanges/subscription/list.go @@ -46,6 +46,15 @@ func (l List) GroupPairs() (n List) { return s.List() } +// Clone returns a deep clone of the List +func (l List) Clone() List { + n := make(List, len(l)) + for i, s := range l { + n[i] = s.Clone() + } + return n +} + // QualifiedChannels returns a sorted list of all the qualified Channels in the list func (l List) QualifiedChannels() []string { c := make([]string, len(l)) diff --git a/exchanges/subscription/list_test.go b/exchanges/subscription/list_test.go index e1a338b4..08eba5da 100644 --- a/exchanges/subscription/list_test.go +++ b/exchanges/subscription/list_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/require" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" + "github.com/thrasher-corp/gocryptotrader/exchanges/kline" ) // TestListStrings exercises List.Strings() @@ -91,3 +92,15 @@ func TestAssetPairs(t *testing.T) { assert.ErrorIs(t, err, expErr, "Should error correctly on GetPairFormat") } } + +func TestListClone(t *testing.T) { + t.Parallel() + l := List{{Channel: TickerChannel}, {Channel: OrderbookChannel}} + n := l.Clone() + assert.NotSame(t, n, l, "Slices must not be the same") + require.NotEmpty(t, n, "List must not be empty") + assert.NotSame(t, n[0], l[0], "Subscriptions must be cloned") + assert.Equal(t, n[0], l[0], "Subscriptions should be equal") + l[0].Interval = kline.OneHour + assert.NotEqual(t, n[0], l[0], "Subscriptions should be cloned") +} diff --git a/exchanges/subscription/subscription_test.go b/exchanges/subscription/subscription_test.go index bd9a9a27..edc828a8 100644 --- a/exchanges/subscription/subscription_test.go +++ b/exchanges/subscription/subscription_test.go @@ -90,8 +90,7 @@ func TestSubscriptionMarshaling(t *testing.T) { assert.Equal(t, `{"enabled":true,"channel":"myTrades","authenticated":true}`, string(j), "Marshalling should be clean and concise") } -// TestClone exercises Clone -func TestClone(t *testing.T) { +func TestSubscriptionClone(t *testing.T) { t.Parallel() params := map[string]any{"a": 42} a := &Subscription{