Commit | Line | Data |
c69190f1 |
1 | use strictures 1; |
2 | use Test::More; |
2e512d30 |
3 | use Test::Fatal; |
c69190f1 |
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 | |
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 | |
40 | is(Two->new->two, 'II', "overwriting accessors using +attr works"); |
41 | |
c69190f1 |
42 | done_testing; |