2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
9 use Scalar::Util 'isweak';
12 $TODO = q{Mouse is not yet completed};
20 has 'bar' => (is => 'rw',
23 my ($self, $bar) = @_;
24 $bar->foo($self) if defined $bar;
27 has 'baz' => (writer => 'set_baz',
31 my ($self, $baz) = @_;
39 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
44 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
59 } '... did not die setting bar';
61 is($foo->bar, $bar, '... set the value foo.bar correctly');
62 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
64 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
68 } '... did not die un-setting bar';
70 is($foo->bar, undef, '... set the value foo.bar correctly');
71 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
77 } '... did not die setting baz';
79 is($foo->get_baz, $baz, '... set the value foo.baz correctly');
80 is($baz->foo, $foo, '... which in turn set the value baz.foo correctly');
82 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
92 my $foo = Foo->new(bar => $bar, baz => $baz);
95 is($foo->bar, $bar, '... set the value foo.bar correctly');
96 is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
98 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
100 is($foo->get_baz, $baz, '... set the value foo.baz correctly');
101 is($baz->foo, $foo, '... which in turn set the value baz.foo correctly');
103 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
113 has('bling' => (is => 'rw', trigger => 'Fail'));
114 } '... a trigger must be a CODE ref';
117 has('bling' => (is => 'rw', trigger => []));
118 } '... a trigger must be a CODE ref';
121 # Triggers do not fire on built values
129 has foo => (is => 'rw', default => sub { 'default foo value' },
130 trigger => sub { my ($self, $val, $attr) = @_;
131 $trigger_calls{foo}++;
132 $trigger_vals{foo} = $val });
133 has bar => (is => 'rw', lazy_build => 1,
134 trigger => sub { my ($self, $val, $attr) = @_;
135 $trigger_calls{bar}++;
136 $trigger_vals{bar} = $val });
137 sub _build_bar { return 'default bar value' }
138 has baz => (is => 'rw', builder => '_build_baz',
139 trigger => sub { my ($self, $val, $attr) = @_;
140 $trigger_calls{baz}++;
141 $trigger_vals{baz} = $val });
142 sub _build_baz { return 'default baz value' }
147 lives_ok { $blarg = Blarg->new; } 'Blarg->new() lives';
148 ok($blarg, 'Have a $blarg');
149 foreach my $attr (qw/foo bar baz/) {
150 is($blarg->$attr(), "default $attr value", "$attr has default value");
152 is_deeply(\%Blarg::trigger_calls, {}, 'No triggers fired');
153 foreach my $attr (qw/foo bar baz/) {
154 $blarg->$attr("Different $attr value");
156 is_deeply(\%Blarg::trigger_calls, { map { $_ => 1 } qw/foo bar baz/ }, 'All triggers fired once on assign');
157 is_deeply(\%Blarg::trigger_vals, { map { $_ => "Different $_ value" } qw/foo bar baz/ }, 'All triggers given assigned values');
159 lives_ok { $blarg => Blarg->new( map { $_ => "Yet another $_ value" } qw/foo bar baz/ ) } '->new() with parameters';
160 is_deeply(\%Blarg::trigger_calls, { map { $_ => 2 } qw/foo bar baz/ }, 'All triggers fired once on construct');
161 is_deeply(\%Blarg::trigger_vals, { map { $_ => "Yet another $_ value" } qw/foo bar baz/ }, 'All triggers given assigned values');
164 # Triggers do not receive the meta-attribute as an argument, but do
165 # receive the old value
171 has foo => (is => 'rw', trigger => sub { push @calls, [@_] });
175 my $attr = Foo->meta->get_attribute('foo');
178 $attr->set_value( $foo, 2 );
183 'trigger called correctly on initial set via meta-API',
187 $attr->set_value( $foo, 3 );
192 'trigger called correctly on second set via meta-API',
196 $attr->set_raw_value( $foo, 4 );
201 'trigger not called using set_raw_value method',
207 my $foo = Foo->new(foo => 2);
211 'trigger called correctly on construction',
219 'trigger called correctly on set (with old value)',
222 Foo->meta->make_immutable, redo if Foo->meta->is_mutable;