master
1package main
2
3import (
4 "fmt"
5 "testing"
6)
7
8func TestWallet(t *testing.T) {
9 t.Run("Deposit", func(t *testing.T) {
10 wallet := Wallet{}
11 wallet.Deposit(Bitcoin(10))
12 fmt.Printf("address of balance in test is %v \n", &wallet.balance)
13 assertBalance(t, wallet, Bitcoin(10))
14 })
15
16 t.Run("Withdraw with funds", func(t *testing.T) {
17 wallet := Wallet{balance: Bitcoin(20)}
18 err := wallet.Withdraw(Bitcoin(10))
19 assertBalance(t, wallet, Bitcoin(10))
20 assertNoError(t, err)
21 })
22
23 t.Run("Withdraw insufficient funds", func(t *testing.T) {
24 wallet := Wallet{balance: Bitcoin(20)}
25 err := wallet.Withdraw(Bitcoin(100))
26 assertBalance(t, wallet, Bitcoin(20))
27 assertError(t, err, InsufficientFundsError)
28 })
29}
30
31func assertBalance(t *testing.T, wallet Wallet, want Bitcoin) {
32 t.Helper()
33 got := wallet.Balance()
34 if got != want {
35 t.Errorf("got %s want %s", got, want)
36 }
37}
38
39func assertError(t *testing.T, got error, want error) {
40 t.Helper()
41 if got == nil {
42 t.Fatal("wanted an error but didn't get one")
43 }
44 if got != want {
45 t.Errorf("got %q, want %q", got, want)
46 }
47}
48
49func assertNoError(t *testing.T, got error) {
50 t.Helper()
51 if got != nil {
52 t.Fatal("wanted an error but didn't get one")
53 }
54}