diff --git a/wallet/wallet.go b/wallet/wallet.go index fca806e..efa0bc4 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -1,6 +1,9 @@ package wallet -import "fmt" +import ( + "errors" + "fmt" +) type Bitcoin int @@ -16,8 +19,12 @@ func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } -func (w *Wallet) Withdraw(amount Bitcoin) { +func (w *Wallet) Withdraw(amount Bitcoin) error { + if w.balance < amount { + return errors.New("not enough to withdraw") + } w.balance -= amount + return nil } func (w *Wallet) Balance() Bitcoin { diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index 65e764c..2e26f58 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -11,14 +11,27 @@ func TestWallet(t *testing.T) { t.Errorf("got %q want %q", got, want) } } + t.Run("deposit", func(t *testing.T) { wallet := Wallet{} wallet.Deposit(10) assertBalance(t, wallet, Bitcoin(10)) }) + t.Run("withdraw", func(t *testing.T) { wallet := Wallet{balance: Bitcoin(20)} wallet.Withdraw(10) assertBalance(t, wallet, Bitcoin(10)) }) + + t.Run("withdraw insufficient funds", func(t *testing.T) { + startingBalance := Bitcoin(20) + wallet := Wallet{balance: startingBalance} + err := wallet.Withdraw(100) + assertBalance(t, wallet, startingBalance) + + if err == nil { + t.Error("wanted an error but didn't get one") + } + }) }