From 2dc4af00c32aac9dd51fc95380ce7b6b68af8aca Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 6 Feb 2018 15:41:51 +1100 Subject: [PATCH] Add CopyPairFormat to pair package and expand test coverage --- currency/pair/pair.go | 10 ++++++++++ currency/pair/pair_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/currency/pair/pair.go b/currency/pair/pair.go index f757577e..9148a442 100644 --- a/currency/pair/pair.go +++ b/currency/pair/pair.go @@ -149,3 +149,13 @@ func FormatPairs(pairs []string, delimiter, index string) []CurrencyPair { } return result } + +// CopyPairFormat copies the pair format from a list of pairs once matched +func CopyPairFormat(p CurrencyPair, pairs []CurrencyPair) CurrencyPair { + for x := range pairs { + if p.Equal(pairs[x]) { + return pairs[x] + } + } + return CurrencyPair{} +} diff --git a/currency/pair/pair_test.go b/currency/pair/pair_test.go index 1785d0c0..f568c125 100644 --- a/currency/pair/pair_test.go +++ b/currency/pair/pair_test.go @@ -253,6 +253,10 @@ func TestContains(t *testing.T) { } func TestFormatPairs(t *testing.T) { + if len(FormatPairs([]string{""}, "-", "")) > 0 { + t.Error("Test failed. TestFormatPairs: Empty string returned a valid pair") + } + if FormatPairs([]string{"BTC-USD"}, "-", "")[0].Pair().String() != "BTC-USD" { t.Error("Test failed. TestFormatPairs: Expected pair was not found") } @@ -265,3 +269,26 @@ func TestFormatPairs(t *testing.T) { t.Error("Test failed. TestFormatPairs: Expected pair was not found") } } + +func TestCopyPairFormat(t *testing.T) { + pairOne := NewCurrencyPair("BTC", "USD") + pairOne.Delimiter = "-" + pairTwo := NewCurrencyPair("LTC", "USD") + + var pairs []CurrencyPair + pairs = append(pairs, pairOne) + pairs = append(pairs, pairTwo) + + testPair := NewCurrencyPair("BTC", "USD") + testPair.Delimiter = "~" + + result := CopyPairFormat(testPair, pairs) + if result.Pair().String() != "BTC-USD" { + t.Error("Test failed. TestCopyPairFormat: Expected pair was not found") + } + + result = CopyPairFormat(NewCurrencyPair("ETH", "USD"), pairs) + if result.Pair().String() != "" { + t.Error("Test failed. TestCopyPairFormat: Unexpected non empty pair returned") + } +}