Add CopyPairFormat to pair package and expand test coverage

This commit is contained in:
Adrian Gallagher
2018-02-06 15:41:51 +11:00
parent 340b49f112
commit 2dc4af00c3
2 changed files with 37 additions and 0 deletions

View File

@@ -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{}
}

View File

@@ -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")
}
}