From: Dave Rolsky Date: Sat, 25 Sep 2010 18:58:03 +0000 (-0500) Subject: Improve Bool trait tests X-Git-Tag: 1.15~98 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=26a792151cf485ba3416b89d3dcafc45021d965f;p=gitmo%2FMoose.git Improve Bool trait tests --- diff --git a/t/070_native_traits/006_trait_bool.t b/t/070_native_traits/006_trait_bool.t index a5cb91f..7587d57 100644 --- a/t/070_native_traits/006_trait_bool.t +++ b/t/070_native_traits/006_trait_bool.t @@ -3,41 +3,88 @@ use strict; use warnings; +use Moose (); use Test::More; +use Test::Exception; +use Test::Moose; { - package Room; - use Moose; - - has 'is_lit' => ( - traits => ['Bool'], - is => 'rw', - isa => 'Bool', - default => 0, - handles => { - illuminate => 'set', - darken => 'unset', - flip_switch => 'toggle', - is_dark => 'not', - }, - ) + my %handles = ( + illuminate => 'set', + darken => 'unset', + flip_switch => 'toggle', + is_dark => 'not', + ); + + my $name = 'Foo1'; + + sub build_class { + my %attr = @_; + + my $class = Moose::Meta::Class->create( + $name++, + superclasses => ['Moose::Object'], + ); + + $class->add_attribute( + is_lit => ( + traits => ['Bool'], + is => 'rw', + isa => 'Bool', + default => 0, + handles => \%handles, + clearer => '_clear_is_list', + %attr, + ), + ); + + return ( $class->name, \%handles ); + } +} + +{ + run_tests(build_class); + run_tests( build_class( lazy => 1 ) ); } -my $room = Room->new; -$room->illuminate; -ok( $room->is_lit, 'set is_lit to 1 using ->illuminate' ); -ok( !$room->is_dark, 'check if is_dark does the right thing' ); +sub run_tests { + my ( $class, $handles ) = @_; -$room->darken; -ok( !$room->is_lit, 'set is_lit to 0 using ->darken' ); -ok( $room->is_dark, 'check if is_dark does the right thing' ); + can_ok( $class, $_ ) for sort keys %{$handles}; -$room->flip_switch; -ok( $room->is_lit, 'toggle is_lit back to 1 using ->flip_switch' ); -ok( !$room->is_dark, 'check if is_dark does the right thing' ); + with_immutable { + my $obj = $class->new; -$room->flip_switch; -ok( !$room->is_lit, 'toggle is_lit back to 0 again using ->flip_switch' ); -ok( $room->is_dark, 'check if is_dark does the right thing' ); + $obj->illuminate; + ok( $obj->is_lit, 'set is_lit to 1 using ->illuminate' ); + ok( !$obj->is_dark, 'check if is_dark does the right thing' ); + + throws_ok { $obj->illuminate(1) } + qr/Cannot call set with any arguments/, + 'set throws an error when an argument is passed'; + + $obj->darken; + ok( !$obj->is_lit, 'set is_lit to 0 using ->darken' ); + ok( $obj->is_dark, 'check if is_dark does the right thing' ); + + throws_ok { $obj->darken(1) } + qr/Cannot call unset with any arguments/, + 'unset throws an error when an argument is passed'; + + $obj->flip_switch; + ok( $obj->is_lit, 'toggle is_lit back to 1 using ->flip_switch' ); + ok( !$obj->is_dark, 'check if is_dark does the right thing' ); + + throws_ok { $obj->flip_switch(1) } + qr/Cannot call toggle with any arguments/, + 'toggle throws an error when an argument is passed'; + + $obj->flip_switch; + ok( !$obj->is_lit, + 'toggle is_lit back to 0 again using ->flip_switch' ); + ok( $obj->is_dark, 'check if is_dark does the right thing' ); + } + $class; +} done_testing;