Perltidy this code a bit.
[gitmo/Moose.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 tests => 12;
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
21     has fur => (
22         isa => "Str",
23         is  => "rw",
24         default => "dirty",
25     );
26
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");
40 ok(!$obj->can( 'fur' ), 'ditto');
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");
51 ok($obj->can('fur'), "it has fur");
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
59 is($obj->fur, "dirty", "role attr initialized");