X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Faccessor-coerce.t;h=7533adac0f9440a0df1b2aaf5168605eb095903e;hb=49d332df39656855cadaa6d4775f6df1a238b7c9;hp=b9e79182d53b6ab83a830d026fae895f0ebfae95;hpb=d295de3027db04c06d30ceffed8f9faf877c64b0;p=gitmo%2FMoo.git diff --git a/t/accessor-coerce.t b/t/accessor-coerce.t index b9e7918..7533ada 100644 --- a/t/accessor-coerce.t +++ b/t/accessor-coerce.t @@ -14,6 +14,20 @@ sub run_for { is($obj->plus_three, 7, 'Value changes after set'); } +sub run_with_default_for { + my $class = shift; + + my $obj = $class->new(); + + is($obj->plus_three, 4, "initial value set (${class})"); + + $obj->plus_three(4); + + is($obj->plus_three, 7, 'Value changes after set'); +} + + + { package Foo; @@ -82,4 +96,77 @@ run_for 'Baz'; like exception { Biff->new(plus_three => 1) }, qr/could not add three!/, 'Exception properly thrown'; +{ + package Foo2; + + use Moo; + + has plus_three => ( + is => 'rw', + default => sub { 1 }, + coerce => sub { $_[0] + 3 } + ); +} + +run_with_default_for 'Foo2'; + +{ + package Bar2; + + use Sub::Quote; + use Moo; + + has plus_three => ( + is => 'rw', + default => sub { 1 }, + coerce => quote_sub q{ + my ($x) = @_; + $x + 3 + } + ); +} + +run_with_default_for 'Bar2'; + +{ + package Baz2; + + use Sub::Quote; + use Moo; + + has plus_three => ( + is => 'rw', + default => sub { 1 }, + coerce => quote_sub( + q{ + my ($value) = @_; + $value + $plus + }, + { '$plus' => \3 } + ) + ); +} + +run_with_default_for 'Baz2'; + +{ + package Biff2; + + use Sub::Quote; + use Moo; + + has plus_three => ( + is => 'rw', + default => sub { 1 }, + coerce => quote_sub( + q{ + die 'could not add three!' + }, + ) + ); +} + +like exception { Biff2->new() }, qr/could not add three!/, 'Exception properly thrown'; + + done_testing;