Commit | Line | Data |
de3f9ba5 |
1 | #!/usr/bin/perl -w |
2 | |
3 | use strict; |
4 | use Test::More; |
5 | BEGIN{ |
6 | if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){ |
7 | plan 'no_plan'; |
8 | } |
9 | else{ |
10 | plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?'; |
11 | } |
12 | } |
13 | |
14 | use Test::Exception; |
15 | $| = 1; |
16 | |
17 | |
18 | |
19 | # =begin testing SETUP |
20 | { |
21 | |
22 | package BankAccount; |
23 | use Mouse; |
24 | |
25 | has 'balance' => ( isa => 'Int', is => 'rw', default => 0 ); |
26 | |
27 | sub deposit { |
28 | my ( $self, $amount ) = @_; |
29 | $self->balance( $self->balance + $amount ); |
30 | } |
31 | |
32 | sub withdraw { |
33 | my ( $self, $amount ) = @_; |
34 | my $current_balance = $self->balance(); |
35 | ( $current_balance >= $amount ) |
36 | || confess "Account overdrawn"; |
37 | $self->balance( $current_balance - $amount ); |
38 | } |
39 | |
40 | package CheckingAccount; |
41 | use Mouse; |
42 | |
43 | extends 'BankAccount'; |
44 | |
45 | has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' ); |
46 | |
47 | before 'withdraw' => sub { |
48 | my ( $self, $amount ) = @_; |
49 | my $overdraft_amount = $amount - $self->balance(); |
50 | if ( $self->overdraft_account && $overdraft_amount > 0 ) { |
51 | $self->overdraft_account->withdraw($overdraft_amount); |
52 | $self->deposit($overdraft_amount); |
53 | } |
54 | }; |
55 | } |
56 | |
57 | |
58 | |
59 | # =begin testing |
60 | { |
61 | my $savings_account; |
62 | |
63 | { |
64 | $savings_account = BankAccount->new( balance => 250 ); |
65 | isa_ok( $savings_account, 'BankAccount' ); |
66 | |
67 | is( $savings_account->balance, 250, '... got the right savings balance' ); |
68 | lives_ok { |
69 | $savings_account->withdraw(50); |
70 | } |
71 | '... withdrew from savings successfully'; |
72 | is( $savings_account->balance, 200, |
73 | '... got the right savings balance after withdrawl' ); |
74 | |
75 | $savings_account->deposit(150); |
76 | is( $savings_account->balance, 350, |
77 | '... got the right savings balance after deposit' ); |
78 | } |
79 | |
80 | { |
81 | my $checking_account = CheckingAccount->new( |
82 | balance => 100, |
83 | overdraft_account => $savings_account |
84 | ); |
85 | isa_ok( $checking_account, 'CheckingAccount' ); |
86 | isa_ok( $checking_account, 'BankAccount' ); |
87 | |
88 | is( $checking_account->overdraft_account, $savings_account, |
89 | '... got the right overdraft account' ); |
90 | |
91 | is( $checking_account->balance, 100, |
92 | '... got the right checkings balance' ); |
93 | |
94 | lives_ok { |
95 | $checking_account->withdraw(50); |
96 | } |
97 | '... withdrew from checking successfully'; |
98 | is( $checking_account->balance, 50, |
99 | '... got the right checkings balance after withdrawl' ); |
100 | is( $savings_account->balance, 350, |
101 | '... got the right savings balance after checking withdrawl (no overdraft)' |
102 | ); |
103 | |
104 | lives_ok { |
105 | $checking_account->withdraw(200); |
106 | } |
107 | '... withdrew from checking successfully'; |
108 | is( $checking_account->balance, 0, |
109 | '... got the right checkings balance after withdrawl' ); |
110 | is( $savings_account->balance, 200, |
111 | '... got the right savings balance after overdraft withdrawl' ); |
112 | } |
113 | |
114 | { |
115 | my $checking_account = CheckingAccount->new( |
116 | balance => 100 |
117 | |
118 | # no overdraft account |
119 | ); |
120 | isa_ok( $checking_account, 'CheckingAccount' ); |
121 | isa_ok( $checking_account, 'BankAccount' ); |
122 | |
123 | is( $checking_account->overdraft_account, undef, |
124 | '... no overdraft account' ); |
125 | |
126 | is( $checking_account->balance, 100, |
127 | '... got the right checkings balance' ); |
128 | |
129 | lives_ok { |
130 | $checking_account->withdraw(50); |
131 | } |
132 | '... withdrew from checking successfully'; |
133 | is( $checking_account->balance, 50, |
134 | '... got the right checkings balance after withdrawl' ); |
135 | |
136 | dies_ok { |
137 | $checking_account->withdraw(200); |
138 | } |
139 | '... withdrawl failed due to attempted overdraft'; |
140 | is( $checking_account->balance, 50, |
141 | '... got the right checkings balance after withdrawl failure' ); |
142 | } |
143 | } |
144 | |
145 | |
146 | |
147 | |
148 | 1; |