Skip to content

Commit

Permalink
Added additional CurrencyFromName helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jul 2, 2020
1 parent a5bca79 commit 99a2675
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
17 changes: 16 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bsvrates

import "strings"

const (

// version is the current package version
Expand Down Expand Up @@ -90,11 +92,24 @@ func (c Currency) Name() string {
return "usd"
case CurrencyBitcoin:
return "bsv"
default:
return ""
}
return ""
}

// CurrencyToName helper function to convert the currency value to it's associated name
func CurrencyToName(currency Currency) string {
return currency.Name()
}

// CurrencyFromName helper function to convert the name into it's Currency type
func CurrencyFromName(name string) Currency {
switch strings.ToLower(name) {
case "usd":
return CurrencyDollars
case "bsv":
return CurrencyBitcoin
default:
return CurrencyDollars
}
}
23 changes: 23 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,29 @@ func TestCurrencyToName(t *testing.T) {
}
}

// TestCurrencyFromName will test the method CurrencyFromName()
func TestCurrencyFromName(t *testing.T) {
t.Parallel()

// Create the list of tests
var tests = []struct {
input string
expected Currency
}{
{"", CurrencyDollars},
{"usd", CurrencyDollars},
{"bsv", CurrencyBitcoin},
{"bogus", CurrencyDollars},
}

// Test all
for _, test := range tests {
if currency := CurrencyFromName(test.input); currency != test.expected {
t.Errorf("%s Failed: Currency returned a unexpected result: %d", t.Name(), currency)
}
}
}

// TestCurrency_IsAccepted will test the method IsAccepted()
func TestCurrency_IsAccepted(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 99a2675

Please sign in to comment.