X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F020_attributes%2F003_attribute_accessor_generation.t;h=a0f8742e34f79d5855b37ce88e93188951419a62;hb=ea829e77f657851c78cd65dd2b7ed05ce6c6ffff;hp=1a248604c53a9ca3f36936f35c2e5ae8d851adbf;hpb=e59a5c292a333cac504b65ebd4bba20b5e98d796;p=gitmo%2FMoose.git diff --git a/t/020_attributes/003_attribute_accessor_generation.t b/t/020_attributes/003_attribute_accessor_generation.t index 1a24860..a0f8742 100644 --- a/t/020_attributes/003_attribute_accessor_generation.t +++ b/t/020_attributes/003_attribute_accessor_generation.t @@ -3,35 +3,32 @@ use strict; use warnings; -use Test::More tests => 57; -use Test::Exception; +use Test::More; +use Test::Fatal; use Scalar::Util 'isweak'; -BEGIN { - use_ok('Moose'); -} { package Foo; use Moose; - + eval { has 'foo' => ( accessor => 'foo', ); }; ::ok(!$@, '... created the accessor method okay'); - + eval { has 'lazy_foo' => ( - accessor => 'lazy_foo', - lazy => 1, + accessor => 'lazy_foo', + lazy => 1, default => sub { 10 } ); }; - ::ok(!$@, '... created the lazy accessor method okay'); - + ::ok(!$@, '... created the lazy accessor method okay'); + eval { has 'foo_required' => ( @@ -47,15 +44,15 @@ BEGIN { isa => 'Int', ); }; - ::ok(!$@, '... created the accessor method with type constraint okay'); - + ::ok(!$@, '... created the accessor method with type constraint okay'); + eval { has 'foo_weak' => ( accessor => 'foo_weak', weak_ref => 1 ); }; - ::ok(!$@, '... created the accessor method with weak_ref okay'); + ::ok(!$@, '... created the accessor method with weak_ref okay'); eval { has 'foo_deref' => ( @@ -64,7 +61,7 @@ BEGIN { auto_deref => 1, ); }; - ::ok(!$@, '... created the accessor method with auto_deref okay'); + ::ok(!$@, '... created the accessor method with auto_deref okay'); eval { has 'foo_deref_ro' => ( @@ -73,7 +70,7 @@ BEGIN { auto_deref => 1, ); }; - ::ok(!$@, '... created the reader method with auto_deref okay'); + ::ok(!$@, '... created the reader method with auto_deref okay'); eval { has 'foo_deref_hash' => ( @@ -82,7 +79,7 @@ BEGIN { auto_deref => 1, ); }; - ::ok(!$@, '... created the reader method with auto_deref okay'); + ::ok(!$@, '... created the reader method with auto_deref okay'); } { @@ -93,78 +90,80 @@ BEGIN { can_ok($foo, 'foo'); is($foo->foo(), undef, '... got an unset value'); - lives_ok { + is( exception { $foo->foo(100); - } '... foo wrote successfully'; - is($foo->foo(), 100, '... got the correct set value'); - - ok(!isweak($foo->{foo}), '... it is not a weak reference'); - + }, undef, '... foo wrote successfully' ); + is($foo->foo(), 100, '... got the correct set value'); + + ok(!isweak($foo->{foo}), '... it is not a weak reference'); + # required writer - - dies_ok { + + isnt( exception { Foo->new; - } '... cannot create without the required attribute'; + }, undef, '... cannot create without the required attribute' ); can_ok($foo, 'foo_required'); is($foo->foo_required(), 'required', '... got an unset value'); - lives_ok { + is( exception { $foo->foo_required(100); - } '... foo_required wrote successfully'; - is($foo->foo_required(), 100, '... got the correct set value'); - - dies_ok { + }, undef, '... foo_required wrote successfully' ); + is($foo->foo_required(), 100, '... got the correct set value'); + + is( exception { $foo->foo_required(undef); - } '... foo_required died successfully'; + }, undef, '... foo_required did not die with undef' ); + + is($foo->foo_required, undef, "value is undef"); + + ok(!isweak($foo->{foo_required}), '... it is not a weak reference'); - ok(!isweak($foo->{foo_required}), '... it is not a weak reference'); - # lazy - + ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot'); - + can_ok($foo, 'lazy_foo'); - is($foo->lazy_foo(), 10, '... got an deferred value'); - + is($foo->lazy_foo(), 10, '... got an deferred value'); + # with type constraint - + can_ok($foo, 'foo_int'); is($foo->foo_int(), undef, '... got an unset value'); - lives_ok { + is( exception { $foo->foo_int(100); - } '... foo_int wrote successfully'; - is($foo->foo_int(), 100, '... got the correct set value'); - - dies_ok { + }, undef, '... foo_int wrote successfully' ); + is($foo->foo_int(), 100, '... got the correct set value'); + + isnt( exception { $foo->foo_int("Foo"); - } '... foo_int died successfully'; - - ok(!isweak($foo->{foo_int}), '... it is not a weak reference'); - + }, undef, '... foo_int died successfully' ); + + ok(!isweak($foo->{foo_int}), '... it is not a weak reference'); + # with weak_ref - + my $test = []; - + can_ok($foo, 'foo_weak'); is($foo->foo_weak(), undef, '... got an unset value'); - lives_ok { + is( exception { $foo->foo_weak($test); - } '... foo_weak wrote successfully'; - is($foo->foo_weak(), $test, '... got the correct set value'); - + }, undef, '... foo_weak wrote successfully' ); + is($foo->foo_weak(), $test, '... got the correct set value'); + ok(isweak($foo->{foo_weak}), '... it is a weak reference'); can_ok( $foo, 'foo_deref'); is_deeply( [$foo->foo_deref()], [], '... default default value'); my @list; - lives_ok { + is( exception { @list = $foo->foo_deref(); - } "... doesn't deref undef value"; + }, undef, "... doesn't deref undef value" ); is_deeply( \@list, [], "returns empty list in list context"); - lives_ok { + is( exception { $foo->foo_deref( [ qw/foo bar gorch/ ] ); - } '... foo_deref wrote successfully'; + }, undef, '... foo_deref wrote successfully' ); is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" ); is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" ); @@ -176,9 +175,9 @@ BEGIN { can_ok( $foo, 'foo_deref' ); is_deeply( [$foo->foo_deref_ro()], [], "... default default value" ); - dies_ok { + isnt( exception { $foo->foo_deref_ro( [] ); - } "... read only"; + }, undef, "... read only" ); $foo->{foo_deref_ro} = [qw/la la la/]; @@ -189,14 +188,14 @@ BEGIN { is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" ); my %hash; - lives_ok { + is( exception { %hash = $foo->foo_deref_hash(); - } "... doesn't deref undef value"; + }, undef, "... doesn't deref undef value" ); is_deeply( \%hash, {}, "returns empty list in list context"); - lives_ok { + is( exception { $foo->foo_deref_hash( { foo => 1, bar => 2 } ); - } '... foo_deref_hash wrote successfully'; + }, undef, '... foo_deref_hash wrote successfully' ); is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" ); @@ -204,5 +203,4 @@ BEGIN { is_deeply( \%hash, { foo => 1, bar => 2 }, "list context"); } - - +done_testing;