got rid of all the use_ok junk except for 000_load.t
[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 => 19;
7 use Test::Exception;
8
9 use Class::MOP;
10
11 {
12     package BankAccount;
13     
14     use strict;
15     use warnings;
16     use metaclass;
17         
18     use Carp 'confess';
19     
20     BankAccount->meta->add_attribute('balance' => (
21         accessor => 'balance',
22                 init_arg => 'balance',
23         default  => 0
24     ));
25     
26     sub new { (shift)->meta->new_object(@_) }
27
28     sub deposit {
29         my ($self, $amount) = @_;
30         $self->balance($self->balance + $amount);
31     }
32     
33     sub withdraw {
34         my ($self, $amount) = @_;
35         my $current_balance = $self->balance();
36         ($current_balance >= $amount)
37             || confess "Account overdrawn";
38         $self->balance($current_balance - $amount);
39     }
40
41         package CheckingAccount;
42         
43         use strict;
44         use warnings;
45     use metaclass;      
46
47         use base 'BankAccount';
48         
49     CheckingAccount->meta->add_attribute('overdraft_account' => (
50         accessor => 'overdraft_account',
51                 init_arg => 'overdraft',
52     )); 
53
54         CheckingAccount->meta->add_before_method_modifier('withdraw' => sub {
55                 my ($self, $amount) = @_;
56                 my $overdraft_amount = $amount - $self->balance();
57                 if ($overdraft_amount > 0) {
58                         $self->overdraft_account->withdraw($overdraft_amount);
59                         $self->deposit($overdraft_amount);
60                 }
61         });
62
63         ::ok(CheckingAccount->meta->has_method('withdraw'), '... checking account now has a withdraw method');
64         ::isa_ok(CheckingAccount->meta->get_method('withdraw'), 'Class::MOP::Method::Wrapped');
65         ::isa_ok(BankAccount->meta->get_method('withdraw'), 'Class::MOP::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 dies_ok {
78         $savings_account->withdraw(250);
79 } '... could not withdraw from savings successfully';
80
81
82 $savings_account->deposit(150);
83 is($savings_account->balance, 350, '... got the right savings balance after deposit');
84
85 my $checking_account = CheckingAccount->new(
86                                                         balance   => 100,
87                                                         overdraft => $savings_account
88                                                 );
89 isa_ok($checking_account, 'CheckingAccount');
90 isa_ok($checking_account, 'BankAccount');
91
92 is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
93
94 is($checking_account->balance, 100, '... got the right checkings balance');
95
96 lives_ok {
97         $checking_account->withdraw(50);
98 } '... withdrew from checking successfully';
99 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
100 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
101
102 lives_ok {
103         $checking_account->withdraw(200);
104 } '... withdrew from checking successfully';
105 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
106 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');
107