1
exercism fetch lua bank-account

bank-account_spec.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local BankAccount = require('bank-account')

describe('bank-ccount', function()
  it('should have a balance of zero after opening a new account', function()
    local account = BankAccount:new()
    assert.equal(0, account:balance())
  end)

  it('should allow deposits', function()
    local account = BankAccount:new()
    account:deposit(45)
    assert.equal(45, account:balance())
  end)

  it('should allow multiple deposits', function()
    local account = BankAccount:new()
    account:deposit(10)
    account:deposit(25)
    assert.equal(35, account:balance())
  end)

  it('should require deposits to be positive', function()
    local account = BankAccount:new()
    assert.has.errors(function()
      account:deposit(0)
    end)
    assert.has.errors(function()
      account:deposit(-1)
    end)
  end)

  it('should allow withdrawals', function()
    local account = BankAccount:new()
    account:deposit(20)
    account:withdraw(10)
    assert.equal(10, account:balance())
  end)

  it('should allow multiple withdrawals', function()
    local account = BankAccount:new()
    account:deposit(100)
    account:withdraw(10)
    account:withdraw(25)
    assert.equal(65, account:balance())
  end)

  it('should require withdrawals to be positive', function()
    local account = BankAccount:new()
    account:deposit(100)
    assert.has.errors(function()
      account:withdraw(0)
    end)
    assert.has.errors(function()
      account:withdraw(-1)
    end)
  end)

  it('should not allow accounts to be overdrawn', function()
    local account = BankAccount:new()
    assert.has.errors(function()
      account:withdraw(1)
    end)
  end)

  it('should allow multiple independent accounts to be created', function()
    local account1 = BankAccount:new()
    local account2 = BankAccount:new()
    account1:deposit(100)
    account2:deposit(42)
    assert.are.equal(100, account1:balance())
    assert.are.equal(42, account2:balance())
  end)

  it('should allow accounts to be closed', function()
    local account = BankAccount:new()
    account:close()
  end)

  it('should now allow deposits to a closed account', function()
    local account = BankAccount:new()
    account:close()
    assert.has.errors(function()
      account:deposit(1)
    end)
  end)

  it('should not allow withdrawals from a closed account', function()
    local account = BankAccount:new()
    account:deposit(10)
    account:close()
    assert.has.errors(function()
      account:withdraw(1)
    end)
  end)
end)