forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
484 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pair | ||
|
||
import "strings" | ||
|
||
type CurrencyItem string | ||
|
||
func (c CurrencyItem) Lower() CurrencyItem { | ||
return CurrencyItem(strings.ToLower(string(c))) | ||
} | ||
|
||
func (c CurrencyItem) Upper() CurrencyItem { | ||
return CurrencyItem(strings.ToUpper(string(c))) | ||
} | ||
|
||
func (c CurrencyItem) String() string { | ||
return string(c) | ||
} | ||
|
||
type CurrencyPair struct { | ||
Delimiter string `json:"delimiter"` | ||
FirstCurrency CurrencyItem `json:"first_currency"` | ||
SecondCurrency CurrencyItem `json:"second_currency"` | ||
} | ||
|
||
func (c CurrencyPair) GetFirstCurrency() CurrencyItem { | ||
return c.FirstCurrency | ||
} | ||
|
||
func (c CurrencyPair) GetSecondCurrency() CurrencyItem { | ||
return c.SecondCurrency | ||
} | ||
|
||
func (c CurrencyPair) Pair() CurrencyItem { | ||
return c.FirstCurrency + CurrencyItem(c.Delimiter) + c.SecondCurrency | ||
} | ||
|
||
func NewCurrencyPairDelimiter(currency, delimiter string) CurrencyPair { | ||
result := strings.Split(currency, delimiter) | ||
return CurrencyPair{ | ||
Delimiter: delimiter, | ||
FirstCurrency: CurrencyItem(result[0]), | ||
SecondCurrency: CurrencyItem(result[1]), | ||
} | ||
} | ||
|
||
func NewCurrencyPair(firstCurrency, secondCurrency string) CurrencyPair { | ||
return CurrencyPair{ | ||
FirstCurrency: CurrencyItem(firstCurrency), | ||
SecondCurrency: CurrencyItem(secondCurrency), | ||
} | ||
} | ||
|
||
func NewCurrencyPairFromString(currency string) CurrencyPair { | ||
delmiters := []string{"_", "-"} | ||
var delimiter string | ||
for _, x := range delmiters { | ||
if strings.Contains(currency, x) { | ||
delimiter = x | ||
return NewCurrencyPairDelimiter(currency, delimiter) | ||
} | ||
} | ||
return NewCurrencyPair(currency[0:3], currency[3:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package pair | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLower(t *testing.T) { | ||
t.Parallel() | ||
pair := CurrencyItem("BTCUSD") | ||
actual := pair.Lower() | ||
expected := CurrencyItem("btcusd") | ||
if actual != expected { | ||
t.Errorf("Test failed. Lower(): %s was not equal to expected value: %s", | ||
actual, expected) | ||
} | ||
} | ||
|
||
func TestUpper(t *testing.T) { | ||
t.Parallel() | ||
pair := CurrencyItem("btcusd") | ||
actual := pair.Upper() | ||
expected := CurrencyItem("BTCUSD") | ||
if actual != expected { | ||
t.Errorf("Test failed. Upper(): %s was not equal to expected value: %s", | ||
actual, expected) | ||
} | ||
} | ||
|
||
func TestString(t *testing.T) { | ||
t.Parallel() | ||
pair := NewCurrencyPair("BTC", "USD") | ||
actual := "BTCUSD" | ||
expected := pair.Pair().String() | ||
if actual != expected { | ||
t.Errorf("Test failed. String(): %s was not equal to expected value: %s", | ||
actual, expected) | ||
} | ||
} | ||
|
||
func TestGetFirstCurrency(t *testing.T) { | ||
t.Parallel() | ||
pair := NewCurrencyPair("BTC", "USD") | ||
actual := pair.GetFirstCurrency() | ||
expected := CurrencyItem("BTC") | ||
if actual != expected { | ||
t.Errorf("Test failed. GetFirstCurrency(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestGetSecondCurrency(t *testing.T) { | ||
t.Parallel() | ||
pair := NewCurrencyPair("BTC", "USD") | ||
actual := pair.GetSecondCurrency() | ||
expected := CurrencyItem("USD") | ||
if actual != expected { | ||
t.Errorf("Test failed. GetSecondCurrency(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestPair(t *testing.T) { | ||
t.Parallel() | ||
pair := NewCurrencyPair("BTC", "USD") | ||
actual := pair.Pair() | ||
expected := CurrencyItem("BTCUSD") | ||
if actual != expected { | ||
t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestNewCurrencyPair(t *testing.T) { | ||
t.Parallel() | ||
pair := NewCurrencyPair("BTC", "USD") | ||
actual := pair.Pair() | ||
expected := CurrencyItem("BTCUSD") | ||
if actual != expected { | ||
t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestNewCurrencyPairDelimiter(t *testing.T) { | ||
t.Parallel() | ||
pair := NewCurrencyPairDelimiter("BTC-USD", "-") | ||
actual := pair.Pair() | ||
expected := CurrencyItem("BTC-USD") | ||
if actual != expected { | ||
t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
|
||
actual = CurrencyItem(pair.Delimiter) | ||
expected = "-" | ||
if actual != expected { | ||
t.Errorf("Test failed. Delmiter: %s was not equal to expected value: %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestNewCurrencyPairFromString(t *testing.T) { | ||
t.Parallel() | ||
pairStr := "BTC-USD" | ||
pair := NewCurrencyPairFromString(pairStr) | ||
actual := pair.Pair() | ||
expected := CurrencyItem("BTC-USD") | ||
if actual != expected { | ||
t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
|
||
pairStr = "BTCUSD" | ||
pair = NewCurrencyPairFromString(pairStr) | ||
actual = pair.Pair() | ||
expected = CurrencyItem("BTCUSD") | ||
if actual != expected { | ||
t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.