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