apply default values when applying role to object
[gitmo/Moo.git] / t / accessor-weaken.t
index ec5b069..245ea7a 100644 (file)
@@ -1,18 +1,24 @@
 use strictures 1;
 use Test::More;
+use Moo::_Utils;
+
+ok(Moo::_Utils::lt_5_8_3, "pretending to be pre-5.8.3")
+    if $ENV{MOO_TEST_PRE_583};
 
 {
   package Foo;
 
   use Moo;
 
-  has one => (is => 'ro', weak_ref => 1);
+  has one => (is => 'rw', weak_ref => 1);
 
   package Foo2;
 
   use Moo;
 
-  has one => (is => 'lazy', weak_ref => 1, default => sub { {} });
+  our $preexist = {};
+  has one => (is => 'rw', lazy => 1, weak_ref => 1, default => sub { $preexist });
+  has two => (is => 'rw', lazy => 1, weak_ref => 1, default => sub { {} });
 }
 
 my $ref = {};
@@ -23,11 +29,21 @@ undef $ref;
 ok(!defined $foo->{one}, 'weak value gone');
 
 my $foo2 = Foo2->new;
-ok(my $ref2 = $foo2->one, 'value returned');
+ok(my $ref2 = $foo2->one, 'external value returned');
+is($foo2->one, $ref2, 'value maintained');
 ok(Scalar::Util::isweak($foo2->{one}), 'value weakened');
+is($foo2->one($ref2), $ref2, 'value returned from setter');
 undef $ref2;
 ok(!defined $foo->{one}, 'weak value gone');
 
+is($foo2->two, undef, 'weak+lazy ref not returned');
+is($foo2->{two}, undef, 'internal value not set');
+my $ref3 = {};
+is($foo2->two($ref3), $ref3, 'value returned from setter');
+undef $ref3;
+ok(!defined $foo->{two}, 'weak value gone');
+
+
 # test readonly SVs
 sub mk_ref { \ 'yay' };
 my $foo_ro = eval { Foo->new(one => mk_ref()) };