X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fmethod-generate-accessor.t;h=87d25089a9d85c0b1556b11a15ba3272b0f501fd;hb=2f425b5770149d4ed2e59da001c3be052cbd6bc1;hp=6914ba5ce31764c4c40c00e2279ed907078d48e4;hpb=17c4135ad8cb1b78f2b4a28b8d953a9b1f6b5bfe;p=gitmo%2FMoo.git diff --git a/t/method-generate-accessor.t b/t/method-generate-accessor.t index 6914ba5..87d2508 100644 --- a/t/method-generate-accessor.t +++ b/t/method-generate-accessor.t @@ -3,6 +3,7 @@ use Test::More; use Test::Fatal; use Method::Generate::Accessor; +use Sub::Quote 'quote_sub'; my $gen = Method::Generate::Accessor->new; @@ -13,7 +14,7 @@ my $gen = Method::Generate::Accessor->new; { package WithOverload; - use overload '&{}' => sub { sub { 5 } }; + use overload '&{}' => sub { sub { 5 } }, fallback => 1; sub new { bless {} } } @@ -31,43 +32,79 @@ like( qr/Unknown is purple/, 'is purple rejected' ); -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' => 'four' => { is => 'ro', coerce => 5 }) }, + qr/Invalid coerce/, "coerce - scalar rejected" +); + +is( + exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', default => 5 }) }, + undef, "default - non-ref scalar accepted" +); + +foreach my $setting (qw( default coerce )) { like( - exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => [] }) }, + exception { $gen->generate_method('Foo' => 'five' => { allow_overwrite => 1, is => 'ro', $setting => [] }) }, qr/Invalid $setting/, "$setting - arrayref rejected" ); like( - exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => Foo->new }) }, + exception { $gen->generate_method('Foo' => 'five' => { allow_overwrite => 1, 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 } }) }, + exception { $gen->generate_method('Foo' => 'six' => { allow_overwrite => 1, is => 'ro', $setting => sub { 5 } }) }, undef, "$setting - coderef accepted" ); is( - exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', $setting => bless sub { 5 } => 'Blah' }) }, + exception { $gen->generate_method('Foo' => 'seven' => { allow_overwrite => 1, is => 'ro', $setting => bless sub { 5 } => 'Blah' }) }, undef, "$setting - blessed sub accepted" ); is( - exception { $gen->generate_method('Foo' => 'eight' => { is => 'ro', $setting => WithOverload->new }) }, + exception { $gen->generate_method('Foo' => 'eight' => { allow_overwrite => 1, is => 'ro', $setting => WithOverload->new }) }, undef, "$setting - object with overloaded ->() accepted" ); like( - exception { $gen->generate_method('Foo' => 'nine' => { is => 'ro', $setting => bless {} => 'Blah' }) }, + exception { $gen->generate_method('Foo' => 'nine' => { allow_overwrite => 1, is => 'ro', $setting => bless {} => 'Blah' }) }, qr/Invalid $setting/, "$setting - object rejected" ); } +is( + exception { $gen->generate_method('Foo' => 'ten' => { is => 'ro', builder => '_build_ten' }) }, + undef, 'builder - string accepted', +); + +is( + exception { $gen->generate_method('Foo' => 'eleven' => { is => 'ro', builder => sub {} }) }, + undef, 'builder - coderef accepted' +); + +like( + exception { $gen->generate_method('Foo' => 'twelve' => { is => 'ro', builder => 'build:twelve' }) }, + qr/Invalid builder/, 'builder - invalid name rejected', +); + +is( + exception { $gen->generate_method('Foo' => 'thirteen' => { is => 'ro', builder => 'build::thirteen' }) }, + undef, 'builder - fully-qualified name accepted', +); + +is( + exception { $gen->generate_method('Foo' => 'fifteen' => { is => 'lazy', builder => sub {15} }) }, + undef, 'builder - coderef accepted' +); + +is( + exception { $gen->generate_method('Foo' => 'sixteen' => { is => 'lazy', builder => quote_sub q{ 16 } }) }, + undef, 'builder - quote_sub accepted' +); + my $foo = Foo->new(one => 1); is($foo->one, 1, 'ro reads'); @@ -78,4 +115,9 @@ is($foo->two, undef, 'rw reads'); $foo->two(-3); is($foo->two, -3, 'rw writes'); +is($foo->fifteen, 15, 'builder installs code sub'); +is($foo->_build_fifteen, 15, 'builder installs code sub under the correct name'); + +is($foo->sixteen, 16, 'builder installs quote_sub'); + done_testing;