6 if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){
10 plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?';
19 # =begin testing SETUP
25 has 'balance' => ( isa => 'Int', is => 'rw', default => 0 );
28 my ( $self, $amount ) = @_;
29 $self->balance( $self->balance + $amount );
33 my ( $self, $amount ) = @_;
34 my $current_balance = $self->balance();
35 ( $current_balance >= $amount )
36 || confess "Account overdrawn";
37 $self->balance( $current_balance - $amount );
40 package CheckingAccount;
43 extends 'BankAccount';
45 has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
47 before 'withdraw' => sub {
48 my ( $self, $amount ) = @_;
49 my $overdraft_amount = $amount - $self->balance();
50 if ( $self->overdraft_account && $overdraft_amount > 0 ) {
51 $self->overdraft_account->withdraw($overdraft_amount);
52 $self->deposit($overdraft_amount);
64 $savings_account = BankAccount->new( balance => 250 );
65 isa_ok( $savings_account, 'BankAccount' );
67 is( $savings_account->balance, 250, '... got the right savings balance' );
69 $savings_account->withdraw(50);
71 '... withdrew from savings successfully';
72 is( $savings_account->balance, 200,
73 '... got the right savings balance after withdrawl' );
75 $savings_account->deposit(150);
76 is( $savings_account->balance, 350,
77 '... got the right savings balance after deposit' );
81 my $checking_account = CheckingAccount->new(
83 overdraft_account => $savings_account
85 isa_ok( $checking_account, 'CheckingAccount' );
86 isa_ok( $checking_account, 'BankAccount' );
88 is( $checking_account->overdraft_account, $savings_account,
89 '... got the right overdraft account' );
91 is( $checking_account->balance, 100,
92 '... got the right checkings balance' );
95 $checking_account->withdraw(50);
97 '... withdrew from checking successfully';
98 is( $checking_account->balance, 50,
99 '... got the right checkings balance after withdrawl' );
100 is( $savings_account->balance, 350,
101 '... got the right savings balance after checking withdrawl (no overdraft)'
105 $checking_account->withdraw(200);
107 '... withdrew from checking successfully';
108 is( $checking_account->balance, 0,
109 '... got the right checkings balance after withdrawl' );
110 is( $savings_account->balance, 200,
111 '... got the right savings balance after overdraft withdrawl' );
115 my $checking_account = CheckingAccount->new(
118 # no overdraft account
120 isa_ok( $checking_account, 'CheckingAccount' );
121 isa_ok( $checking_account, 'BankAccount' );
123 is( $checking_account->overdraft_account, undef,
124 '... no overdraft account' );
126 is( $checking_account->balance, 100,
127 '... got the right checkings balance' );
130 $checking_account->withdraw(50);
132 '... withdrew from checking successfully';
133 is( $checking_account->balance, 50,
134 '... got the right checkings balance after withdrawl' );
137 $checking_account->withdraw(200);
139 '... withdrawl failed due to attempted overdraft';
140 is( $checking_account->balance, 50,
141 '... got the right checkings balance after withdrawl failure' );