d2ab4709b317280c0dcbb53138904d310f26814c
[gitmo/Moo.git] / t / accessor-roles.t
1 use strictures 1;
2 use Test::More;
3 use Test::Fatal;
4 use 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
17 my $combined = Moo::Role->create_class_with_roles('One', qw(One::P1 One::P2));
18 isa_ok $combined, "One";
19 ok $combined->does($_), "Does $_" for qw(One::P1 One::P2);
20
21 my $c = $combined->new;
22 is $c->one, "one",     "attr default set from class";
23 is $c->two, "two",     "attr default set from role";
24 is $c->three, "three", "attr default set from role";
25
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
40 is(Two->new->two, 'II', "overwriting accessors using +attr works");
41
42 done_testing;