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