Benchmark-DKbench
view release on metacpan or search on metacpan
data/t/attributes/attribute_triggers.t view on Meta::CPAN
use strict;
use warnings;
use Scalar::Util 'isweak';
use Test::More;
use Test::Fatal;
{
package Foo;
use Moose;
has 'bar' => (is => 'rw',
isa => 'Maybe[Bar]',
trigger => sub {
my ($self, $bar) = @_;
$bar->foo($self) if defined $bar;
});
has 'baz' => (writer => 'set_baz',
reader => 'get_baz',
isa => 'Baz',
trigger => sub {
my ($self, $baz) = @_;
$baz->foo($self);
});
package Bar;
use Moose;
has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
package Baz;
use Moose;
has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
}
{
my $foo = Foo->new;
isa_ok($foo, 'Foo');
my $bar = Bar->new;
isa_ok($bar, 'Bar');
my $baz = Baz->new;
isa_ok($baz, 'Baz');
is( exception {
$foo->bar($bar);
}, undef, '... did not die setting bar' );
is($foo->bar, $bar, '... set the value foo.bar correctly');
is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
is( exception {
$foo->bar(undef);
}, undef, '... did not die un-setting bar' );
is($foo->bar, undef, '... set the value foo.bar correctly');
is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
# test the writer
( run in 0.610 second using v1.01-cache-2.11-cpan-54e63673c56 )