merge trunk to pluggable errors
[gitmo/Moose.git] / t / 000_recipes / basics / 002_bank_account.t
CommitLineData
ad1ac1bd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
e606ae5f 6use Test::More tests => 23;
ad1ac1bd 7use Test::Exception;
8
ad1ac1bd 9{
10 package BankAccount;
11 use Moose;
e606ae5f 12
13 has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );
ad1ac1bd 14
15 sub deposit {
e606ae5f 16 my ( $self, $amount ) = @_;
17 $self->balance( $self->balance + $amount );
ad1ac1bd 18 }
e606ae5f 19
ad1ac1bd 20 sub withdraw {
e606ae5f 21 my ( $self, $amount ) = @_;
ad1ac1bd 22 my $current_balance = $self->balance();
e606ae5f 23 ( $current_balance >= $amount )
ad1ac1bd 24 || confess "Account overdrawn";
e606ae5f 25 $self->balance( $current_balance - $amount );
ad1ac1bd 26 }
e606ae5f 27
28 __PACKAGE__->meta->make_immutable( debug => 0 );
29}
30
31{
32 package CheckingAccount;
33 use Moose;
34
35 extends 'BankAccount';
36
37 has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );
38
39 before 'withdraw' => sub {
40 my ( $self, $amount ) = @_;
41 my $overdraft_amount = $amount - $self->balance();
42 if ( $self->overdraft_account && $overdraft_amount > 0 ) {
43 $self->overdraft_account->withdraw($overdraft_amount);
44 $self->deposit($overdraft_amount);
45 }
46 };
47
48 __PACKAGE__->meta->make_immutable( debug => 0 );
ad1ac1bd 49}
50
ad1ac1bd 51my $savings_account = BankAccount->new(balance => 250);
52isa_ok($savings_account, 'BankAccount');
53
54is($savings_account->balance, 250, '... got the right savings balance');
55lives_ok {
56 $savings_account->withdraw(50);
57} '... withdrew from savings successfully';
58is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
59
60$savings_account->deposit(150);
61is($savings_account->balance, 350, '... got the right savings balance after deposit');
62
703d9522 63{
64 my $checking_account = CheckingAccount->new(
65 balance => 100,
66 overdraft_account => $savings_account
67 );
68 isa_ok($checking_account, 'CheckingAccount');
69 isa_ok($checking_account, 'BankAccount');
70
71 is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
72
73 is($checking_account->balance, 100, '... got the right checkings balance');
74
75 lives_ok {
76 $checking_account->withdraw(50);
77 } '... withdrew from checking successfully';
78 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
79 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
80
81 lives_ok {
82 $checking_account->withdraw(200);
83 } '... withdrew from checking successfully';
84 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
85 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');
86}
ad1ac1bd 87
703d9522 88{
89 my $checking_account = CheckingAccount->new(
90 balance => 100
91 # no overdraft account
92 );
93 isa_ok($checking_account, 'CheckingAccount');
94 isa_ok($checking_account, 'BankAccount');
95
96 is($checking_account->overdraft_account, undef, '... no overdraft account');
97
98 is($checking_account->balance, 100, '... got the right checkings balance');
99
100 lives_ok {
101 $checking_account->withdraw(50);
102 } '... withdrew from checking successfully';
103 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
104
105 dies_ok {
106 $checking_account->withdraw(200);
107 } '... withdrawl failed due to attempted overdraft';
108 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl failure');
109}
ad1ac1bd 110
ad1ac1bd 111