Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rekseto authored and Rekseto committed Apr 26, 2022
1 parent 2d0212e commit 0a8722b
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 323 deletions.
41 changes: 7 additions & 34 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

var ErrCurrencyNotFound = errors.New("currency not found")

type CurrencyCode string

// Currency represents currency with information how to format it
type Currency struct {
Code CurrencyCode
Expand All @@ -19,8 +21,8 @@ type Currency struct {

type CurrenciesMap map[CurrencyCode]Currency

func (c CurrenciesMap) CurrencyByNumericCode(code string) (result Currency, err error) {
for _, sc := range c {
func CurrencyByNumericCode(code string) (result Currency, err error) {
for _, sc := range currencies {
if sc.NumericCode == code {
return sc, nil
}
Expand All @@ -29,21 +31,16 @@ func (c CurrenciesMap) CurrencyByNumericCode(code string) (result Currency, err
return result, ErrCurrencyNotFound
}

func (c CurrenciesMap) CurrencyByCode(code CurrencyCode) (result Currency, err error) {
sc, ok := c[code]
func CurrencyByCode(code CurrencyCode) (result Currency, err error) {
sc, ok := currencies[code]
if !ok {
return sc, ErrCurrencyNotFound
}

return sc, nil
}

func (c CurrenciesMap) Add(currency Currency) CurrenciesMap {
c[currency.Code] = currency
return c
}

var Currencies = CurrenciesMap{
var currencies = CurrenciesMap{
MKD: {Decimal: ".", Thousand: ",", Code: MKD, Fraction: 2, NumericCode: "807", Grapheme: "\u0434\u0435\u043d", Template: "$1"},
MWK: {Decimal: ".", Thousand: ",", Code: MWK, Fraction: 2, NumericCode: "454", Grapheme: "MK", Template: "$1"},
BGN: {Decimal: ".", Thousand: ",", Code: BGN, Fraction: 2, NumericCode: "975", Grapheme: "\u043b\u0432", Template: "$1"},
Expand Down Expand Up @@ -215,30 +212,6 @@ var Currencies = CurrenciesMap{
SBD: {Decimal: ".", Thousand: ",", Code: SBD, Fraction: 2, NumericCode: "090", Grapheme: "$", Template: "$1"},
}

// AddCurrency lets you insert or update currency in Currencies list.
func AddCurrency(code CurrencyCode, Grapheme, Template, Decimal, Thousand string, Fraction int, numericCode string) Currency {
c := Currency{
Code: code,
Grapheme: Grapheme,
Template: Template,
Decimal: Decimal,
Thousand: Thousand,
Fraction: Fraction,
NumericCode: numericCode,
}

Currencies.Add(c)

return c
}

// GetCurrency returns the currency given the code.
func GetCurrency(code CurrencyCode) (result Currency, err error) {
return Currencies.CurrencyByCode(code)
}

type CurrencyCode string

const (
KRW CurrencyCode = "KRW"
ILS CurrencyCode = "ILS"
Expand Down
35 changes: 4 additions & 31 deletions currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,23 @@ package monies_test
import (
"github.com/Craftserve/monies"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestAddCurrency(t *testing.T) {
var code monies.CurrencyCode = "TEST"
decimals := 5
monies.AddCurrency(code, "T$", "1 $", ".", ",", decimals, "0")

m, err := monies.New(1, code)
assert.NoError(t, err)

assert.Equal(t, code, m.Currency().Code)
assert.Equal(t, decimals, m.Currency().Fraction)
}

func TestCurrencyGetCurrency(t *testing.T) {
var code monies.CurrencyCode = "KLINGONDOLLAR"
desired := monies.Currency{Decimal: ".", Thousand: ",", Code: code, Fraction: 2, Grapheme: "$", Template: "$1"}
monies.AddCurrency(desired.Code, desired.Grapheme, desired.Template, desired.Decimal, desired.Thousand, desired.Fraction, desired.NumericCode)

currency, err := monies.GetCurrency(code)
require.NoError(t, err)
assert.Equal(t, desired, currency)

}

func TestCurrencyGetNonExistingCurrency(t *testing.T) {
_, err := monies.GetCurrency("I*am*Not*a*CurrencyCode")
_, err := monies.CurrencyByCode("I*am*Not*a*CurrencyCode")
assert.Error(t, err, monies.ErrCurrencyNotFound)
}

func TestCurrencyGetCurrencyByNumericCode(t *testing.T) {
var code monies.CurrencyCode = "EUROGĄBKI"
desired := monies.Currency{Decimal: ".", Thousand: ",", Code: code, Fraction: 2, Grapheme: "$", Template: "$1", NumericCode: "9999"}
monies.AddCurrency(desired.Code, desired.Grapheme, desired.Template, desired.Decimal, desired.Thousand, desired.Fraction, desired.NumericCode)
desired := monies.Currency{Decimal: ",", Thousand: ".", Code: monies.HUF, Fraction: 0, NumericCode: "348", Grapheme: "Ft", Template: "1 $"}
currency, err := monies.CurrencyByNumericCode("348")

currency, err := monies.Currencies.CurrencyByNumericCode("9999")
assert.NoError(t, err)
assert.Equal(t, desired, currency)

}

func TestCurrencyCurrencyByNumericCodeNonExisting(t *testing.T) {
_, err := monies.Currencies.CurrencyByNumericCode("0900990")
_, err := monies.CurrencyByNumericCode("0900990")
assert.ErrorIs(t, err, monies.ErrCurrencyNotFound)
}
26 changes: 0 additions & 26 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@ package monies

import "math"

// @NOTE: Not sure if this file is really that needed

func add(a, b int64) int64 {
return a + b
}

func subtract(a, b int64) int64 {
return a - b
}

func multiply(a, m int64) int64 {
return a * m
}

func divide(a int64, d int64) int64 {
return a / d
}

func modulus(a int64, d int64) int64 {
return a % d
}
Expand All @@ -36,14 +18,6 @@ func absolute(a int64) int64 {
return a
}

func negative(a int64) int64 {
if a > 0 {
return -a
}

return a
}

func round(a int64, e int) int64 {
if a == 0 {
return 0
Expand Down
Loading

0 comments on commit 0a8722b

Please sign in to comment.