X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F021-weak-ref.t;h=cdafd3c2cd0da7a2e7d5c647d51ef96ce306cdaf;hb=bf8e5b90e442d6ec7dbb837a051f547d685ee2e9;hp=bb3bf6f2d7ddba2cd5f88c80e0f32e659df4847c;hpb=e6ac69d42c5750e8ccb529b8bb4f05307d940df2;p=gitmo%2FMouse.git diff --git a/t/021-weak-ref.t b/t/021-weak-ref.t index bb3bf6f..cdafd3c 100644 --- a/t/021-weak-ref.t +++ b/t/021-weak-ref.t @@ -1,9 +1,18 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 18; + +use Test::More; +BEGIN { + if (eval "require Scalar::Util; 1") { + plan tests => 21; + } + else { + plan skip_all => "Scalar::Util required for this test"; + } +} + use Test::Exception; -use Scalar::Util 'isweak'; my %destroyed; @@ -35,7 +44,7 @@ do { $self2->self($self3); for my $object ($self, $self2, $self3) { - ok(isweak($object->{self}), "weak reference"); + ok(Scalar::Util::isweak($object->{self}), "weak reference"); ok($object->self->self->self->self, "we've got circularity"); } }; @@ -44,22 +53,33 @@ is($destroyed{accessor}, 1, "destroyed from the accessor"); is($destroyed{constructor}, 1, "destroyed from the constructor"); is($destroyed{middle}, 1, "casuality of war"); -ok(!Class->meta->get_attribute('type')->weak_ref, "type is not a weakref"); -ok(Class->meta->get_attribute('self')->weak_ref, "self IS a weakref"); +ok(!Class->meta->get_attribute('type')->is_weak_ref, "type is not a weakref"); +ok(Class->meta->get_attribute('self')->is_weak_ref, "self IS a weakref"); do { package Class2; use Mouse; has value => ( - is => 'ro', + is => 'rw', default => 10, weak_ref => 1, ); }; -throws_ok { Class2->new } qr/Can't weaken a nonreference/; -ok(Class2->meta->get_attribute('value')->weak_ref, "value IS a weakref"); +ok(Class2->meta->get_attribute('value')->is_weak_ref, "value IS a weakref"); + +lives_ok { + my $obj = Class2->new; + is($obj->value, 10, "weak_ref doesn't apply to non-refs"); +}; + +my $obj2 = Class2->new; +lives_ok { + $obj2->value({}); +}; + +is_deeply($obj2->value, undef, "weakened the reference even with a nonref default"); do { package Class3; @@ -81,4 +101,4 @@ $obj->hashref({1 => 1}); is($obj->hashref, undef, "hashref collected between set and get because refcount=0"); ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance'); -ok(Class3->meta->get_attribute('hashref')->weak_ref, "hashref IS a weakref"); +ok(Class3->meta->get_attribute('hashref')->is_weak_ref, "hashref IS a weakref");