Commit | Line | Data |
ddc8edba |
1 | use strict; |
2 | use warnings; |
3 | |
efd3d14c |
4 | use Test::More tests => 19; |
ddc8edba |
5 | use Test::Exception; |
6 | |
efd3d14c |
7 | use Class::MOP; |
ddc8edba |
8 | |
9 | { |
10 | package BankAccount; |
11 | |
12 | use strict; |
13 | use warnings; |
14 | use metaclass; |
15 | |
16 | use Carp 'confess'; |
17 | |
1aeb4c53 |
18 | BankAccount->meta->add_attribute('balance' => ( |
ddc8edba |
19 | accessor => 'balance', |
20 | init_arg => 'balance', |
21 | default => 0 |
22 | )); |
23 | |
24 | sub new { (shift)->meta->new_object(@_) } |
96ceced8 |
25 | |
ddc8edba |
26 | sub deposit { |
27 | my ($self, $amount) = @_; |
ddc8edba |
28 | $self->balance($self->balance + $amount); |
29 | } |
30 | |
31 | sub withdraw { |
a4258ffd |
32 | my ($self, $amount) = @_; |
33 | my $current_balance = $self->balance(); |
34 | ($current_balance >= $amount) |
ddc8edba |
35 | || confess "Account overdrawn"; |
ddc8edba |
36 | $self->balance($current_balance - $amount); |
37 | } |
38 | |
39 | package CheckingAccount; |
40 | |
41 | use strict; |
42 | use warnings; |
96ceced8 |
43 | use metaclass; |
44 | |
ddc8edba |
45 | use base 'BankAccount'; |
46 | |
1aeb4c53 |
47 | CheckingAccount->meta->add_attribute('overdraft_account' => ( |
ddc8edba |
48 | accessor => 'overdraft_account', |
49 | init_arg => 'overdraft', |
50 | )); |
51 | |
a4258ffd |
52 | CheckingAccount->meta->add_before_method_modifier('withdraw' => sub { |
ddc8edba |
53 | my ($self, $amount) = @_; |
ddc8edba |
54 | my $overdraft_amount = $amount - $self->balance(); |
55 | if ($overdraft_amount > 0) { |
ddc8edba |
56 | $self->overdraft_account->withdraw($overdraft_amount); |
57 | $self->deposit($overdraft_amount); |
58 | } |
ddc8edba |
59 | }); |
60 | |
61 | ::ok(CheckingAccount->meta->has_method('withdraw'), '... checking account now has a withdraw method'); |
195f5bf8 |
62 | ::isa_ok(CheckingAccount->meta->get_method('withdraw'), 'Class::MOP::Method::Wrapped'); |
63 | ::isa_ok(BankAccount->meta->get_method('withdraw'), 'Class::MOP::Method'); |
ddc8edba |
64 | } |
65 | |
66 | |
67 | my $savings_account = BankAccount->new(balance => 250); |
68 | isa_ok($savings_account, 'BankAccount'); |
69 | |
70 | is($savings_account->balance, 250, '... got the right savings balance'); |
71 | lives_ok { |
72 | $savings_account->withdraw(50); |
73 | } '... withdrew from savings successfully'; |
74 | is($savings_account->balance, 200, '... got the right savings balance after withdrawl'); |
195f5bf8 |
75 | dies_ok { |
76 | $savings_account->withdraw(250); |
77 | } '... could not withdraw from savings successfully'; |
78 | |
ddc8edba |
79 | |
80 | $savings_account->deposit(150); |
81 | is($savings_account->balance, 350, '... got the right savings balance after deposit'); |
82 | |
83 | my $checking_account = CheckingAccount->new( |
84 | balance => 100, |
85 | overdraft => $savings_account |
86 | ); |
87 | isa_ok($checking_account, 'CheckingAccount'); |
88 | isa_ok($checking_account, 'BankAccount'); |
89 | |
90 | is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account'); |
91 | |
92 | is($checking_account->balance, 100, '... got the right checkings balance'); |
93 | |
94 | lives_ok { |
96ceced8 |
95 | $checking_account->withdraw(50); |
ddc8edba |
96 | } '... withdrew from checking successfully'; |
96ceced8 |
97 | is($checking_account->balance, 50, '... got the right checkings balance after withdrawl'); |
98 | is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)'); |
ddc8edba |
99 | |
96ceced8 |
100 | lives_ok { |
101 | $checking_account->withdraw(200); |
102 | } '... withdrew from checking successfully'; |
ddc8edba |
103 | is($checking_account->balance, 0, '... got the right checkings balance after withdrawl'); |
96ceced8 |
104 | is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl'); |
ddc8edba |
105 | |