From: Dave Rolsky Date: Sat, 25 Sep 2010 18:17:10 +0000 (-0500) Subject: Improve tests for Counter trait X-Git-Tag: 1.15~105 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8b0c0c9d3628d7fd7b09256213e12843862bab71;p=gitmo%2FMoose.git Improve tests for Counter trait --- diff --git a/t/070_native_traits/001_trait_counter.t b/t/070_native_traits/001_trait_counter.t index 16a30be..eda1b6f 100644 --- a/t/070_native_traits/001_trait_counter.t +++ b/t/070_native_traits/001_trait_counter.t @@ -3,8 +3,9 @@ use strict; use warnings; +use Test::Exception; use Test::More; -use Test::Moose 'does_ok'; +use Test::Moose; my %handles = ( inc_counter => 'inc', @@ -17,7 +18,8 @@ my %handles = ( ); { - package MyHomePage; + + package Foo; use Moose; has 'counter' => ( @@ -29,41 +31,59 @@ my %handles = ( ); } -my $page = MyHomePage->new(); -isa_ok( $page, 'MyHomePage' ); +can_ok( 'Foo', $_ ) for sort keys %handles; + +with_immutable { + my $page = Foo->new(); + + is( $page->counter, 0, '... got the default value' ); -can_ok( $page, $_ ) for sort keys %handles; + $page->inc_counter; + is( $page->counter, 1, '... got the incremented value' ); -is( $page->counter, 0, '... got the default value' ); + $page->inc_counter; + is( $page->counter, 2, '... got the incremented value (again)' ); -$page->inc_counter; -is( $page->counter, 1, '... got the incremented value' ); + throws_ok { $page->inc_counter( 1, 2 ) } + qr/Cannot call inc with more than 1 argument/, + 'inc throws an error when two arguments are passed'; -$page->inc_counter; -is( $page->counter, 2, '... got the incremented value (again)' ); + $page->dec_counter; + is( $page->counter, 1, '... got the decremented value' ); -$page->dec_counter; -is( $page->counter, 1, '... got the decremented value' ); + throws_ok { $page->dec_counter( 1, 2 ) } + qr/Cannot call dec with more than 1 argument/, + 'dec throws an error when two arguments are passed'; -$page->reset_counter; -is( $page->counter, 0, '... got the original value' ); + $page->reset_counter; + is( $page->counter, 0, '... got the original value' ); -$page->set_counter(5); -is( $page->counter, 5, '... set the value' ); + throws_ok { $page->reset_counter(2) } + qr/Cannot call reset with any arguments/, + 'reset throws an error when an argument is passed'; -$page->inc_counter(2); -is( $page->counter, 7, '... increment by arg' ); + $page->set_counter(5); + is( $page->counter, 5, '... set the value' ); -$page->dec_counter(5); -is( $page->counter, 2, '... decrement by arg' ); + throws_ok { $page->set_counter( 1, 2 ) } + qr/Cannot call set with more than 1 argument/, + 'set throws an error when two arguments are passed'; -$page->inc_counter_2; -is( $page->counter, 4, '... curried increment' ); + $page->inc_counter(2); + is( $page->counter, 7, '... increment by arg' ); -$page->dec_counter_2; -is( $page->counter, 2, '... curried deccrement' ); + $page->dec_counter(5); + is( $page->counter, 2, '... decrement by arg' ); -$page->set_counter_42; -is( $page->counter, 42, '... curried set' ); + $page->inc_counter_2; + is( $page->counter, 4, '... curried increment' ); + + $page->dec_counter_2; + is( $page->counter, 2, '... curried deccrement' ); + + $page->set_counter_42; + is( $page->counter, 42, '... curried set' ); +} +'Foo'; done_testing;