X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F017_add_method_modifier.t;h=37ecf5ccbf48781fcbe200c376208b0fdb319f17;hb=70df4709bf61592d5dfdcaa53b2a80b18f6bc9c3;hp=7ac25f6b20a5e79d4eaf167e9d4d55c56e8dd56d;hpb=96ceced87583646c1396bba4fdfa92d0b6c37058;p=gitmo%2FClass-MOP.git diff --git a/t/017_add_method_modifier.t b/t/017_add_method_modifier.t index 7ac25f6..37ecf5c 100644 --- a/t/017_add_method_modifier.t +++ b/t/017_add_method_modifier.t @@ -1,103 +1,138 @@ -#!/usr/bin/perl - use strict; use warnings; -use Test::More tests => 17; +use Test::More tests => 21; use Test::Exception; -BEGIN { - use_ok('Class::MOP'); -} +use Class::MOP; { + package BankAccount; - + use strict; use warnings; use metaclass; - + use Carp 'confess'; - - BankAccount->meta->add_attribute('$:balance' => ( - accessor => 'balance', - init_arg => 'balance', - default => 0 - )); - + + BankAccount->meta->add_attribute( + 'balance' => ( + accessor => 'balance', + init_arg => 'balance', + default => 0 + ) + ); + sub new { (shift)->meta->new_object(@_) } sub deposit { - my ($self, $amount) = @_; - $self->balance($self->balance + $amount); + my ( $self, $amount ) = @_; + $self->balance( $self->balance + $amount ); } - + sub withdraw { - my ($self, $amount) = @_; + my ( $self, $amount ) = @_; my $current_balance = $self->balance(); - ($current_balance >= $amount) + ( $current_balance >= $amount ) || confess "Account overdrawn"; - $self->balance($current_balance - $amount); + $self->balance( $current_balance - $amount ); } - package CheckingAccount; - - use strict; - use warnings; - use metaclass; - - use base 'BankAccount'; - - CheckingAccount->meta->add_attribute('$:overdraft_account' => ( - accessor => 'overdraft_account', - init_arg => 'overdraft', - )); - - CheckingAccount->meta->add_before_method_modifier('withdraw' => sub { - my ($self, $amount) = @_; - my $overdraft_amount = $amount - $self->balance(); - if ($overdraft_amount > 0) { - $self->overdraft_account->withdraw($overdraft_amount); - $self->deposit($overdraft_amount); - } - }); - - ::ok(CheckingAccount->meta->has_method('withdraw'), '... checking account now has a withdraw method'); -} + package CheckingAccount; + use strict; + use warnings; + use metaclass; + + use base 'BankAccount'; + + CheckingAccount->meta->add_attribute( + 'overdraft_account' => ( + accessor => 'overdraft_account', + init_arg => 'overdraft', + ) + ); + + CheckingAccount->meta->add_before_method_modifier( + 'withdraw' => sub { + my ( $self, $amount ) = @_; + my $overdraft_amount = $amount - $self->balance(); + if ( $overdraft_amount > 0 ) { + $self->overdraft_account->withdraw($overdraft_amount); + $self->deposit($overdraft_amount); + } + } + ); + + ::throws_ok( + sub { + CheckingAccount->meta->add_before_method_modifier( + 'does_not_exist' => sub { } ); + }, + qr/\QThe method 'does_not_exist' was not found in the inheritance hierarchy for CheckingAccount/ + ); + + ::ok( CheckingAccount->meta->has_method('withdraw'), + '... checking account now has a withdraw method' ); + ::isa_ok( CheckingAccount->meta->get_method('withdraw'), + 'Class::MOP::Method::Wrapped' ); + ::isa_ok( BankAccount->meta->get_method('withdraw'), + 'Class::MOP::Method' ); + + CheckingAccount->meta->add_method( foo => sub { 'foo' } ); + CheckingAccount->meta->add_before_method_modifier( foo => sub { 'wrapped' } ); + ::isa_ok( CheckingAccount->meta->get_method('foo'), + 'Class::MOP::Method::Wrapped' ); +} -my $savings_account = BankAccount->new(balance => 250); -isa_ok($savings_account, 'BankAccount'); +my $savings_account = BankAccount->new( balance => 250 ); +isa_ok( $savings_account, 'BankAccount' ); -is($savings_account->balance, 250, '... got the right savings balance'); +is( $savings_account->balance, 250, '... got the right savings balance' ); lives_ok { - $savings_account->withdraw(50); -} '... withdrew from savings successfully'; -is($savings_account->balance, 200, '... got the right savings balance after withdrawl'); + $savings_account->withdraw(50); +} +'... withdrew from savings successfully'; +is( $savings_account->balance, 200, + '... got the right savings balance after withdrawal' ); +dies_ok { + $savings_account->withdraw(250); +} +'... could not withdraw from savings successfully'; $savings_account->deposit(150); -is($savings_account->balance, 350, '... got the right savings balance after deposit'); +is( $savings_account->balance, 350, + '... got the right savings balance after deposit' ); my $checking_account = CheckingAccount->new( - balance => 100, - overdraft => $savings_account - ); -isa_ok($checking_account, 'CheckingAccount'); -isa_ok($checking_account, 'BankAccount'); + balance => 100, + overdraft => $savings_account +); +isa_ok( $checking_account, 'CheckingAccount' ); +isa_ok( $checking_account, 'BankAccount' ); -is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account'); +is( $checking_account->overdraft_account, $savings_account, + '... got the right overdraft account' ); -is($checking_account->balance, 100, '... got the right checkings balance'); +is( $checking_account->balance, 100, '... got the right checkings balance' ); lives_ok { - $checking_account->withdraw(50); -} '... withdrew from checking successfully'; -is($checking_account->balance, 50, '... got the right checkings balance after withdrawl'); -is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)'); + $checking_account->withdraw(50); +} +'... withdrew from checking successfully'; +is( $checking_account->balance, 50, + '... got the right checkings balance after withdrawal' ); +is( $savings_account->balance, 350, + '... got the right savings balance after checking withdrawal (no overdraft)' +); lives_ok { - $checking_account->withdraw(200); -} '... withdrew from checking successfully'; -is($checking_account->balance, 0, '... got the right checkings balance after withdrawl'); -is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl'); + $checking_account->withdraw(200); +} +'... withdrew from checking successfully'; +is( $checking_account->balance, 0, + '... got the right checkings balance after withdrawal' ); +is( $savings_account->balance, 200, + '... got the right savings balance after overdraft withdrawal' );