go-by-test/wallet/wallet_test.go

47 lines
1006 B
Go
Raw Normal View History

package wallet
import "testing"
func TestWallet(t *testing.T) {
2024-09-11 09:22:04 +00:00
assertBalance := func(t testing.TB, wallet Wallet, want Bitcoin) {
t.Helper()
2024-09-11 09:22:04 +00:00
got := wallet.Balance()
if got != want {
2024-09-11 09:32:40 +00:00
t.Errorf("got %q, want %q", got, want)
2024-09-11 09:22:04 +00:00
}
}
2024-09-11 09:26:39 +00:00
2024-09-11 09:32:40 +00:00
assertError := func(t testing.TB, got error, want string) {
t.Helper()
2024-09-11 09:32:40 +00:00
if got == nil {
t.Fatal("didn't get an error but wanted one")
}
if got.Error() != want {
t.Errorf("got %q, want %q", got, want)
}
}
2024-09-11 09:22:04 +00:00
t.Run("deposit", func(t *testing.T) {
wallet := Wallet{}
wallet.Deposit(10)
assertBalance(t, wallet, Bitcoin(10))
})
2024-09-11 09:26:39 +00:00
2024-09-11 09:22:04 +00:00
t.Run("withdraw", func(t *testing.T) {
wallet := Wallet{balance: Bitcoin(20)}
wallet.Withdraw(10)
assertBalance(t, wallet, Bitcoin(10))
})
2024-09-11 09:26:39 +00:00
t.Run("withdraw insufficient funds", func(t *testing.T) {
startingBalance := Bitcoin(20)
wallet := Wallet{balance: startingBalance}
err := wallet.Withdraw(100)
2024-09-11 09:32:40 +00:00
assertError(t, err, "not enough to withdraw")
assertBalance(t, wallet, startingBalance)
2024-09-11 09:26:39 +00:00
})
}