5949981e8a7b034583b2ed6ce698179db578ac03
[gitmo/Mouse.git] / t / 000-recipes / moose_cookbook_basics_recipe2.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5
6 use Test::Exception;
7 $| = 1;
8
9
10
11 # =begin testing SETUP
12 {
13
14   package BankAccount;
15   use Mouse;
16
17   has 'balance' => ( isa => 'Int', is => 'rw', default => 0 );
18
19   sub deposit {
20       my ( $self, $amount ) = @_;
21       $self->balance( $self->balance + $amount );
22   }
23
24   sub withdraw {
25       my ( $self, $amount ) = @_;
26       my $current_balance = $self->balance();
27       ( $current_balance >= $amount )
28           || confess "Account overdrawn";
29       $self->balance( $current_balance - $amount );
30   }
31
32   package CheckingAccount;
33   use Mouse;
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
49
50
51 # =begin testing
52 {
53 my $savings_account;
54
55 {
56     $savings_account = BankAccount->new( balance => 250 );
57     isa_ok( $savings_account, 'BankAccount' );
58
59     is( $savings_account->balance, 250, '... got the right savings balance' );
60     lives_ok {
61         $savings_account->withdraw(50);
62     }
63     '... withdrew from savings successfully';
64     is( $savings_account->balance, 200,
65         '... got the right savings balance after withdrawl' );
66
67     $savings_account->deposit(150);
68     is( $savings_account->balance, 350,
69         '... got the right savings balance after deposit' );
70 }
71
72 {
73     my $checking_account = CheckingAccount->new(
74         balance           => 100,
75         overdraft_account => $savings_account
76     );
77     isa_ok( $checking_account, 'CheckingAccount' );
78     isa_ok( $checking_account, 'BankAccount' );
79
80     is( $checking_account->overdraft_account, $savings_account,
81         '... got the right overdraft account' );
82
83     is( $checking_account->balance, 100,
84         '... got the right checkings balance' );
85
86     lives_ok {
87         $checking_account->withdraw(50);
88     }
89     '... withdrew from checking successfully';
90     is( $checking_account->balance, 50,
91         '... got the right checkings balance after withdrawl' );
92     is( $savings_account->balance, 350,
93         '... got the right savings balance after checking withdrawl (no overdraft)'
94     );
95
96     lives_ok {
97         $checking_account->withdraw(200);
98     }
99     '... withdrew from checking successfully';
100     is( $checking_account->balance, 0,
101         '... got the right checkings balance after withdrawl' );
102     is( $savings_account->balance, 200,
103         '... got the right savings balance after overdraft withdrawl' );
104 }
105
106 {
107     my $checking_account = CheckingAccount->new(
108         balance => 100
109
110             # no overdraft account
111     );
112     isa_ok( $checking_account, 'CheckingAccount' );
113     isa_ok( $checking_account, 'BankAccount' );
114
115     is( $checking_account->overdraft_account, undef,
116         '... no overdraft account' );
117
118     is( $checking_account->balance, 100,
119         '... got the right checkings balance' );
120
121     lives_ok {
122         $checking_account->withdraw(50);
123     }
124     '... withdrew from checking successfully';
125     is( $checking_account->balance, 50,
126         '... got the right checkings balance after withdrawl' );
127
128     dies_ok {
129         $checking_account->withdraw(200);
130     }
131     '... withdrawl failed due to attempted overdraft';
132     is( $checking_account->balance, 50,
133         '... got the right checkings balance after withdrawl failure' );
134 }
135 }
136
137
138
139
140 1;