Beta Shell
v2.0 ยท web2.us.cloudlogin.co
[FM]
[CMD]
[PHP]
[DB]
[INFO]
[SEC]
File Manager
~
/
usr
/
share
/
doc
/
perl-Moose
/
t
/
recipes
Upload
21 items
Name
Size
Perms
Modified
Actions
[ .. / .. ]
basics_bankaccount_methodmodifiersandsubclassing.t
3.71 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_binarytree_attributefeatures.t
3.83 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_company_subtypes.t
9.54 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_datetime_extendingnonmooseparent.t
1.16 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_document_augmentandinner.t
1.45 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_genome_overloadingsubtypesandcoercion.t
5.2 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_http_subtypesandcoercion.t
3.24 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
basics_point_attributesandsubclassing.t
5.35 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
extending_debugging_baseclassrole.t
755 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
extending_mooseish_moosesugar.t
983 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
legacy_debugging_baseclassreplacement.t
953 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
legacy_labeled_attributemetaclass.t
1.52 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_globref_instancemetaclass.t
3.23 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_labeled_attributetrait.t
1.43 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_privateorpublic_methodmetaclass.t
1.93 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
meta_table_metaclasstrait.t
798 B
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
roles_applicationtoinstance.t
1.73 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
roles_comparable_codereuse.t
5.16 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
roles_restartable_advancedcomposition.t
2 KB
-rw-r--r--
2019-10-21 09:36:33
Edit
Del
Editing: basics_bankaccount_methodmodifiersandsubclassing.t
(3.71 KB)
Path: /usr/share/doc/perl-Moose/t/recipes/basics_bankaccount_methodmodifiersandsubclassing.t
Back
#!/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' ); is( $savings_account->balance, 200, '... got the right savings balance after withdrawal' ); $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' ); is( exception { $checking_account->withdraw(50); }, undef, '... withdrew from checking successfully' ); is( $checking_account->balance, 50, '... got the right checkings balance after withdrawal' ); is( $savings_account->balance, 350, '... got the right savings balance after checking withdrawal (no overdraft)' ); is( exception { $checking_account->withdraw(200); }, undef, '... withdrew from checking successfully' ); is( $checking_account->balance, 0, '... got the right checkings balance after withdrawal' ); is( $savings_account->balance, 200, '... got the right savings balance after overdraft withdrawal' ); } { 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' ); is( exception { $checking_account->withdraw(50); }, undef, '... withdrew from checking successfully' ); is( $checking_account->balance, 50, '... got the right checkings balance after withdrawal' ); isnt( exception { $checking_account->withdraw(200); }, undef, '... withdrawal failed due to attempted overdraft' ); is( $checking_account->balance, 50, '... got the right checkings balance after withdrawal failure' ); } } 1;