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