the beginnings of Moose handling
[gitmo/Moo.git] / t / accessor-weaken.t
CommitLineData
32381de9 1use strictures 1;
2use Test::More;
3
4{
5 package Foo;
6
7 use Moo;
8
9 has one => (is => 'ro', weak_ref => 1);
10}
11
2215d4b9 12my $ref = {};
32381de9 13my $foo = Foo->new(one => $ref);
2215d4b9 14is($foo->one, $ref, 'value present');
32381de9 15ok(Scalar::Util::isweak($foo->{one}), 'value weakened');
2215d4b9 16undef $ref;
17ok (!defined $foo->{one}, 'weak value gone');
18
19# test readonly SVs
20sub mk_ref { \ 'yay' };
21my $foo_ro = eval { Foo->new(one => mk_ref()) };
22if ($] < 5.008003) {
23 like(
24 $@,
25 qr/\QReference to readonly value in "one" can not be weakened on Perl < 5.8.3/,
26 'Expected exception thrown on old perls'
27 );
28}
5f578bd3 29elsif ($^O eq 'cygwin' and $] < 5.012000) {
30 SKIP: { skip 'Static coderef reaping seems nonfunctional on cygwin < 5.12', 1 }
31}
2215d4b9 32else {
33 is(${$foo_ro->one},'yay', 'value present');
34 ok(Scalar::Util::isweak($foo_ro->{one}), 'value weakened');
35
36 { no warnings 'redefine'; *mk_ref = sub {} }
37 ok (!defined $foo_ro->{one}, 'optree reaped, ro static value gone');
38}
32381de9 39
40done_testing;