docs-n-attr-refactor
[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 =pod
14
15 NOTE:
16
17 Should we be testing here that the has & override
18 are injecting their methods correctly? In other 
19 words, should 'has_method' return true for them?
20
21 =cut
22
23 {
24     package FooRole;
25     
26     use strict;
27     use warnings;
28     use Moose::Role;
29     
30     our $VERSION = '0.01';
31     
32     has 'bar' => (is => 'rw', isa => 'Foo');
33     has 'baz' => (is => 'ro');    
34     
35     sub foo { 'FooRole::foo' }
36     sub boo { 'FooRole::boo' }    
37     
38     before 'boo' => sub { "FooRole::boo:before" };
39     
40     after  'boo' => sub { "FooRole::boo:after1"  }; 
41     after  'boo' => sub { "FooRole::boo:after2"  };        
42     
43     around 'boo' => sub { "FooRole::boo:around" };  
44     
45     override 'bling' => sub { "FooRole::bling:override" };   
46     override 'fling' => sub { "FooRole::fling:override" };  
47     
48     ::dies_ok { extends() } '... extends() is not supported';
49     ::dies_ok { augment() } '... augment() is not supported';    
50     ::dies_ok { inner()   } '... inner() is not supported';        
51 }
52
53 my $foo_role = FooRole->meta;
54 isa_ok($foo_role, 'Moose::Meta::Role');
55
56 isa_ok($foo_role->_role_meta, 'Class::MOP::Class');
57
58 is($foo_role->name, 'FooRole', '... got the right name of FooRole');
59 is($foo_role->version, '0.01', '... got the right version of FooRole');
60
61 # methods ...
62
63 ok($foo_role->has_method('foo'), '... FooRole has the foo method');
64 is($foo_role->get_method('foo'), \&FooRole::foo, '... FooRole got the foo method');
65
66 isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method');
67
68 ok($foo_role->has_method('boo'), '... FooRole has the boo method');
69 is($foo_role->get_method('boo'), \&FooRole::boo, '... FooRole got the boo method');
70
71 isa_ok($foo_role->get_method('boo'), 'Moose::Meta::Role::Method');
72
73 is_deeply(
74     [ sort $foo_role->get_method_list() ],
75     [ 'boo', 'foo' ],
76     '... got the right method list');
77     
78 # attributes ...
79
80 is_deeply(
81     [ sort $foo_role->get_attribute_list() ],
82     [ 'bar', 'baz' ],
83     '... got the right attribute list');
84
85 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
86
87 is_deeply(
88     $foo_role->get_attribute('bar'),
89     { is => 'rw', isa => 'Foo' },
90     '... got the correct description of the bar attribute');
91
92 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
93
94 is_deeply(
95     $foo_role->get_attribute('baz'),
96     { is => 'ro' },
97     '... got the correct description of the baz attribute');
98
99 # method modifiers
100
101 ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
102 is(($foo_role->get_before_method_modifiers('boo'))[0]->(), 
103     "FooRole::boo:before", 
104     '... got the right method back');
105
106 is_deeply(
107     [ $foo_role->get_method_modifier_list('before') ],
108     [ 'boo' ],
109     '... got the right list of before method modifiers');
110
111 ok($foo_role->has_after_method_modifiers('boo'), '... now we have a boo:after modifier');
112 is(($foo_role->get_after_method_modifiers('boo'))[0]->(), 
113     "FooRole::boo:after1", 
114     '... got the right method back');
115 is(($foo_role->get_after_method_modifiers('boo'))[1]->(), 
116     "FooRole::boo:after2", 
117     '... got the right method back');    
118
119 is_deeply(
120     [ $foo_role->get_method_modifier_list('after') ],
121     [ 'boo' ],
122     '... got the right list of after method modifiers');
123     
124 ok($foo_role->has_around_method_modifiers('boo'), '... now we have a boo:around modifier');
125 is(($foo_role->get_around_method_modifiers('boo'))[0]->(), 
126     "FooRole::boo:around", 
127     '... got the right method back');
128
129 is_deeply(
130     [ $foo_role->get_method_modifier_list('around') ],
131     [ 'boo' ],
132     '... got the right list of around method modifiers');
133
134 ## overrides
135
136 ok($foo_role->has_override_method_modifier('bling'), '... now we have a bling:override modifier');
137 is($foo_role->get_override_method_modifier('bling')->(), 
138     "FooRole::bling:override", 
139     '... got the right method back');
140
141 ok($foo_role->has_override_method_modifier('fling'), '... now we have a fling:override modifier');
142 is($foo_role->get_override_method_modifier('fling')->(), 
143     "FooRole::fling:override", 
144     '... got the right method back');
145
146 is_deeply(
147     [ sort $foo_role->get_method_modifier_list('override') ],
148     [ 'bling', 'fling' ],
149     '... got the right list of override method modifiers');
150