Add Test::Mouse
[gitmo/Mouse.git] / t / 030_roles / 002_role.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 40;
7 use Test::Exception;
8
9 use lib 't/lib';
10 use Test::Mouse;
11
12 use MooseCompat;
13
14 =pod
15
16 NOTE:
17
18 Should we be testing here that the has & override
19 are injecting their methods correctly? In other
20 words, should 'has_method' return true for them?
21
22 =cut
23
24 {
25     package FooRole;
26     use Mouse::Role;
27
28     our $VERSION = '0.01';
29
30     has 'bar' => (is => 'rw', isa => 'Foo');
31     has 'baz' => (is => 'ro');
32
33     sub foo { 'FooRole::foo' }
34     sub boo { 'FooRole::boo' }
35
36     before 'boo' => sub { "FooRole::boo:before" };
37
38     after  'boo' => sub { "FooRole::boo:after1"  };
39     after  'boo' => sub { "FooRole::boo:after2"  };
40
41     around 'boo' => sub { "FooRole::boo:around" };
42
43     override 'bling' => sub { "FooRole::bling:override" };
44     override 'fling' => sub { "FooRole::fling:override" };
45
46     ::dies_ok { extends() } '... extends() is not supported';
47     ::dies_ok { augment() } '... augment() is not supported';
48     ::dies_ok { inner()   } '... inner() is not supported';
49
50     no Mouse::Role;
51 }
52
53 my $foo_role = FooRole->meta;
54 isa_ok($foo_role, 'Mouse::Meta::Role');
55 #isa_ok($foo_role, 'Class::MOP::Module');
56
57 is($foo_role->name, 'FooRole', '... got the right name of FooRole');
58 is($foo_role->version, '0.01', '... got the right version of FooRole');
59
60 # methods ...
61
62
63 ok($foo_role->has_method('foo'), '... FooRole has the foo method');
64 is($foo_role->get_method('foo')->body, \&FooRole::foo, '... FooRole got the foo method');
65
66 isa_ok($foo_role->get_method('foo'), 'Mouse::Meta::Role::Method');
67
68 ok($foo_role->has_method('boo'), '... FooRole has the boo method');
69 is($foo_role->get_method('boo')->body, \&FooRole::boo, '... FooRole got the boo method');
70
71 isa_ok($foo_role->get_method('boo'), 'Mouse::Meta::Role::Method');
72
73 is_deeply(
74     [ sort $foo_role->get_method_list() ],
75     [ 'boo', 'foo', 'meta' ],
76     '... got the right method list');
77
78 ok(FooRole->can('foo'), "locally defined methods are still there");
79 ok(!FooRole->can('has'), "sugar was unimported");
80
81 # attributes ...
82
83 is_deeply(
84     [ sort $foo_role->get_attribute_list() ],
85     [ 'bar', 'baz' ],
86     '... got the right attribute list');
87
88 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
89
90 my $bar_attr = $foo_role->get_attribute('bar');
91 is($bar_attr->{is}, 'rw',
92    'bar attribute is rw');
93 is($bar_attr->{isa}, 'Foo',
94    'bar attribute isa Foo');
95 {
96     local $TODO = 'definition_context is not yet implemented';
97     is(ref($bar_attr->{definition_context}), 'HASH',
98        'bar\'s definition context is a hash');
99     is($bar_attr->{definition_context}->{package}, 'FooRole',
100        'bar was defined in FooRole');
101 }
102
103 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
104
105 my $baz_attr = $foo_role->get_attribute('baz');
106 is($baz_attr->{is}, 'ro',
107    'baz attribute is ro');
108
109 {
110     local $TODO = 'definition_context is not yet implemented';
111     is(ref($baz_attr->{definition_context}), 'HASH',
112        'bar\'s definition context is a hash');
113     is($baz_attr->{definition_context}->{package}, 'FooRole',
114        'baz was defined in FooRole');
115 }
116
117 # method modifiers
118
119 ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
120 is(($foo_role->get_before_method_modifiers('boo'))[0]->(),
121     "FooRole::boo:before",
122     '... got the right method back');
123
124 is_deeply(
125     [ $foo_role->get_method_modifier_list('before') ],
126     [ 'boo' ],
127     '... got the right list of before method modifiers');
128
129 ok($foo_role->has_after_method_modifiers('boo'), '... now we have a boo:after modifier');
130 is(($foo_role->get_after_method_modifiers('boo'))[0]->(),
131     "FooRole::boo:after1",
132     '... got the right method back');
133 is(($foo_role->get_after_method_modifiers('boo'))[1]->(),
134     "FooRole::boo:after2",
135     '... got the right method back');
136
137 is_deeply(
138     [ $foo_role->get_method_modifier_list('after') ],
139     [ 'boo' ],
140     '... got the right list of after method modifiers');
141
142 ok($foo_role->has_around_method_modifiers('boo'), '... now we have a boo:around modifier');
143 is(($foo_role->get_around_method_modifiers('boo'))[0]->(),
144     "FooRole::boo:around",
145     '... got the right method back');
146
147 is_deeply(
148     [ $foo_role->get_method_modifier_list('around') ],
149     [ 'boo' ],
150     '... got the right list of around method modifiers');
151
152 ## overrides
153
154 ok($foo_role->has_override_method_modifier('bling'), '... now we have a bling:override modifier');
155 is($foo_role->get_override_method_modifier('bling')->(),
156     "FooRole::bling:override",
157     '... got the right method back');
158
159 ok($foo_role->has_override_method_modifier('fling'), '... now we have a fling:override modifier');
160 is($foo_role->get_override_method_modifier('fling')->(),
161     "FooRole::fling:override",
162     '... got the right method back');
163
164 is_deeply(
165     [ sort $foo_role->get_method_modifier_list('override') ],
166     [ 'bling', 'fling' ],
167     '... got the right list of override method modifiers');
168