this repo has no description
at main 61 lines 1.3 kB view raw
1package pointers 2 3import "testing" 4 5func TestWallet(t *testing.T) { 6 t.Run("deposit", func(t *testing.T) { 7 wallet := Wallet{} 8 9 wallet.Deposit(Bitcoin(10)) 10 11 assertBalance(t, wallet, Bitcoin(10)) 12 }) 13 14 t.Run("withdraw", func(t *testing.T) { 15 wallet := Wallet{balance: Bitcoin(20)} 16 17 err := wallet.Withdraw(Bitcoin(10)) 18 19 assertNoError(t, err) 20 assertBalance(t, wallet, Bitcoin(10)) 21 }) 22 23 t.Run("withdraw insufficient funds", func(t *testing.T) { 24 startingBalance := Bitcoin(20) 25 wallet := Wallet{startingBalance} 26 err := wallet.Withdraw(Bitcoin(100)) 27 28 assertError(t, err, "cannot withdraw, insufficient funds") 29 assertBalance(t, wallet, startingBalance) 30 }) 31} 32 33func assertBalance(t testing.TB, wallet Wallet, want Bitcoin) { 34 t.Helper() 35 got := wallet.Balance() 36 37 if got != want { 38 t.Errorf("got %s want %s", got, want) 39 } 40} 41 42func assertNoError(t testing.TB, got error) { 43 t.Helper() 44 45 if got != nil { 46 t.Fatal("got an error butt didn't want one") 47 } 48} 49 50 51func assertError(t testing.TB, got error, want string) { 52 t.Helper() 53 54 if got == nil { 55 t.Fatal("didn't get an error but wanted one") 56 } 57 58 if got.Error() != want { 59 t.Errorf("got %q, want %q", got, want) 60 } 61}