Commit | Line | Data |
67199842 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 11; |
7 | use Test::Exception; |
8 | use Scalar::Util 'blessed'; |
9 | |
10 | |
11 | |
12 | |
13 | { |
14 | package Dog; |
15 | use Mouse::Role; |
16 | |
17 | sub talk { 'woof' } |
18 | |
19 | has fur => ( |
20 | isa => "Str", |
21 | is => "rw", |
22 | default => "dirty", |
23 | ); |
24 | |
25 | package Foo; |
26 | use Mouse; |
27 | |
28 | has 'dog' => ( |
29 | is => 'rw', |
30 | does => 'Dog', |
31 | ); |
32 | } |
33 | |
34 | my $obj = Foo->new; |
6cfa1e5e |
35 | isa_ok($obj, 'Foo'); |
67199842 |
36 | |
37 | ok(!$obj->can( 'talk' ), "... the role is not composed yet"); |
38 | ok(!$obj->can( 'fur' ), 'ditto'); |
39 | ok(!$obj->does('Dog'), '... we do not do any roles yet'); |
40 | |
41 | dies_ok { |
42 | $obj->dog($obj) |
43 | } '... and setting the accessor fails (not a Dog yet)'; |
44 | |
45 | Dog->meta->apply($obj); |
46 | |
47 | ok($obj->does('Dog'), '... we now do the Bark role'); |
48 | ok($obj->can('talk'), "... the role is now composed at the object level"); |
49 | ok($obj->can('fur'), "it has fur"); |
50 | |
51 | is($obj->talk, 'woof', '... got the right return value for the newly composed method'); |
52 | |
53 | lives_ok { |
54 | $obj->dog($obj) |
55 | } '... and setting the accessor is okay'; |
56 | |
57 | is($obj->fur, "dirty", "role attr initialized"); |