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