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