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