MOOOOOOOOOOOOOOOOOOOOOOSSSSEE
[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' => (
20                 accessor        => 'balance', 
21                 default         => 0,
22                 type_constraint => Int(),               
23         );
24
25     sub deposit {
26         my ($self, $amount) = @_;
27         $self->balance($self->balance + $amount);
28     }
29     
30     sub withdraw {
31         my ($self, $amount) = @_;
32         my $current_balance = $self->balance();
33         ($current_balance >= $amount)
34             || confess "Account overdrawn";
35         $self->balance($current_balance - $amount);
36     }
37
38         package CheckingAccount;
39         use strict;
40         use warnings;   
41         use Moose;
42
43         extends 'BankAccount';
44         
45     has 'overdraft_account' => (
46                 accessor        => 'overdraft_account',
47                 type_constraint => subtype Object => where { $_->isa('BankAccount') },          
48         );      
49
50         before 'withdraw' => sub {
51                 my ($self, $amount) = @_;
52                 my $overdraft_amount = $amount - $self->balance();
53                 if ($overdraft_amount > 0) {
54                         $self->overdraft_account->withdraw($overdraft_amount);
55                         $self->deposit($overdraft_amount);
56                 }
57         };
58 }
59
60
61 my $savings_account = BankAccount->new(balance => 250);
62 isa_ok($savings_account, 'BankAccount');
63
64 is($savings_account->balance, 250, '... got the right savings balance');
65 lives_ok {
66         $savings_account->withdraw(50);
67 } '... withdrew from savings successfully';
68 is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
69
70 $savings_account->deposit(150);
71 is($savings_account->balance, 350, '... got the right savings balance after deposit');
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, '... got the right overdraft account');
81
82 is($checking_account->balance, 100, '... got the right checkings balance');
83
84 lives_ok {
85         $checking_account->withdraw(50);
86 } '... withdrew from checking successfully';
87 is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
88 is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');
89
90 lives_ok {
91         $checking_account->withdraw(200);
92 } '... withdrew from checking successfully';
93 is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
94 is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');
95