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