tests and changelogging
[gitmo/Moose.git] / t / 030_roles / 046_role_attr_application.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5
6 {
7     package Foo::Meta::Attribute;
8     use Moose::Role;
9 }
10
11 {
12     package Foo::Meta::Attribute2;
13     use Moose::Role;
14 }
15
16 {
17     package Foo::Role;
18     use Moose::Role;
19
20     has foo => (is => 'ro');
21 }
22
23 {
24     package Foo;
25     use Moose;
26     Moose::Util::MetaRole::apply_metaroles(
27         for => __PACKAGE__,
28         class_metaroles => { attribute => ['Foo::Meta::Attribute'] },
29         role_metaroles  => { applied_attribute => ['Foo::Meta::Attribute2'] },
30     );
31     with 'Foo::Role';
32
33     has bar => (is => 'ro');
34 }
35
36 ok(Moose::Util::does_role(Foo->meta->get_attribute('bar'), 'Foo::Meta::Attribute'), "attrs defined in the class get the class metarole applied");
37 ok(!Moose::Util::does_role(Foo->meta->get_attribute('bar'), 'Foo::Meta::Attribute2'), "attrs defined in the class don't get the role metarole applied");
38 ok(!Moose::Util::does_role(Foo->meta->get_attribute('foo'), 'Foo::Meta::Attribute'), "attrs defined in the role don't get the metarole applied");
39 ok(!Moose::Util::does_role(Foo->meta->get_attribute('foo'), 'Foo::Meta::Attribute'), "attrs defined in the role don't get the role metarole defined in the class applied");
40
41 {
42     package Bar::Meta::Attribute;
43     use Moose::Role;
44 }
45
46 {
47     package Bar::Meta::Attribute2;
48     use Moose::Role;
49 }
50
51 {
52     package Bar::Role;
53     use Moose::Role;
54     Moose::Util::MetaRole::apply_metaroles(
55         for => __PACKAGE__,
56         class_metaroles => { attribute => ['Bar::Meta::Attribute'] },
57         role_metaroles  => { applied_attribute => ['Bar::Meta::Attribute2'] },
58     );
59
60     has foo => (is => 'ro');
61 }
62
63 {
64     package Bar;
65     use Moose;
66     with 'Bar::Role';
67
68     has bar => (is => 'ro');
69 }
70
71 ok(!Moose::Util::does_role(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute'), "attrs defined in the class don't get the class metarole from the role applied");
72 ok(!Moose::Util::does_role(Bar->meta->get_attribute('bar'), 'Bar::Meta::Attribute2'), "attrs defined in the class don't get the role metarole applied");
73 ok(Moose::Util::does_role(Bar->meta->get_attribute('foo'), 'Bar::Meta::Attribute2'), "attrs defined in the role get the role metarole applied");
74 ok(!Moose::Util::does_role(Bar->meta->get_attribute('foo'), 'Bar::Meta::Attribute'), "attrs defined in the role don't get the class metarole applied");
75
76 {
77     package Baz::Meta::Attribute;
78     use Moose::Role;
79 }
80
81 {
82     package Baz::Meta::Attribute2;
83     use Moose::Role;
84 }
85
86 {
87     package Baz::Role;
88     use Moose::Role;
89     Moose::Util::MetaRole::apply_metaroles(
90         for => __PACKAGE__,
91         class_metaroles => { attribute => ['Baz::Meta::Attribute'] },
92         role_metaroles  => { applied_attribute => ['Baz::Meta::Attribute2'] },
93     );
94
95     has foo => (is => 'ro');
96 }
97
98 {
99     package Baz;
100     use Moose;
101     Moose::Util::MetaRole::apply_metaroles(
102         for => __PACKAGE__,
103         class_metaroles => { attribute => ['Baz::Meta::Attribute'] },
104         role_metaroles  => { applied_attribute => ['Baz::Meta::Attribute2'] },
105     );
106     with 'Baz::Role';
107
108     has bar => (is => 'ro');
109 }
110
111 ok(Moose::Util::does_role(Baz->meta->get_attribute('bar'), 'Baz::Meta::Attribute'), "attrs defined in the class get the class metarole applied");
112 ok(!Moose::Util::does_role(Baz->meta->get_attribute('bar'), 'Baz::Meta::Attribute2'), "attrs defined in the class don't get the role metarole applied");
113 ok(Moose::Util::does_role(Baz->meta->get_attribute('foo'), 'Baz::Meta::Attribute2'), "attrs defined in the role get the role metarole applied");
114 ok(!Moose::Util::does_role(Baz->meta->get_attribute('foo'), 'Baz::Meta::Attribute'), "attrs defined in the role don't get the class metarole applied");
115
116 {
117     package Accessor::Modifying::Role;
118     use Moose::Role;
119
120     around _process_options => sub {
121         my $orig = shift;
122         my $self = shift;
123         my ($name, $params) = @_;
124         $self->$orig(@_);
125         $params->{reader} .= '_foo';
126     };
127 }
128
129 {
130     package Plain::Role;
131     use Moose::Role;
132
133     has foo => (
134         is  => 'ro',
135         isa => 'Str',
136     );
137 }
138
139 {
140     package Class::With::Trait;
141     use Moose;
142     Moose::Util::MetaRole::apply_metaroles(
143         for => __PACKAGE__,
144         class_metaroles => {
145             attribute => ['Accessor::Modifying::Role'],
146         },
147     );
148     with 'Plain::Role';
149
150     has bar => (
151         is  => 'ro',
152         isa => 'Str',
153     );
154 }
155
156 {
157     can_ok('Class::With::Trait', 'foo');
158     can_ok('Class::With::Trait', 'bar_foo');
159 }
160
161 {
162     package Role::With::Trait;
163     use Moose::Role;
164     Moose::Util::MetaRole::apply_metaroles(
165         for => __PACKAGE__,
166         role_metaroles => {
167             applied_attribute => ['Accessor::Modifying::Role'],
168         },
169     );
170     with 'Plain::Role';
171
172     has foo => (
173         is  => 'ro',
174         isa => 'Str',
175     );
176
177     sub foo_test {
178         my $self = shift;
179         return $self->can('foo_foo');
180     }
181 }
182
183 {
184     package Class::With::Role::With::Trait;
185     use Moose;
186     with 'Role::With::Trait';
187
188     has bar => (
189         is  => 'ro',
190         isa => 'Str',
191     );
192
193     sub bar_test {
194         my $self = shift;
195         return $self->can('bar');
196     }
197 }
198
199 {
200     can_ok('Class::With::Role::With::Trait', 'foo_foo');
201     can_ok('Class::With::Role::With::Trait', 'bar');
202 }
203
204 done_testing;