ec98fd01f522f72678afbf041f0b1a08f8042f65
[gitmo/Moose.git] / t / 002_basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 16;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 {
14     package BankAccount;
15         use strict;
16         use warnings;
17     use Moose;
18     
19     has 'balance' => (isa => 'Int', is => 'rw', default => 0);
20
21     sub deposit {
22         my ($self, $amount) = @_;
23         $self->balance($self->balance + $amount);
24     }
25     
26     sub withdraw {
27         my ($self, $amount) = @_;
28         my $current_balance = $self->balance();
29         ($current_balance >= $amount)
30             || confess "Account overdrawn";
31         $self->balance($current_balance - $amount);
32     }
33
34         package CheckingAccount;
35         use strict;
36         use warnings;   
37         use Moose;
38
39         extends 'BankAccount';
40         
41     has 'overdraft_account' => (isa => 'BankAccount', is => 'rw');      
42
43         before 'withdraw' => sub {
44                 my ($self, $amount) = @_;
45                 my $overdraft_amount = $amount - $self->balance();
46                 if ($overdraft_amount > 0) {
47                         $self->overdraft_account->withdraw($overdraft_amount);
48                         $self->deposit($overdraft_amount);
49                 }
50         };
51 }
52
53 my $savings_account = BankAccount->new(balance => 250);
54 isa_ok($savings_account, 'BankAccount');
55
56 is($savings_account->balance, 250, '... got the right savings balance');
57 lives_ok {
58         $savings_account->withdraw(50);
59 } '... withdrew from savings successfully';
60 is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
61
62 $savings_account->deposit(150);
63 is($savings_account->balance, 350, '... got the right savings balance after deposit');
64
65 my $checking_account = CheckingAccount->new(
66                                                         balance => 100,
67                                                         overdraft_account => $savings_account
68                                                 );
69 isa_ok($checking_account, 'CheckingAccount');
70 isa_ok($checking_account, 'BankAccount');
71
72 is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');
73
74 is($checking_account->balance, 100, '... got the right checkings balance');
75
76 lives_ok {
77         $checking_account->withdraw(50);
78 } '... withdrew from checking successfully';
79 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
80 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
81
82 lives_ok {
83         $checking_account->withdraw(200);
84 } '... withdrew from checking successfully';
85 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
86 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');
87