X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Faccessor-isa.t;h=26d355b770b7b7f18a98b5ee3f31e1492c466ffb;hb=0fac3e2399478aea66e4a13e2aaa0b10f22239ed;hp=2ce945a8a177b4216df36e6e1c8506a01dc65eb9;hpb=159d9c5b735d060e33a267a00b391901b478502d;p=gitmo%2FMoo.git diff --git a/t/accessor-isa.t b/t/accessor-isa.t index 2ce945a..26d355b 100644 --- a/t/accessor-isa.t +++ b/t/accessor-isa.t @@ -103,14 +103,69 @@ my $lt3; $lt3 = 4; +my $lazyfoo = LazyFoo->new; like( - exception { LazyFoo->new->less_than_three }, + exception { $lazyfoo->less_than_three }, qr/isa check for "less_than_three" failed: 4 is not less than three/, "exception thrown on bad builder return value (LazyFoo)" ); $lt3 = 2; +is( + exception { $lazyfoo->less_than_three }, + undef, + 'Corrected builder value on existing object returned ok' +); + is(LazyFoo->new->less_than_three, 2, 'Correct builder value returned ok'); +{ + package Fizz; + + use Moo; + + has attr1 => ( + is => 'ro', + isa => sub { + no warnings 'once'; + my $attr = $Method::Generate::Accessor::CurrentAttribute; + die bless [@$attr{'name', 'init_arg', 'step'}], 'MyException'; + }, + init_arg => 'attr_1', + ); +} + +my $e = exception { Fizz->new(attr_1 => 5) }; +is( + ref($e), + 'MyException', + 'Exception objects passed though correctly', +); + +is($e->[0], 'attr1', 'attribute name available in isa check'); +is($e->[1], 'attr_1', 'attribute init_arg available in isa check'); +is($e->[2], 'isa check', 'step available in isa check'); + +{ + my $called; + local $SIG{__DIE__} = sub { $called++; die $_[0] }; + my $e = exception { Fizz->new(attr_1 => 5) }; + is($called, 1, '__DIE__ handler called if set') +} + +{ + package ClassWithDeadlyIsa; + use Moo; + has foo => (is => 'ro', isa => sub { die "nope" }); + + package ClassUsingDeadlyIsa; + use Moo; + has bar => (is => 'ro', coerce => sub { ClassWithDeadlyIsa->new(foo => $_[0]) }); +} + +like exception { ClassUsingDeadlyIsa->new(bar => 1) }, + qr/isa check for "foo" failed: nope/, + 'isa check within isa check produces correct exception'; + done_testing;