Skip to content

Commit 2afcf67

Browse files
committed
🐛 (bitcoin-wallet): add throw error logic for insufficient error
1 parent 2cf0140 commit 2afcf67

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pointers-and-errors/bitcoin-and-wallet.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func (w *Wallet) Balance() Bitcoin {
2424
}
2525

2626
// Withdraw - deposit amount from wallet balance
27-
func (w *Wallet) Withdraw(amount Bitcoin) {
27+
func (w *Wallet) Withdraw(amount Bitcoin) error {
28+
if w.balance < amount {
29+
return fmt.Errorf("oh no")
30+
}
2831
w.balance -= amount
32+
return nil
2933
}

pointers-and-errors/bitcoin-and-wallet_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ func TestWallet(t *testing.T) {
2929
want := Bitcoin(10)
3030
assertBalance(t, wallet, want)
3131
})
32+
33+
t.Run("withdraw insufficient funds", func(t *testing.T) {
34+
startingBalance := Bitcoin(20)
35+
wallet := Wallet{startingBalance}
36+
err := wallet.Withdraw(Bitcoin(100))
37+
38+
assertBalance(t, wallet, startingBalance)
39+
40+
if err == nil {
41+
t.Error("wanted an error but didn't get one")
42+
}
43+
})
3244
}

0 commit comments

Comments
 (0)