Perltidy this code a bit.
[gitmo/Moose.git] / t / 020_attributes / 004_attribute_triggers.t
CommitLineData
8c9d74e7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Scalar::Util 'isweak';
7
65e14c86 8use Test::More tests => 26;
8c9d74e7 9use Test::Exception;
10
11BEGIN {
12 use_ok('Moose');
13}
14
15{
16 package Foo;
8c9d74e7 17 use Moose;
18
19 has 'bar' => (is => 'rw',
ab76842e 20 isa => 'Maybe[Bar]',
8c9d74e7 21 trigger => sub {
22 my ($self, $bar) = @_;
23 $bar->foo($self) if defined $bar;
24 });
25
26 has 'baz' => (writer => 'set_baz',
27 reader => 'get_baz',
28 isa => 'Baz',
29 trigger => sub {
30 my ($self, $baz) = @_;
31 $baz->foo($self);
32 });
33
34
35 package Bar;
8c9d74e7 36 use Moose;
37
38 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
39
40 package Baz;
8c9d74e7 41 use Moose;
42
43 has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
44}
45
46{
47 my $foo = Foo->new;
48 isa_ok($foo, 'Foo');
49
50 my $bar = Bar->new;
51 isa_ok($bar, 'Bar');
52
53 my $baz = Baz->new;
54 isa_ok($baz, 'Baz');
55
56 lives_ok {
57 $foo->bar($bar);
58 } '... did not die setting bar';
59
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');
62
63 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
64
65 lives_ok {
66 $foo->bar(undef);
67 } '... did not die un-setting bar';
68
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');
71
72 # test the writer
73
74 lives_ok {
75 $foo->set_baz($baz);
76 } '... did not die setting baz';
77
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');
80
81 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
82}
83
84{
85 my $bar = Bar->new;
86 isa_ok($bar, 'Bar');
87
88 my $baz = Baz->new;
89 isa_ok($baz, 'Baz');
90
91 my $foo = Foo->new(bar => $bar, baz => $baz);
92 isa_ok($foo, 'Foo');
93
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');
96
97 ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
98
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');
101
102 ok(isweak($baz->{foo}), '... baz.foo is a weak reference');
103}
104
7eaef7ad 105# some errors
106
107{
108 package Bling;
7eaef7ad 109 use Moose;
110
111 ::dies_ok {
7eaef7ad 112 has('bling' => (is => 'rw', trigger => 'Fail'));
65e14c86 113 } '... a trigger must be a CODE ref';
7eaef7ad 114
115 ::dies_ok {
116 has('bling' => (is => 'rw', trigger => []));
65e14c86 117 } '... a trigger must be a CODE ref';
7eaef7ad 118}
119
120