6 use Scalar::Util 'isweak';
8 use Test::More tests => 43;
19 has 'bar' => (is => 'rw',
22 my ($self, $bar) = @_;
23 $bar->foo($self) if defined $bar;
26 has 'baz' => (writer => 'set_baz',
30 my ($self, $baz) = @_;
38 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
43 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
58 } '... did not die setting bar';
60 is($foo->bar, $bar, '... set the value foo.bar correctly');
61 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
63 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
67 } '... did not die un-setting bar';
69 is($foo->bar, undef, '... set the value foo.bar correctly');
70 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
76 } '... did not die setting baz';
78 is($foo->get_baz, $baz, '... set the value foo.baz correctly');
79 is($baz->foo, $foo, '... which in turn set the value baz.foo correctly');
81 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
91 my $foo = Foo->new(bar => $bar, baz => $baz);
94 is($foo->bar, $bar, '... set the value foo.bar correctly');
95 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
97 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
99 is($foo->get_baz, $baz, '... set the value foo.baz correctly');
100 is($baz->foo, $foo, '... which in turn set the value baz.foo correctly');
102 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
105 # before/around/after triggers
117 my ($self, $str, @args) = @_;
118 push(@{$self->calls}, $str);
126 $_[0]->called('before');
129 my ($ori, $self, $val, @whatever) = @_;
130 $self->called('around');
131 $ori->($self, $val.'-diddly', @whatever);
134 $_[0]->called('after');
140 sub fancy_trigger_tests
146 } "... $type constructor";
147 my $expected_calls = [qw(before around after)];
149 is_deeply($blah->calls, $expected_calls, "$type default triggered");
150 is($blah->noise, 'Sartak-diddly', "$type default around modified value");
151 @{$blah->calls} = ();
153 $blah->noise('argle-bargle');
154 is_deeply($blah->calls, $expected_calls, "$type set triggered");
155 is($blah->noise, 'argle-bargle-diddly', "$type set around modified value");
157 $blah = Fweet->new(noise => 'woot');
158 is_deeply($blah->calls, $expected_calls, "$type constructor triggered");
159 is($blah->noise, 'woot-diddly', "$type constructor around modified value");
163 fancy_trigger_tests('normal');
165 Fweet->meta->make_immutable;
166 } '... make_immutable works';
167 fancy_trigger_tests('inline');
177 has('bling' => (is => 'rw', trigger => {FAIL => sub {}}));
178 } '... hash specifier has to be before/around/after';
181 has('bling' => (is => 'rw', trigger => {around => 'FAIL'}));
182 } '... hash specifier value must be CODE ref';
185 has('bling' => (is => 'rw', trigger => 'Fail'));
186 } '... a trigger must be a CODE or HASH ref';
189 has('bling' => (is => 'rw', trigger => []));
190 } '... a trigger must be a CODE or HASH ref';