Skip to content

Commit cc28783

Browse files
authored
Update bank-account tests (#581)
1 parent 8dc4e9c commit cc28783

File tree

4 files changed

+197
-57
lines changed

4 files changed

+197
-57
lines changed

exercises/practice/bank-account/.meta/example.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
local BankAccount = {}
22

3+
function BankAccount:open()
4+
assert(not self._open)
5+
self._open = true
6+
self._balance = 0
7+
end
8+
39
function BankAccount:balance()
4-
return self._value
10+
assert(self._open)
11+
return self._balance
512
end
613

714
function BankAccount:deposit(amount)
8-
assert(not self._closed)
15+
assert(self._open)
916
assert(amount > 0)
10-
self._value = self._value + amount
17+
self._balance = self._balance + amount
1118
end
1219

1320
function BankAccount:withdraw(amount)
14-
assert(not self._closed)
21+
assert(self._open)
1522
assert(amount > 0)
16-
assert(self._value - amount > 0)
17-
self._value = self._value - amount
23+
assert(self._balance - amount >= 0)
24+
self._balance = self._balance - amount
1825
end
1926

2027
function BankAccount:close(amount)
21-
self._closed = true
28+
assert(self._open)
29+
self._open = false
2230
end
2331

2432
function BankAccount:new()
25-
local o = setmetatable({ _value = 0 }, self)
33+
local o = setmetatable({ _balance = 0, _open = false }, self)
2634
self.__index = self
2735
return o
2836
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local function render_operation(operation)
2+
if operation.amount then
3+
return 'account:' .. operation.operation .. '(' .. operation.amount .. ')'
4+
else
5+
return 'account:' .. operation.operation .. '()'
6+
end
7+
end
8+
9+
return {
10+
module_name = 'BankAccount',
11+
12+
generate_test = function(case)
13+
local lines = { 'local account = BankAccount:new()' }
14+
15+
for i, operation in ipairs(case.input.operations) do
16+
if i == #case.input.operations then
17+
if type(case.expected) == 'table' and case.expected.error then
18+
table.insert(lines, 'assert.has_error(function() ' .. render_operation(operation) .. ' end)')
19+
else
20+
table.insert(lines, 'assert.equal(' .. case.expected .. ', ' .. render_operation(operation) .. ')')
21+
end
22+
else
23+
table.insert(lines, render_operation(operation))
24+
end
25+
end
26+
27+
return table.concat(lines, '\n')
28+
end
29+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[983a1528-4ceb-45e5-8257-8ce01aceb5ed]
13+
description = "Newly opened account has zero balance"
14+
15+
[e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]
16+
description = "Single deposit"
17+
18+
[3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]
19+
description = "Multiple deposits"
20+
21+
[08f1af07-27ae-4b38-aa19-770bde558064]
22+
description = "Withdraw once"
23+
24+
[6f6d242f-8c31-4ac6-8995-a90d42cad59f]
25+
description = "Withdraw twice"
26+
27+
[45161c94-a094-4c77-9cec-998b70429bda]
28+
description = "Can do multiple operations sequentially"
29+
30+
[f9facfaa-d824-486e-8381-48832c4bbffd]
31+
description = "Cannot check balance of closed account"
32+
33+
[7a65ba52-e35c-4fd2-8159-bda2bde6e59c]
34+
description = "Cannot deposit into closed account"
35+
36+
[a0a1835d-faae-4ad4-a6f3-1fcc2121380b]
37+
description = "Cannot deposit into unopened account"
38+
39+
[570dfaa5-0532-4c1f-a7d3-0f65c3265608]
40+
description = "Cannot withdraw from closed account"
41+
42+
[c396d233-1c49-4272-98dc-7f502dbb9470]
43+
description = "Cannot close an account that was not opened"
44+
45+
[c06f534f-bdc2-4a02-a388-1063400684de]
46+
description = "Cannot open an already opened account"
47+
48+
[0722d404-6116-4f92-ba3b-da7f88f1669c]
49+
description = "Reopened account does not retain balance"
50+
51+
[ec42245f-9361-4341-8231-a22e8d19c52f]
52+
description = "Cannot withdraw more than deposited"
53+
54+
[4f381ef8-10ef-4507-8e1d-0631ecc8ee72]
55+
description = "Cannot withdraw negative"
56+
57+
[d45df9ea-1db0-47f3-b18c-d365db49d938]
58+
description = "Cannot deposit negative"
59+
60+
[ba0c1e0b-0f00-416f-8097-a7dfc97871ff]
61+
description = "Can handle concurrent transactions"
62+
include = false
Lines changed: 90 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,136 @@
11
local BankAccount = require('bank-account')
22

33
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()
55
local account = BankAccount:new()
6+
account:open()
67
assert.equal(0, account:balance())
78
end)
89

9-
it('should allow deposits', function()
10+
it('single deposit', function()
1011
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())
1315
end)
1416

15-
it('should allow multiple deposits', function()
17+
it('multiple deposits', function()
1618
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())
2023
end)
2124

22-
it('should require deposits to be positive', function()
25+
it('withdraw once', function()
2326
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())
3031
end)
3132

32-
it('should allow withdrawals', function()
33+
it('withdraw twice', function()
3334
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())
3740
end)
3841

39-
it('should allow multiple withdrawals', function()
42+
it('can do multiple operations sequentially', function()
4043
local account = BankAccount:new()
44+
account:open()
4145
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())
4551
end)
4652

47-
it('should require withdrawals to be positive', function()
53+
it('cannot check balance of closed account', function()
4854
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()
5559
end)
5660
end)
5761

58-
it('should not allow accounts to be overdrawn', function()
62+
it('cannot deposit into closed account', function()
5963
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)
6268
end)
6369
end)
6470

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)
7276
end)
7377

74-
it('should allow accounts to be closed', function()
78+
it('cannot withdraw from closed account', function()
7579
local account = BankAccount:new()
80+
account:open()
7681
account:close()
82+
assert.has_error(function()
83+
account:withdraw(50)
84+
end)
7785
end)
7886

79-
it('should not allow deposits to a closed account', function()
87+
it('cannot close an account that was not opened', function()
8088
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()
8499
end)
85100
end)
86101

87-
it('should not allow withdrawals from a closed account', function()
102+
it('reopened account does not retain balance', function()
88103
local account = BankAccount:new()
89-
account:deposit(10)
104+
account:open()
105+
account:deposit(50)
90106
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)
93134
end)
94135
end)
95136
end)

0 commit comments

Comments
 (0)