X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FCookbook%2FBasics%2FRecipe2.pod;h=d180a381fb9a2978d099bea29899791a806f4c6a;hb=1808c2dab86d0317d10f70d3a7a923e97daf54bb;hp=6acf62d055590b6dc347088ec3c85ef187893b48;hpb=8c3d5c8821f3f10840b6e247468317f29489ce63;p=gitmo%2FMoose.git diff --git a/lib/Moose/Cookbook/Basics/Recipe2.pod b/lib/Moose/Cookbook/Basics/Recipe2.pod index 6acf62d..d180a38 100644 --- a/lib/Moose/Cookbook/Basics/Recipe2.pod +++ b/lib/Moose/Cookbook/Basics/Recipe2.pod @@ -157,14 +157,12 @@ method, which accepts named parameters. ); And as with the first recipe, a more in-depth example can be found in -the F test file. +the F test file. =head1 CONCLUSION -The aim of this recipe was to take the knowledge gained in the first -recipe and expand upon it with a more realistic use case. The next -recipe will expand on Moose attributes to create a behaviorally -sophisticated class defined almost entirely by its attributes. +This recipe expanded on the basic concepts from the first recipe with +a more "real world" use case. =head1 FOOTNOTES @@ -229,4 +227,91 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. +=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' ); + 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' ); +} + +=end testing + =cut