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