1
exercism fetch erlang bank-account

test/bank_account_tests.erl

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
-module(bank_account_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

create_test() ->
  BankAccount = bank_account:create(),
  ?assert(bank_account:balance(BankAccount) =:= 0).

close_account_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 1),
  Amount = bank_account:close(BankAccount),
  ?assert(Amount =:= 1),
  ?assertEqual({error, account_closed}, bank_account:balance( BankAccount )).

deposit_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 1),
  ?assert(bank_account:balance(BankAccount) =:= 1).

deposit_fail_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, -1),
  ?assert(bank_account:balance(BankAccount) =:= 0).

deposit_many_test() ->
  BankAccount = bank_account:create(),
  [erlang:spawn( fun () -> bank_account:deposit(BankAccount, X) end ) || X <- lists:seq(1, 10)],
  timer:sleep(100),
  Last = bank_account:balance(BankAccount),
  ?assert(Last =:= 55).

withdraw_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10 ),
  Amount = bank_account:withdraw(BankAccount, 1),
  ?assert(Amount =:= 1),
  ?assert(bank_account:balance(BankAccount) =:= 9).

withdraw_fail_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:withdraw(BankAccount, -1),
  ?assert(Amount =:= 0),
  ?assert(bank_account:balance(BankAccount) =:= 10).

withdraw_excessive_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10 ),
  Amount = bank_account:withdraw(BankAccount, 20),
  ?assert(Amount =:= 10),
  ?assert(bank_account:balance(BankAccount) =:= 0).

withdraw_many_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 55 ),
  [erlang:spawn( fun () -> bank_account:withdraw(BankAccount, X) end ) || X <- lists:seq(1, 10)],
  timer:sleep(100),
  Last = bank_account:balance(BankAccount),
  ?assert(Last =:= 0).

charge_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:charge(BankAccount, 2),
  ?assert(Amount =:= 2),
  ?assert(bank_account:balance(BankAccount) =:= 8).

charge_fail_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:charge(BankAccount, -2),
  ?assert(Amount =:= 0),
  ?assert(bank_account:balance(BankAccount) =:= 10).

charge_excessive_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 10),
  Amount = bank_account:charge(BankAccount, 20),
  ?assert(Amount =:= 0),
  ?assert(bank_account:balance(BankAccount) =:= 10).

charge_many_test() ->
  BankAccount = bank_account:create(),
  bank_account:deposit(BankAccount, 55 ),
  [erlang:spawn( fun () -> bank_account:charge(BankAccount, 10) end ) || _X <- lists:seq(1, 10)],
  timer:sleep(100),
  Last = bank_account:balance(BankAccount),
  ?assert(Last =:= 5).

version_test() ->
  ?assertMatch(1, bank_account:test_version()).