8 has one => (is => 'ro', default => sub { 'one' });
10 package One::P1; use Moo::Role;
11 has two => (is => 'ro', default => sub { 'two' });
13 package One::P2; use Moo::Role;
14 has three => (is => 'ro', default => sub { 'three' });
15 has four => (is => 'ro', lazy => 1, default => sub { 'four' }, predicate => 1);
17 package One::P3; use Moo::Role;
18 has '+three' => (is => 'ro', default => sub { 'three' });
21 my $combined = Moo::Role->create_class_with_roles('One', qw(One::P1 One::P2));
22 isa_ok $combined, "One";
23 ok $combined->does($_), "Does $_" for qw(One::P1 One::P2);
25 my $c = $combined->new;
26 is $c->one, "one", "attr default set from class";
27 is $c->two, "two", "attr default set from role";
28 is $c->three, "three", "attr default set from role";
31 package Deux; use Moo; with 'One::P1';
33 ::exception { has two => (is => 'ro', default => sub { 'II' }); },
34 qr{^You cannot overwrite a locally defined method \(two\) with a reader},
35 'overwriting accesssors with roles fails'
40 package Two; use Moo; with 'One::P1';
41 has '+two' => (is => 'ro', default => sub { 'II' });
44 is(Two->new->two, 'II', "overwriting accessors using +attr works");
47 Moo::Role->apply_roles_to_object($o, 'One::P2');
48 is($o->three, 'three', 'attr default set from role applied to object');
49 ok(!$o->has_four, 'lazy attr default not set on apply');
51 $o = $combined->new(three => '3');
52 Moo::Role->apply_roles_to_object($o, 'One::P3');
53 is($o->three, '3', 'attr default not used when already set when role applied to object');