Remove shebangs from tests.
[gitmo/Class-MOP.git] / t / 017_add_method_modifier.t
CommitLineData
ddc8edba 1use strict;
2use warnings;
3
efd3d14c 4use Test::More tests => 19;
ddc8edba 5use Test::Exception;
6
efd3d14c 7use 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
67my $savings_account = BankAccount->new(balance => 250);
68isa_ok($savings_account, 'BankAccount');
69
70is($savings_account->balance, 250, '... got the right savings balance');
71lives_ok {
72 $savings_account->withdraw(50);
73} '... withdrew from savings successfully';
74is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
195f5bf8 75dies_ok {
76 $savings_account->withdraw(250);
77} '... could not withdraw from savings successfully';
78
ddc8edba 79
80$savings_account->deposit(150);
81is($savings_account->balance, 350, '... got the right savings balance after deposit');
82
83my $checking_account = CheckingAccount->new(
84 balance => 100,
85 overdraft => $savings_account
86 );
87isa_ok($checking_account, 'CheckingAccount');
88isa_ok($checking_account, 'BankAccount');
89
90is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
91
92is($checking_account->balance, 100, '... got the right checkings balance');
93
94lives_ok {
96ceced8 95 $checking_account->withdraw(50);
ddc8edba 96} '... withdrew from checking successfully';
96ceced8 97is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
98is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
ddc8edba 99
96ceced8 100lives_ok {
101 $checking_account->withdraw(200);
102} '... withdrew from checking successfully';
ddc8edba 103is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
96ceced8 104is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');
ddc8edba 105