|
1 | 1 | local BankAccount = require('bank-account') |
2 | 2 |
|
3 | 3 | describe('bank-account', function() |
4 | | - it('should have a balance of zero after opening a new account', function() |
| 4 | + it('newly opened account has zero balance', function() |
5 | 5 | local account = BankAccount:new() |
| 6 | + account:open() |
6 | 7 | assert.equal(0, account:balance()) |
7 | 8 | end) |
8 | 9 |
|
9 | | - it('should allow deposits', function() |
| 10 | + it('single deposit', function() |
10 | 11 | local account = BankAccount:new() |
11 | | - account:deposit(45) |
12 | | - assert.equal(45, account:balance()) |
| 12 | + account:open() |
| 13 | + account:deposit(100) |
| 14 | + assert.equal(100, account:balance()) |
13 | 15 | end) |
14 | 16 |
|
15 | | - it('should allow multiple deposits', function() |
| 17 | + it('multiple deposits', function() |
16 | 18 | local account = BankAccount:new() |
17 | | - account:deposit(10) |
18 | | - account:deposit(25) |
19 | | - assert.equal(35, account:balance()) |
| 19 | + account:open() |
| 20 | + account:deposit(100) |
| 21 | + account:deposit(50) |
| 22 | + assert.equal(150, account:balance()) |
20 | 23 | end) |
21 | 24 |
|
22 | | - it('should require deposits to be positive', function() |
| 25 | + it('withdraw once', function() |
23 | 26 | local account = BankAccount:new() |
24 | | - assert.has.errors(function() |
25 | | - account:deposit(0) |
26 | | - end) |
27 | | - assert.has.errors(function() |
28 | | - account:deposit(-1) |
29 | | - end) |
| 27 | + account:open() |
| 28 | + account:deposit(100) |
| 29 | + account:withdraw(75) |
| 30 | + assert.equal(25, account:balance()) |
30 | 31 | end) |
31 | 32 |
|
32 | | - it('should allow withdrawals', function() |
| 33 | + it('withdraw twice', function() |
33 | 34 | local account = BankAccount:new() |
34 | | - account:deposit(20) |
35 | | - account:withdraw(10) |
36 | | - assert.equal(10, account:balance()) |
| 35 | + account:open() |
| 36 | + account:deposit(100) |
| 37 | + account:withdraw(80) |
| 38 | + account:withdraw(20) |
| 39 | + assert.equal(0, account:balance()) |
37 | 40 | end) |
38 | 41 |
|
39 | | - it('should allow multiple withdrawals', function() |
| 42 | + it('can do multiple operations sequentially', function() |
40 | 43 | local account = BankAccount:new() |
| 44 | + account:open() |
41 | 45 | account:deposit(100) |
42 | | - account:withdraw(10) |
43 | | - account:withdraw(25) |
44 | | - assert.equal(65, account:balance()) |
| 46 | + account:deposit(110) |
| 47 | + account:withdraw(200) |
| 48 | + account:deposit(60) |
| 49 | + account:withdraw(50) |
| 50 | + assert.equal(20, account:balance()) |
45 | 51 | end) |
46 | 52 |
|
47 | | - it('should require withdrawals to be positive', function() |
| 53 | + it('cannot check balance of closed account', function() |
48 | 54 | local account = BankAccount:new() |
49 | | - account:deposit(100) |
50 | | - assert.has.errors(function() |
51 | | - account:withdraw(0) |
52 | | - end) |
53 | | - assert.has.errors(function() |
54 | | - account:withdraw(-1) |
| 55 | + account:open() |
| 56 | + account:close() |
| 57 | + assert.has_error(function() |
| 58 | + account:balance() |
55 | 59 | end) |
56 | 60 | end) |
57 | 61 |
|
58 | | - it('should not allow accounts to be overdrawn', function() |
| 62 | + it('cannot deposit into closed account', function() |
59 | 63 | local account = BankAccount:new() |
60 | | - assert.has.errors(function() |
61 | | - account:withdraw(1) |
| 64 | + account:open() |
| 65 | + account:close() |
| 66 | + assert.has_error(function() |
| 67 | + account:deposit(50) |
62 | 68 | end) |
63 | 69 | end) |
64 | 70 |
|
65 | | - it('should allow multiple independent accounts to be created', function() |
66 | | - local account1 = BankAccount:new() |
67 | | - local account2 = BankAccount:new() |
68 | | - account1:deposit(100) |
69 | | - account2:deposit(42) |
70 | | - assert.are.equal(100, account1:balance()) |
71 | | - assert.are.equal(42, account2:balance()) |
| 71 | + it('cannot deposit into unopened account', function() |
| 72 | + local account = BankAccount:new() |
| 73 | + assert.has_error(function() |
| 74 | + account:deposit(50) |
| 75 | + end) |
72 | 76 | end) |
73 | 77 |
|
74 | | - it('should allow accounts to be closed', function() |
| 78 | + it('cannot withdraw from closed account', function() |
75 | 79 | local account = BankAccount:new() |
| 80 | + account:open() |
76 | 81 | account:close() |
| 82 | + assert.has_error(function() |
| 83 | + account:withdraw(50) |
| 84 | + end) |
77 | 85 | end) |
78 | 86 |
|
79 | | - it('should not allow deposits to a closed account', function() |
| 87 | + it('cannot close an account that was not opened', function() |
80 | 88 | local account = BankAccount:new() |
81 | | - account:close() |
82 | | - assert.has.errors(function() |
83 | | - account:deposit(1) |
| 89 | + assert.has_error(function() |
| 90 | + account:close() |
| 91 | + end) |
| 92 | + end) |
| 93 | + |
| 94 | + it('cannot open an already opened account', function() |
| 95 | + local account = BankAccount:new() |
| 96 | + account:open() |
| 97 | + assert.has_error(function() |
| 98 | + account:open() |
84 | 99 | end) |
85 | 100 | end) |
86 | 101 |
|
87 | | - it('should not allow withdrawals from a closed account', function() |
| 102 | + it('reopened account does not retain balance', function() |
88 | 103 | local account = BankAccount:new() |
89 | | - account:deposit(10) |
| 104 | + account:open() |
| 105 | + account:deposit(50) |
90 | 106 | account:close() |
91 | | - assert.has.errors(function() |
92 | | - account:withdraw(1) |
| 107 | + account:open() |
| 108 | + assert.equal(0, account:balance()) |
| 109 | + end) |
| 110 | + |
| 111 | + it('cannot withdraw more than deposited', function() |
| 112 | + local account = BankAccount:new() |
| 113 | + account:open() |
| 114 | + account:deposit(25) |
| 115 | + assert.has_error(function() |
| 116 | + account:withdraw(50) |
| 117 | + end) |
| 118 | + end) |
| 119 | + |
| 120 | + it('cannot withdraw negative', function() |
| 121 | + local account = BankAccount:new() |
| 122 | + account:open() |
| 123 | + account:deposit(100) |
| 124 | + assert.has_error(function() |
| 125 | + account:withdraw(-50) |
| 126 | + end) |
| 127 | + end) |
| 128 | + |
| 129 | + it('cannot deposit negative', function() |
| 130 | + local account = BankAccount:new() |
| 131 | + account:open() |
| 132 | + assert.has_error(function() |
| 133 | + account:deposit(-50) |
93 | 134 | end) |
94 | 135 | end) |
95 | 136 | end) |
0 commit comments