Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / roles / role_attrs.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use Moose ();
7 use Moose::Meta::Role;
8 use Moose::Util;
9
10 my $role1 = Moose::Meta::Role->initialize('Foo');
11 $role1->add_attribute( foo => ( is => 'ro' ) );
12
13 ok( $role1->has_attribute('foo'), 'Foo role has a foo attribute' );
14
15 my $foo_attr = $role1->get_attribute('foo');
16 is(
17     $foo_attr->associated_role->name, 'Foo',
18     'associated_role for foo attr is Foo role'
19 );
20
21 isa_ok(
22     $foo_attr->attribute_for_class('Moose::Meta::Attribute'),
23     'Moose::Meta::Attribute',
24     'attribute returned by ->attribute_for_class'
25 );
26
27 my $role2 = Moose::Meta::Role->initialize('Bar');
28 $role1->apply($role2);
29
30 ok( $role2->has_attribute('foo'), 'Bar role has a foo attribute' );
31
32 is(
33     $foo_attr->associated_role->name, 'Foo',
34     'associated_role for foo attr is still Foo role'
35 );
36
37 isa_ok(
38     $foo_attr->attribute_for_class('Moose::Meta::Attribute'),
39     'Moose::Meta::Attribute',
40     'attribute returned by ->attribute_for_class'
41 );
42
43 my $role3 = Moose::Meta::Role->initialize('Baz');
44 my $combined = Moose::Meta::Role->combine( [ $role1->name ], [ $role3->name ] );
45
46 ok( $combined->has_attribute('foo'), 'combined role has a foo attribute' );
47
48 is(
49     $foo_attr->associated_role->name, 'Foo',
50     'associated_role for foo attr is still Foo role'
51 );
52
53 done_testing;