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