From 6ee7bc6fdac98962d0b4e444304387427d489ba6 Mon Sep 17 00:00:00 2001 From: Jordi Llonch Date: Tue, 23 May 2017 13:00:16 +1000 Subject: [PATCH 1/3] a new instance of Money has invalid Currency content --- money.go | 6 +++++- money_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/money.go b/money.go index 36230c9..1ed1b6e 100644 --- a/money.go +++ b/money.go @@ -18,9 +18,13 @@ type Money struct { // New creates and returns new instance of Money func New(amount int64, code string) *Money { + c := currencies[code] + if c == nil { + c = newCurrency(code) + } return &Money{ amount: &Amount{val: amount}, - currency: newCurrency(code), + currency: newCurrency(code).get(), } } diff --git a/money_test.go b/money_test.go index 02fe644..cb611f3 100644 --- a/money_test.go +++ b/money_test.go @@ -23,6 +23,14 @@ func TestNew(t *testing.T) { } } +func TestCurrency(t *testing.T) { + m := New(1, "EUR") + f := m.Currency().Fraction + if f != 2 { + t.Errorf("Expected %d got %d", 2, f) + } +} + func TestMoney_SameCurrency(t *testing.T) { m := New(0, "EUR") om := New(0, "USD") From 5c391f03dc78563b280bc93c94d0a99dd81855bb Mon Sep 17 00:00:00 2001 From: Jordi Llonch Date: Thu, 25 May 2017 19:01:21 +1000 Subject: [PATCH 2/3] add mock currency --- money_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/money_test.go b/money_test.go index cb611f3..d50dcd6 100644 --- a/money_test.go +++ b/money_test.go @@ -24,10 +24,17 @@ func TestNew(t *testing.T) { } func TestCurrency(t *testing.T) { - m := New(1, "EUR") + code := "MOCK" + decimals := 5 + AddCurrency(code, "M$", "1 $", ".", ",", decimals) + m := New(1, code) + c := m.Currency().Code + if c != code { + t.Errorf("Expected %d got %d", code, c) + } f := m.Currency().Fraction - if f != 2 { - t.Errorf("Expected %d got %d", 2, f) + if f != decimals { + t.Errorf("Expected %d got %d", decimals, f) } } From f119e53930c62094a8c2f0c3dcfee74dca893eb8 Mon Sep 17 00:00:00 2001 From: Jordi Llonch Date: Thu, 25 May 2017 19:01:56 +1000 Subject: [PATCH 3/3] remove code --- money.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/money.go b/money.go index 1ed1b6e..8fc3d74 100644 --- a/money.go +++ b/money.go @@ -18,10 +18,6 @@ type Money struct { // New creates and returns new instance of Money func New(amount int64, code string) *Money { - c := currencies[code] - if c == nil { - c = newCurrency(code) - } return &Money{ amount: &Amount{val: amount}, currency: newCurrency(code).get(),