From f9d20a67f91ed4d499dd0888ec7d35f0de201397 Mon Sep 17 00:00:00 2001 From: Ayoob Mohammed Date: Fri, 19 Aug 2022 19:28:16 +0300 Subject: [PATCH] remove panic --- money.go | 32 ++++++++++++++++---------------- money_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/money.go b/money.go index b427729..8a6825f 100644 --- a/money.go +++ b/money.go @@ -199,53 +199,53 @@ func (m *Money) Negative() *Money { } // Add returns new Money struct with value representing sum of Self and Other Money. -func (m *Money) Add(om ...*Money) (*Money, error) { - if len(om) == 0 { - panic("At least one Money is required to add") +func (m *Money) Add(ms ...*Money) (*Money, error) { + if len(ms) == 0 { + return m, nil } k := New(0, m.currency.Code) - for i := 0; i < len(om); i++ { - if err := m.assertSameCurrency(om[i]); err != nil { + for _, m2 := range ms { + if err := m.assertSameCurrency(m2); err != nil { return nil, err } - k.amount = mutate.calc.add(k.amount, om[i].amount) + k.amount = mutate.calc.add(k.amount, m2.amount) } return &Money{amount: mutate.calc.add(m.amount, k.amount), currency: m.currency}, nil } // Subtract returns new Money struct with value representing difference of Self and Other Money. -func (m *Money) Subtract(om ...*Money) (*Money, error) { - if len(om) == 0 { - panic("At least one Money is required to subtract") +func (m *Money) Subtract(ms ...*Money) (*Money, error) { + if len(ms) == 0 { + return m, nil } k := New(0, m.currency.Code) - for i := 0; i < len(om); i++ { - if err := m.assertSameCurrency(om[i]); err != nil { + for _, m2 := range ms { + if err := m.assertSameCurrency(m2); err != nil { return nil, err } - k.amount = mutate.calc.add(k.amount, om[i].amount) + k.amount = mutate.calc.add(k.amount, m2.amount) } return &Money{amount: mutate.calc.subtract(m.amount, k.amount), currency: m.currency}, nil } // Multiply returns new Money struct with value representing Self multiplied value by multiplier. -func (m *Money) Multiply(mul ...int64) *Money { - if len(mul) == 0 { +func (m *Money) Multiply(muls ...int64) *Money { + if len(muls) == 0 { panic("At least one multiplier is required to multiply") } k := New(1, m.currency.Code) - for i := 0; i < len(mul); i++ { - k.amount = mutate.calc.multiply(k.amount, mul[i]) + for _, m2 := range muls { + k.amount = mutate.calc.multiply(k.amount, m2) } return &Money{amount: mutate.calc.multiply(m.amount, k.amount), currency: m.currency} diff --git a/money_test.go b/money_test.go index 3173d67..45c5fd0 100644 --- a/money_test.go +++ b/money_test.go @@ -370,6 +370,19 @@ func TestMoney_Add3(t *testing.T) { } } +func TestMoney_Add4(t *testing.T) { + m := New(100, EUR) + r, err := m.Add() + + if err != nil { + t.Error(err) + } + + if r.amount != 100 { + t.Error("Expected amount to be 100") + } +} + func TestMoney_Subtract(t *testing.T) { tcs := []struct { amount1 int64 @@ -436,6 +449,19 @@ func TestMoney_Subtract3(t *testing.T) { } } +func TestMoney_Subtract4(t *testing.T) { + m := New(100, EUR) + r, err := m.Subtract() + + if err != nil { + t.Error(err) + } + + if r.amount != 100 { + t.Error("Expected amount to be 100") + } +} + func TestMoney_Multiply(t *testing.T) { tcs := []struct { amount int64