2024-09-11 09:08:53 +00:00
|
|
|
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:08:53 +00:00
|
|
|
|
2024-09-11 09:22:04 +00:00
|
|
|
got := wallet.Balance()
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got %q want %q", got, want)
|
|
|
|
}
|
2024-09-11 09:08:53 +00:00
|
|
|
}
|
2024-09-11 09:26:39 +00:00
|
|
|
|
2024-09-11 09:28:45 +00:00
|
|
|
assertError := func(t testing.TB, err error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("wanted an error but didn't get one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:28:45 +00:00
|
|
|
assertError(t, err)
|
|
|
|
assertBalance(t, wallet, startingBalance)
|
2024-09-11 09:26:39 +00:00
|
|
|
})
|
2024-09-11 09:08:53 +00:00
|
|
|
}
|