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