6 use Scalar::Util 'isweak';
16 has 'bar' => (is => 'rw',
19 my ($self, $bar) = @_;
20 $bar->foo($self) if defined $bar;
23 has 'baz' => (writer => 'set_baz',
27 my ($self, $baz) = @_;
35 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
40 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
55 } '... did not die setting bar';
57 is($foo->bar, $bar, '... set the value foo.bar correctly');
58 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
60 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
64 } '... did not die un-setting bar';
66 is($foo->bar, undef, '... set the value foo.bar correctly');
67 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
73 } '... did not die setting baz';
75 is($foo->get_baz, $baz, '... set the value foo.baz correctly');
76 is($baz->foo, $foo, '... which in turn set the value baz.foo correctly');
78 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
88 my $foo = Foo->new(bar => $bar, baz => $baz);
91 is($foo->bar, $bar, '... set the value foo.bar correctly');
92 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
94 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
96 is($foo->get_baz, $baz, '... set the value foo.baz correctly');
97 is($baz->foo, $foo, '... which in turn set the value baz.foo correctly');
99 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
109 has('bling' => (is => 'rw', trigger => 'Fail'));
110 } '... a trigger must be a CODE ref';
113 has('bling' => (is => 'rw', trigger => []));
114 } '... a trigger must be a CODE ref';
117 # Triggers do not fire on built values
125 has foo => (is => 'rw', default => sub { 'default foo value' },
126 trigger => sub { my ($self, $val, $attr) = @_;
127 $trigger_calls{foo}++;
128 $trigger_vals{foo} = $val });
129 has bar => (is => 'rw', lazy_build => 1,
130 trigger => sub { my ($self, $val, $attr) = @_;
131 $trigger_calls{bar}++;
132 $trigger_vals{bar} = $val });
133 sub _build_bar { return 'default bar value' }
134 has baz => (is => 'rw', builder => '_build_baz',
135 trigger => sub { my ($self, $val, $attr) = @_;
136 $trigger_calls{baz}++;
137 $trigger_vals{baz} = $val });
138 sub _build_baz { return 'default baz value' }
143 lives_ok { $blarg = Blarg->new; } 'Blarg->new() lives';
144 ok($blarg, 'Have a $blarg');
145 foreach my $attr (qw/foo bar baz/) {
146 is($blarg->$attr(), "default $attr value", "$attr has default value");
148 is_deeply(\%Blarg::trigger_calls, {}, 'No triggers fired');
149 foreach my $attr (qw/foo bar baz/) {
150 $blarg->$attr("Different $attr value");
152 is_deeply(\%Blarg::trigger_calls, { map { $_ => 1 } qw/foo bar baz/ }, 'All triggers fired once on assign');
153 is_deeply(\%Blarg::trigger_vals, { map { $_ => "Different $_ value" } qw/foo bar baz/ }, 'All triggers given assigned values');
155 lives_ok { $blarg => Blarg->new( map { $_ => "Yet another $_ value" } qw/foo bar baz/ ) } '->new() with parameters';
156 is_deeply(\%Blarg::trigger_calls, { map { $_ => 2 } qw/foo bar baz/ }, 'All triggers fired once on construct');
157 is_deeply(\%Blarg::trigger_vals, { map { $_ => "Yet another $_ value" } qw/foo bar baz/ }, 'All triggers given assigned values');
160 # Triggers do not receive the meta-attribute as an argument, but do
161 # receive the old value
167 has foo => (is => 'rw', trigger => sub { push @calls, [@_] });
171 my $attr = Foo->meta->get_attribute('foo');
174 $attr->set_value( $foo, 2 );
179 'trigger called correctly on initial set via meta-API',
183 $attr->set_value( $foo, 3 );
188 'trigger called correctly on second set via meta-API',
192 $attr->set_raw_value( $foo, 4 );
197 'trigger not called using set_raw_value method',
203 my $foo = Foo->new(foo => 2);
207 'trigger called correctly on construction',
215 'trigger called correctly on set (with old value)',
218 Foo->meta->make_immutable, redo if Foo->meta->is_mutable;