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