summaryrefslogtreecommitdiff
path: root/t/roles/role_attr_application.t
blob: 05720e9f766f8a5f8159c783d3a34b79e558087c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use strict;
use warnings;
use Test::More;
use Test::Moose;
use Moose::Util qw( does_role );

{
    package Foo::Meta::Attribute;
    use Moose::Role;
}

{
    package Foo::Meta::Attribute2;
    use Moose::Role;
}

{
    package Foo::Role;
    use Moose::Role;

    has foo => (is => 'ro');
}

{
    package Foo;
    use Moose;
    Moose::Util::MetaRole::apply_metaroles(
        for => __PACKAGE__,
        class_metaroles => { attribute => ['Foo::Meta::Attribute'] },
        role_metaroles  => { applied_attribute => ['Foo::Meta::Attribute2'] },
    );
    with 'Foo::Role';

    has bar => (is => 'ro');
}

ok(Moose::Util::does_role(Foo->meta->get_attribute('bar'), 'Foo::Meta::Attribute'), "attrs defined in the class get the class metarole applied");
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");
ok(!Moose::Util::does_role(Foo->meta->get_attribute('foo'), 'Foo::Meta::Attribute'), "attrs defined in the role don't get the metarole applied");
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");

{
    package Bar::Meta::Attribute;
    use Moose::Role;
}

{
    package Bar::Meta::Attribute2;
    use Moose::Role;
}

{
    package Bar::Role;
    use Moose::Role;
    Moose::Util::MetaRole::apply_metaroles(
        for => __PACKAGE__,
        class_metaroles => { attribute => ['Bar::Meta::Attribute'] },
        role_metaroles  => { applied_attribute => ['Bar::Meta::Attribute2'] },
    );

    has foo => (is => 'ro');
}

{
    package Bar;
    use Moose;
    with 'Bar::Role';

    has bar => (is => 'ro');
}

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");
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");
ok(Moose::Util::does_role(Bar->meta->get_attribute('foo'), 'Bar::Meta::Attribute2'), "attrs defined in the role get the role metarole applied");
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");

{
    package Baz::Meta::Attribute;
    use Moose::Role;
}

{
    package Baz::Meta::Attribute2;
    use Moose::Role;
}

{
    package Baz::Role;
    use Moose::Role;
    Moose::Util::MetaRole::apply_metaroles(
        for => __PACKAGE__,
        class_metaroles => { attribute => ['Baz::Meta::Attribute'] },
        role_metaroles  => { applied_attribute => ['Baz::Meta::Attribute2'] },
    );

    has foo => (is => 'ro');
}

{
    package Baz;
    use Moose;
    Moose::Util::MetaRole::apply_metaroles(
        for => __PACKAGE__,
        class_metaroles => { attribute => ['Baz::Meta::Attribute'] },
        role_metaroles  => { applied_attribute => ['Baz::Meta::Attribute2'] },
    );
    with 'Baz::Role';

    has bar => (is => 'ro');
}

ok(Moose::Util::does_role(Baz->meta->get_attribute('bar'), 'Baz::Meta::Attribute'), "attrs defined in the class get the class metarole applied");
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");
ok(Moose::Util::does_role(Baz->meta->get_attribute('foo'), 'Baz::Meta::Attribute2'), "attrs defined in the role get the role metarole applied");
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");

{
    package Accessor::Modifying::Role;
    use Moose::Role;

    around _process_options => sub {
        my $orig = shift;
        my $self = shift;
        my ($name, $params) = @_;
        $self->$orig(@_);
        $params->{reader} .= '_foo';
    };
}

{
    package Plain::Role;
    use Moose::Role;

    has foo => (
        is  => 'ro',
        isa => 'Str',
    );
}

{
    package Class::With::Trait;
    use Moose;
    Moose::Util::MetaRole::apply_metaroles(
        for => __PACKAGE__,
        class_metaroles => {
            attribute => ['Accessor::Modifying::Role'],
        },
    );
    with 'Plain::Role';

    has bar => (
        is  => 'ro',
        isa => 'Str',
    );
}

{
    can_ok('Class::With::Trait', 'foo');
    can_ok('Class::With::Trait', 'bar_foo');
}

{
    package Role::With::Trait;
    use Moose::Role;
    Moose::Util::MetaRole::apply_metaroles(
        for => __PACKAGE__,
        role_metaroles => {
            applied_attribute => ['Accessor::Modifying::Role'],
        },
    );
    with 'Plain::Role';

    has foo => (
        is  => 'ro',
        isa => 'Str',
    );

    sub foo_test {
        my $self = shift;
        return $self->can('foo_foo');
    }
}

{
    package Class::With::Role::With::Trait;
    use Moose;
    with 'Role::With::Trait';

    has bar => (
        is  => 'ro',
        isa => 'Str',
    );

    sub bar_test {
        my $self = shift;
        return $self->can('bar');
    }
}

{
    can_ok('Class::With::Role::With::Trait', 'foo_foo');
    can_ok('Class::With::Role::With::Trait', 'bar');
}

{
    package Quux::Meta::Role::Attribute;
    use Moose::Role;
}

{
    package Quux::Role1;
    use Moose::Role;

    has foo => (traits => ['Quux::Meta::Role::Attribute'], is => 'ro');
    has baz => (is => 'ro');
}

{
    package Quux::Role2;
    use Moose::Role;
    Moose::Util::MetaRole::apply_metaroles(
        for            => __PACKAGE__,
        role_metaroles => {
            applied_attribute => ['Quux::Meta::Role::Attribute']
        },
    );

    has bar => (is => 'ro');
}

{
    package Quux;
    use Moose;
    with 'Quux::Role1', 'Quux::Role2';
}

{
    my $foo = Quux->meta->get_attribute('foo');
    does_ok($foo, 'Quux::Meta::Role::Attribute',
            "individual attribute trait applied correctly");

    my $baz = Quux->meta->get_attribute('baz');
    ok(! does_role($baz, 'Quux::Meta::Role::Attribute'),
       "applied_attribute traits do not end up applying to attributes from other roles during composition");

    my $bar = Quux->meta->get_attribute('bar');
    does_ok($bar, 'Quux::Meta::Role::Attribute',
            "attribute metarole applied correctly");
}

{
    package HasMeta;
    use Moose::Role;
    Moose::Util::MetaRole::apply_metaroles(
        for            => __PACKAGE__,
        role_metaroles => {
            applied_attribute => ['Quux::Meta::Role::Attribute']
        },
    );

    has foo => (is => 'ro');
}

{
    package NoMeta;
    use Moose::Role;

    with 'HasMeta';

    has bar => (is => 'ro');
}

{
    package ConsumesBoth;
    use Moose;
    with 'HasMeta', 'NoMeta';
}

{
    my $foo = ConsumesBoth->meta->get_attribute('foo');
    does_ok($foo, 'Quux::Meta::Role::Attribute',
            'applied_attribute traits are preserved when one role consumes another');

    my $bar = ConsumesBoth->meta->get_attribute('bar');
    ok(! does_role($bar, 'Quux::Meta::Role::Attribute'),
       "applied_attribute traits do not spill over from consumed role");
}



done_testing;