roles-do-roles
[gitmo/Moose.git] / t / 041_role.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 35;
7 use Test::Exception;
8
9 BEGIN {  
10     use_ok('Moose::Role');               
11 }
12
13 {
14     package FooRole;
15     
16     use strict;
17     use warnings;
18     use Moose::Role;
19     
20     our $VERSION = '0.01';
21     
22     has 'bar' => (is => 'rw', isa => 'Foo');
23     has 'baz' => (is => 'ro');    
24     
25     sub foo { 'FooRole::foo' }
26     sub boo { 'FooRole::boo' }    
27     
28     before 'boo' => sub { "FooRole::boo:before" };
29     
30     after  'boo' => sub { "FooRole::boo:after1"  }; 
31     after  'boo' => sub { "FooRole::boo:after2"  };        
32     
33     around 'boo' => sub { "FooRole::boo:around" };  
34     
35     override 'bling' => sub { "FooRole::bling:override" };   
36     override 'fling' => sub { "FooRole::fling:override" };  
37     
38     ::dies_ok { extends() } '... extends() is not supported';
39     ::dies_ok { augment() } '... augment() is not supported';    
40     ::dies_ok { inner()   } '... inner() is not supported';        
41 }
42
43 my $foo_role = FooRole->meta;
44 isa_ok($foo_role, 'Moose::Meta::Role');
45
46 isa_ok($foo_role->_role_meta, 'Class::MOP::Class');
47
48 is($foo_role->name, 'FooRole', '... got the right name of FooRole');
49 is($foo_role->version, '0.01', '... got the right version of FooRole');
50
51 # methods ...
52
53 ok($foo_role->has_method('foo'), '... FooRole has the foo method');
54 is($foo_role->get_method('foo'), \&FooRole::foo, '... FooRole got the foo method');
55
56 isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method');
57
58 ok($foo_role->has_method('boo'), '... FooRole has the boo method');
59 is($foo_role->get_method('boo'), \&FooRole::boo, '... FooRole got the boo method');
60
61 isa_ok($foo_role->get_method('boo'), 'Moose::Meta::Role::Method');
62
63 is_deeply(
64     [ sort $foo_role->get_method_list() ],
65     [ 'boo', 'foo' ],
66     '... got the right method list');
67     
68 # attributes ...
69
70 is_deeply(
71     [ sort $foo_role->get_attribute_list() ],
72     [ 'bar', 'baz' ],
73     '... got the right attribute list');
74
75 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
76
77 is_deeply(
78     $foo_role->get_attribute('bar'),
79     { is => 'rw', isa => 'Foo' },
80     '... got the correct description of the bar attribute');
81
82 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
83
84 is_deeply(
85     $foo_role->get_attribute('baz'),
86     { is => 'ro' },
87     '... got the correct description of the baz attribute');
88
89 # method modifiers
90
91 ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
92 is(($foo_role->get_before_method_modifiers('boo'))[0]->(), 
93     "FooRole::boo:before", 
94     '... got the right method back');
95
96 is_deeply(
97     [ $foo_role->get_method_modifier_list('before') ],
98     [ 'boo' ],
99     '... got the right list of before method modifiers');
100
101 ok($foo_role->has_after_method_modifiers('boo'), '... now we have a boo:after modifier');
102 is(($foo_role->get_after_method_modifiers('boo'))[0]->(), 
103     "FooRole::boo:after1", 
104     '... got the right method back');
105 is(($foo_role->get_after_method_modifiers('boo'))[1]->(), 
106     "FooRole::boo:after2", 
107     '... got the right method back');    
108
109 is_deeply(
110     [ $foo_role->get_method_modifier_list('after') ],
111     [ 'boo' ],
112     '... got the right list of after method modifiers');
113     
114 ok($foo_role->has_around_method_modifiers('boo'), '... now we have a boo:around modifier');
115 is(($foo_role->get_around_method_modifiers('boo'))[0]->(), 
116     "FooRole::boo:around", 
117     '... got the right method back');
118
119 is_deeply(
120     [ $foo_role->get_method_modifier_list('around') ],
121     [ 'boo' ],
122     '... got the right list of around method modifiers');
123
124 ## overrides
125
126 ok($foo_role->has_override_method_modifier('bling'), '... now we have a bling:override modifier');
127 is($foo_role->get_override_method_modifier('bling')->(), 
128     "FooRole::bling:override", 
129     '... got the right method back');
130
131 ok($foo_role->has_override_method_modifier('fling'), '... now we have a fling:override modifier');
132 is($foo_role->get_override_method_modifier('fling')->(), 
133     "FooRole::fling:override", 
134     '... got the right method back');
135
136 is_deeply(
137     [ sort $foo_role->get_method_modifier_list('override') ],
138     [ 'bling', 'fling' ],
139     '... got the right list of override method modifiers');
140