extra test from FAIL_RT70593 branch
[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);
6998abed 10
11 package Foo2;
12
13 use Moo;
14
15 has one => (is => 'lazy', weak_ref => 1, default => sub { {} });
32381de9 16}
17
2215d4b9 18my $ref = {};
32381de9 19my $foo = Foo->new(one => $ref);
2215d4b9 20is($foo->one, $ref, 'value present');
32381de9 21ok(Scalar::Util::isweak($foo->{one}), 'value weakened');
2215d4b9 22undef $ref;
6998abed 23ok(!defined $foo->{one}, 'weak value gone');
24
25my $foo2 = Foo2->new;
26ok(my $ref2 = $foo2->one, 'value returned');
27ok(Scalar::Util::isweak($foo2->{one}), 'value weakened');
28undef $ref2;
29ok(!defined $foo->{one}, 'weak value gone');
2215d4b9 30
31# test readonly SVs
32sub mk_ref { \ 'yay' };
33my $foo_ro = eval { Foo->new(one => mk_ref()) };
34if ($] < 5.008003) {
35 like(
36 $@,
37 qr/\QReference to readonly value in "one" can not be weakened on Perl < 5.8.3/,
38 'Expected exception thrown on old perls'
39 );
40}
5f578bd3 41elsif ($^O eq 'cygwin' and $] < 5.012000) {
42 SKIP: { skip 'Static coderef reaping seems nonfunctional on cygwin < 5.12', 1 }
43}
2215d4b9 44else {
45 is(${$foo_ro->one},'yay', 'value present');
46 ok(Scalar::Util::isweak($foo_ro->{one}), 'value weakened');
47
48 { no warnings 'redefine'; *mk_ref = sub {} }
49 ok (!defined $foo_ro->{one}, 'optree reaped, ro static value gone');
50}
32381de9 51
52done_testing;