pointers
This commit is contained in:
parent
0e7bf6a805
commit
f2da4c1faa
35
pointers/wallet.go
Normal file
35
pointers/wallet.go
Normal file
@ -0,0 +1,35 @@
|
||||
package wallet
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Bitcoin int
|
||||
|
||||
func (b Bitcoin) String() string {
|
||||
return fmt.Sprintf("%d BTC", b)
|
||||
}
|
||||
|
||||
type Wallet struct{
|
||||
balance Bitcoin
|
||||
}
|
||||
|
||||
func (w *Wallet) Deposit(amount Bitcoin) {
|
||||
w.balance += amount
|
||||
}
|
||||
|
||||
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds")
|
||||
|
||||
func (w *Wallet) Withdraw(amount Bitcoin) error {
|
||||
if amount > w.balance {
|
||||
return ErrInsufficientFunds
|
||||
}
|
||||
|
||||
w.balance -= amount
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Wallet) Balance() Bitcoin {
|
||||
return w.balance
|
||||
}
|
51
pointers/wallet_test.go
Normal file
51
pointers/wallet_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package wallet
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
assertNoError := func(t testing.TB, got error) {
|
||||
t.Helper()
|
||||
if got != nil {
|
||||
t.Fatal("got an error but didn't want one")
|
||||
}
|
||||
}
|
||||
|
||||
assertError := func(t testing.TB, got, 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)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("deposit", func(t *testing.T) {
|
||||
wallet := Wallet{}
|
||||
wallet.Deposit(Bitcoin(10))
|
||||
assertBalance(t, wallet, Bitcoin(10))
|
||||
})
|
||||
t.Run("withdraw with funds", 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 insufficient funds", func(t *testing.T) {
|
||||
startingBalance := Bitcoin(20)
|
||||
wallet := Wallet{startingBalance}
|
||||
err := wallet.Withdraw(Bitcoin(100))
|
||||
assertError(t, err, ErrInsufficientFunds)
|
||||
assertBalance(t, wallet, startingBalance)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user