inlining for overloaded object isa/coerce
[gitmo/Moo.git] / t / accessor-roles.t
CommitLineData
c69190f1 1use strictures 1;
2use Test::More;
2e512d30 3use Test::Fatal;
c69190f1 4use Sub::Quote;
5
6{
7 package One; use Moo;
8 has one => (is => 'ro', default => sub { 'one' });
9
10 package One::P1; use Moo::Role;
11 has two => (is => 'ro', default => sub { 'two' });
12
13 package One::P2; use Moo::Role;
14 has three => (is => 'ro', default => sub { 'three' });
15}
16
17my $combined = Moo::Role->create_class_with_roles('One', qw(One::P1 One::P2));
18isa_ok $combined, "One";
19ok $combined->does($_), "Does $_" for qw(One::P1 One::P2);
20
21my $c = $combined->new;
22is $c->one, "one", "attr default set from class";
23is $c->two, "two", "attr default set from role";
24is $c->three, "three", "attr default set from role";
25
2e512d30 26{
27 package Deux; use Moo; with 'One::P1';
28 ::like(
29 ::exception { has two => (is => 'ro', default => sub { 'II' }); },
30 qr{^You cannot overwrite a locally defined method \(two\) with a reader},
31 'overwriting accesssors with roles fails'
32 );
33}
34
35{
36 package Two; use Moo; with 'One::P1';
37 has '+two' => (is => 'ro', default => sub { 'II' });
38}
39
40is(Two->new->two, 'II', "overwriting accessors using +attr works");
41
c69190f1 42done_testing;