File tree Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change 11package pointers_and_errors
22
3- import "fmt"
3+ import (
4+ "errors"
5+ "fmt"
6+ )
7+
8+ var ErrInsufficientFunds = errors .New ("cannot withdraw, insufficient funds" )
49
510type Bitcoin int
611
@@ -26,7 +31,7 @@ func (w *Wallet) Balance() Bitcoin {
2631// Withdraw - deposit amount from wallet balance
2732func (w * Wallet ) Withdraw (amount Bitcoin ) error {
2833 if w .balance < amount {
29- return fmt . Errorf ( "oh no" )
34+ return ErrInsufficientFunds
3035 }
3136 w .balance -= amount
3237 return nil
Original file line number Diff line number Diff line change @@ -14,11 +14,15 @@ func TestWallet(t *testing.T) {
1414 }
1515 }
1616
17- assertError := func (t testing.TB , err error ) {
17+ assertError := func (t testing.TB , err , want error ) {
1818 t .Helper ()
1919 if err == nil {
2020 t .Error ("wanted an error but didn't get one" )
2121 }
22+
23+ if err != want {
24+ t .Errorf ("got %q want %q" , err .Error (), want )
25+ }
2226 }
2327
2428 t .Run ("deposit" , func (t * testing.T ) {
@@ -42,7 +46,7 @@ func TestWallet(t *testing.T) {
4246 wallet := Wallet {startingBalance }
4347 err := wallet .Withdraw (Bitcoin (100 ))
4448
45- assertError (t , err )
49+ assertError (t , err , ErrInsufficientFunds )
4650 assertBalance (t , wallet , startingBalance )
4751
4852 })
You can’t perform that action at this time.
0 commit comments