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