Benchmark-DKbench

 view release on metacpan or  search on metacpan

data/t/recipes/basics_bankaccount_methodmodifiersandsubclassing.t  view on Meta::CPAN

#!/usr/bin/perl -w

use strict;
use Test::More 'no_plan';
use Test::Fatal;
$| = 1;



# =begin testing SETUP
{

  package BankAccount;
  use Moose;

  has 'balance' => ( isa => 'Int', is => 'rw', default => 0 );

  sub deposit {
      my ( $self, $amount ) = @_;
      $self->balance( $self->balance + $amount );
  }

  sub withdraw {
      my ( $self, $amount ) = @_;
      my $current_balance = $self->balance();
      ( $current_balance >= $amount )
          || confess "Account overdrawn";
      $self->balance( $current_balance - $amount );
  }

  package CheckingAccount;
  use Moose;

  extends 'BankAccount';

  has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' );

  before 'withdraw' => sub {
      my ( $self, $amount ) = @_;
      my $overdraft_amount = $amount - $self->balance();
      if ( $self->overdraft_account && $overdraft_amount > 0 ) {
          $self->overdraft_account->withdraw($overdraft_amount);
          $self->deposit($overdraft_amount);
      }
  };
}



# =begin testing
{
my $savings_account;

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

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



( run in 1.612 second using v1.01-cache-2.11-cpan-54e63673c56 )