6 package One; use Role::Tiny;
7 around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
8 package Two; use Role::Tiny;
9 around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
10 package Three; use Role::Tiny;
11 around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
12 package Four; use Role::Tiny;
13 around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
14 package Base; sub foo { __PACKAGE__ }
18 [ qw(One Two Three Four) ],
19 [ qw(Two Four Three) ],
22 my $combined = Role::Tiny->create_class_with_roles('Base', @$combo);
24 [ $combined->foo ], [ reverse(@$combo), 'Base' ],
27 my $object = bless({}, 'Base');
28 Role::Tiny->apply_roles_to_object($object, @$combo);
29 is(ref($object), $combined, 'Object reblessed into correct class');
36 has attr1 => (is => 'ro', default => -1);
38 package RoleWithAttr2;
41 has attr2 => (is => 'ro', default => -2);
43 package ClassWithAttr;
46 has attr3 => (is => 'ro', default => -3);
49 Moo::Role->apply_roles_to_package('ClassWithAttr', 'RoleWithAttr', 'RoleWithAttr2');
50 my $o = ClassWithAttr->new(attr1 => 1, attr2 => 2, attr3 => 3);
51 is($o->attr1, 1, 'attribute from role works');
52 is($o->attr2, 2, 'attribute from role 2 works');
53 is($o->attr3, 3, 'attribute from base class works');
56 package SubClassWithoutAttr;
58 extends 'ClassWithAttr';
61 my $o2 = Moo::Role->create_class_with_roles(
62 'SubClassWithoutAttr', 'RoleWithAttr')->new;
63 is($o2->attr3, -3, 'constructor includes base class');
64 is($o2->attr2, -2, 'constructor includes role');
67 package AccessorExtension;
69 around 'generate_method' => sub {
72 my ($into, $name) = @_;
75 *{"${into}::_${name}_marker"} = sub { };
82 requires '_attr1_marker';
86 package ClassWithExtension;
88 Moo::Role->apply_roles_to_object(
89 Moo->_accessor_maker_for(__PACKAGE__),
92 with qw(RoleWithAttr RoleWithReq);
93 }, undef, 'apply_roles_to_object correctly calls accessor generator';
101 package RoleWithReq2;
107 Moo::Role->create_class_with_roles(
108 'EmptyClass', 'RoleWithReq2', 'RoleWithAttr2');
109 }, undef, 'create_class_with_roles accepts attributes for requirements';
112 Moo::Role->create_class_with_roles(
113 'EmptyClass', 'RoleWithReq2', 'RoleWithAttr');
114 }, qr/Can't apply .* missing attr2/,
115 'create_class_with_roles accepts attributes for requirements';