File: 002_bank_account.t

package info (click to toggle)
libmoosex-declare-perl 0.35-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 424 kB
  • sloc: perl: 1,437; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 23;
use Test::Exception;

use MooseX::Declare;

class BankAccount {
    has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );

    method deposit (Num $amount) {
        $self->balance( $self->balance + $amount );
    }

    method withdraw (Num $amount) {
        my $current_balance = $self->balance();
        ( $current_balance >= $amount )
            || confess "Account overdrawn";
        $self->balance( $current_balance - $amount );
    }
}

class CheckingAccount extends BankAccount {
    has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );

    before withdraw (Num $amount) {
        my $overdraft_amount = $amount - $self->balance();
        if ( $self->overdraft_account && $overdraft_amount > 0 ) {
            $self->overdraft_account->withdraw($overdraft_amount);
            $self->deposit($overdraft_amount);
        }
    }
}

my $savings_account = BankAccount->new(balance => 250);
isa_ok($savings_account, 'BankAccount');

is($savings_account->balance, 250, '... got the right savings balance');
lives_ok {
    $savings_account->withdraw(50);
} '... withdrew from savings successfully';
is($savings_account->balance, 200, '... got the right savings balance after withdrawl');

$savings_account->deposit(150);
is($savings_account->balance, 350, '... got the right savings balance after deposit');

{
    my $checking_account = CheckingAccount->new(
                                balance => 100,
                                overdraft_account => $savings_account
                            );
    isa_ok($checking_account, 'CheckingAccount');
    isa_ok($checking_account, 'BankAccount');

    is($checking_account->overdraft_account, $savings_account, '... got the right overdraft account');

    is($checking_account->balance, 100, '... got the right checkings balance');

    lives_ok {
        $checking_account->withdraw(50);
    } '... withdrew from checking successfully';
    is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');
    is($savings_account->balance, 350, '... got the right savings balance after checking withdrawl (no overdraft)');

    lives_ok {
        $checking_account->withdraw(200);
    } '... withdrew from checking successfully';
    is($checking_account->balance, 0, '... got the right checkings balance after withdrawl');
    is($savings_account->balance, 200, '... got the right savings balance after overdraft withdrawl');
}

{
    my $checking_account = CheckingAccount->new(
                                balance => 100
                                # no overdraft account
                            );
    isa_ok($checking_account, 'CheckingAccount');
    isa_ok($checking_account, 'BankAccount');

    is($checking_account->overdraft_account, undef, '... no overdraft account');

    is($checking_account->balance, 100, '... got the right checkings balance');

    lives_ok {
        $checking_account->withdraw(50);
    } '... withdrew from checking successfully';
    is($checking_account->balance, 50, '... got the right checkings balance after withdrawl');

    dies_ok {
        $checking_account->withdraw(200);
    } '... withdrawl failed due to attempted overdraft';
    is($checking_account->balance, 50, '... got the right checkings balance after withdrawl failure');
}