54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestWallet(t *testing.T) {
|
|
assertBalance := func(t testing.TB, wallet Wallet, want Bitcoin) {
|
|
t.Helper()
|
|
got := wallet.Balance()
|
|
|
|
if got != want {
|
|
t.Errorf("got %s want %s", got, want)
|
|
}
|
|
}
|
|
|
|
assertError := func(t testing.TB, got error, want error) {
|
|
t.Helper()
|
|
if got == nil {
|
|
t.Fatal("wanted an error but didn't get one")
|
|
}
|
|
|
|
if got != want {
|
|
t.Errorf("got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
assertNoError := func(t testing.TB, got error) {
|
|
t.Helper()
|
|
if got != nil {
|
|
t.Fatal("got an error but didn't want one")
|
|
}
|
|
}
|
|
|
|
t.Run("Deposit", func(t *testing.T) {
|
|
wallet := Wallet{}
|
|
wallet.Deposit(Bitcoin(10))
|
|
assertBalance(t, wallet, Bitcoin(10))
|
|
})
|
|
|
|
t.Run("Withdraw", func(t *testing.T) {
|
|
wallet := Wallet{balance: Bitcoin(20)}
|
|
err := wallet.Withdraw(Bitcoin(10))
|
|
assertNoError(t, err)
|
|
assertBalance(t, wallet, Bitcoin(10))
|
|
})
|
|
|
|
t.Run("Withdraw error", func(t *testing.T) {
|
|
startingBalance := Bitcoin(20)
|
|
wallet := Wallet{balance: startingBalance}
|
|
err := wallet.Withdraw(Bitcoin(30))
|
|
assertError(t, err, ErrInsufficientFunds)
|
|
assertBalance(t, wallet, startingBalance)
|
|
})
|
|
}
|