more tests
Stevan Little [Mon, 6 Mar 2006 06:29:23 +0000 (06:29 +0000)]
lib/Moose.pm
t/002_basic.t [new file with mode: 0644]

index 6067f2a..d060032 100644 (file)
@@ -25,7 +25,7 @@ sub import {
        else {
                $meta = Class::MOP::Class->initialize($pkg);
        }
-
+       
        $meta->alias_method('has' => sub {
                my ($name, %options) = @_;
                my ($init_arg) = ($name =~ /^[\$\@\%][\.\:](.*)$/);
@@ -41,6 +41,9 @@ sub import {
        
        $meta->superclasses('Moose::Object') 
                unless $meta->superclasses();
+
+       $meta->alias_method('confess' => \&confess);                    
+       $meta->alias_method('blessed' => \&blessed);                            
 }
 
 1;
diff --git a/t/002_basic.t b/t/002_basic.t
new file mode 100644 (file)
index 0000000..b87883b
--- /dev/null
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 16;
+use Test::Exception;
+
+BEGIN {
+    use_ok('Moose');           
+}
+
+{
+    package BankAccount;
+    use Moose;
+    
+    has '$.balance' => (accessor => 'balance', 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;
+
+       use base 'BankAccount';
+       
+    has '$.overdraft_account' => (accessor => 'overdraft_account');    
+
+       before 'withdraw' => sub {
+               my ($self, $amount) = @_;
+               my $overdraft_amount = $amount - $self->balance();
+               if ($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');
+