@@ -6,36 +6,17 @@ import (
6
6
7
7
func TestWallet (t * testing.T ) {
8
8
9
- assertBalance := func (t * testing.T , wallet Wallet , want Bitcoin ) {
10
- t .Helper ()
11
- got := wallet .Balance ()
12
-
13
- if got != want {
14
- t .Errorf ("got %s want %s" , got , want )
15
- }
16
- }
17
-
18
- assertError := func (t * testing.T , got error , want error ) {
19
- t .Helper ()
20
- if got == nil {
21
- t .Fatal ("wanted an error but didn't get one" )
22
- }
23
-
24
- if got != want {
25
- t .Errorf ("got %q, want %q" , got , want )
26
- }
27
- }
28
-
29
9
t .Run ("Deposit" , func (t * testing.T ) {
30
10
wallet := Wallet {}
31
11
wallet .Deposit (Bitcoin (10 ))
32
12
assertBalance (t , wallet , Bitcoin (10 ))
33
13
})
34
14
35
- t .Run ("Withdraw" , func (t * testing.T ) {
15
+ t .Run ("Withdraw with funds " , func (t * testing.T ) {
36
16
wallet := Wallet {balance : Bitcoin (20 )}
37
- wallet .Withdraw (Bitcoin (10 ))
17
+ err := wallet .Withdraw (Bitcoin (10 ))
38
18
assertBalance (t , wallet , Bitcoin (10 ))
19
+ assertNoError (t , err )
39
20
})
40
21
41
22
t .Run ("Withdraw insufficient funds" , func (t * testing.T ) {
@@ -46,3 +27,30 @@ func TestWallet(t *testing.T) {
46
27
assertError (t , err , ErrInsufficientFunds )
47
28
})
48
29
}
30
+
31
+ func assertBalance (t * testing.T , wallet Wallet , want Bitcoin ) {
32
+ t .Helper ()
33
+ got := wallet .Balance ()
34
+
35
+ if got != want {
36
+ t .Errorf ("got %s want %s" , got , want )
37
+ }
38
+ }
39
+
40
+ func assertNoError (t * testing.T , got error ) {
41
+ t .Helper ()
42
+ if got != nil {
43
+ t .Fatal ("got an error but didn't want one" )
44
+ }
45
+ }
46
+
47
+ func assertError (t * testing.T , got error , want error ) {
48
+ t .Helper ()
49
+ if got == nil {
50
+ t .Fatal ("wanted an error but didn't get one" )
51
+ }
52
+
53
+ if got != want {
54
+ t .Errorf ("got %q, want %q" , got , want )
55
+ }
56
+ }
0 commit comments