From: Christian Walde Date: Sun, 15 Jul 2012 17:35:15 +0000 (+0200) Subject: useful and detailed errors for coerce in attrib generation X-Git-Tag: v0.091013~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=17c4135ad8cb1b78f2b4a28b8d953a9b1f6b5bfe;p=gitmo%2FMoo.git useful and detailed errors for coerce in attrib generation --- diff --git a/Changes b/Changes index c3911ab..2797e2d 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ + - useful and detailed errors for coerce in attrib generation + 0.091012 - 2012-07-15 - useful and detailed errors for default checker in attrib generation - throw an error when trying to extend a role diff --git a/lib/Method/Generate/Accessor.pm b/lib/Method/Generate/Accessor.pm index 1268a9e..d12af58 100644 --- a/lib/Method/Generate/Accessor.pm +++ b/lib/Method/Generate/Accessor.pm @@ -44,14 +44,16 @@ sub generate_method { if (($spec->{trigger}||0) eq 1) { $spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)'); } - if (exists $spec->{default}) { - my $default = $spec->{default}; - my $invalid = "Invalid default '" . overload::StrVal($default) + + for my $setting (qw( default coerce )) { + next if !exists $spec->{$setting}; + my $value = $spec->{$setting}; + my $invalid = "Invalid $setting '" . overload::StrVal($value) . "' for $into->$name - not a coderef"; die "$invalid or code-convertible object" - unless ref $default and (ref $default eq 'CODE' or blessed($default)); + unless ref $value and (ref $value eq 'CODE' or blessed($value)); die "$invalid and could not be converted to a coderef: $@" - if !eval { \&$default }; + if !eval { \&$value }; } my %methods; diff --git a/t/method-generate-accessor.t b/t/method-generate-accessor.t index f66acfb..6914ba5 100644 --- a/t/method-generate-accessor.t +++ b/t/method-generate-accessor.t @@ -11,6 +11,12 @@ my $gen = Method::Generate::Accessor->new; use Moo; } +{ + package WithOverload; + use overload '&{}' => sub { sub { 5 } }; + sub new { bless {} } +} + $gen->generate_method('Foo' => 'one' => { is => 'ro' }); $gen->generate_method('Foo' => 'two' => { is => 'rw' }); @@ -25,47 +31,43 @@ like( qr/Unknown is purple/, 'is purple rejected' ); -like( - exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', default => 5 }) }, - qr/Invalid default/, 'default - scalar rejected' -); - -like( - exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => [] }) }, - qr/Invalid default/, 'default - arrayref rejected' -); - -like( - exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => Foo->new }) }, - qr/Invalid default/, 'default - non-code-convertible object rejected' -); - -is( - exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', default => sub { 5 } }) }, - undef, 'default - coderef accepted' -); - -is( - exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', default => bless sub { 5 } => 'Blah' }) }, - undef, 'default - blessed sub accepted' -); - -{ - package WithOverload; - use overload '&{}' => sub { sub { 5 } }; - sub new { bless {} } +for my $setting (qw( default coerce )) { + like( + exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', $setting => 5 }) }, + qr/Invalid $setting/, "$setting - scalar rejected" + ); + + like( + exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => [] }) }, + qr/Invalid $setting/, "$setting - arrayref rejected" + ); + + like( + exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => Foo->new }) }, + qr/Invalid $setting/, "$setting - non-code-convertible object rejected" + ); + + is( + exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', $setting => sub { 5 } }) }, + undef, "$setting - coderef accepted" + ); + + is( + exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', $setting => bless sub { 5 } => 'Blah' }) }, + undef, "$setting - blessed sub accepted" + ); + + is( + exception { $gen->generate_method('Foo' => 'eight' => { is => 'ro', $setting => WithOverload->new }) }, + undef, "$setting - object with overloaded ->() accepted" + ); + + like( + exception { $gen->generate_method('Foo' => 'nine' => { is => 'ro', $setting => bless {} => 'Blah' }) }, + qr/Invalid $setting/, "$setting - object rejected" + ); } -is( - exception { $gen->generate_method('Foo' => 'eight' => { is => 'ro', default => WithOverload->new }) }, - undef, 'default - object with overloaded ->() accepted' -); - -like( - exception { $gen->generate_method('Foo' => 'nine' => { is => 'ro', default => bless {} => 'Blah' }) }, - qr/Invalid default/, 'default - object rejected' -); - my $foo = Foo->new(one => 1); is($foo->one, 1, 'ro reads');