6 use Test::More no_plan => 1;
11 use_ok('Moose::Role');
16 Mutually recursive roles.
28 sub bar { 'Role::Foo::bar' }
37 sub foo { 'Role::Bar::foo' }
47 with 'Role::Foo', 'Role::Bar';
48 } '... our mutually recursive roles combine okay';
56 with 'Role::Bar', 'Role::Foo';
57 } '... our mutually recursive roles combine okay (no matter what order)';
60 my $test1 = My::Test1->new;
61 isa_ok($test1, 'My::Test1');
63 ok($test1->does('Role::Foo'), '... $test1 does Role::Foo');
64 ok($test1->does('Role::Bar'), '... $test1 does Role::Bar');
66 can_ok($test1, 'foo');
67 can_ok($test1, 'bar');
69 is($test1->foo, 'Role::Bar::foo', '... $test1->foo worked');
70 is($test1->bar, 'Role::Foo::bar', '... $test1->bar worked');
72 my $test2 = My::Test2->new;
73 isa_ok($test2, 'My::Test2');
75 ok($test2->does('Role::Foo'), '... $test2 does Role::Foo');
76 ok($test2->does('Role::Bar'), '... $test2 does Role::Bar');
78 can_ok($test2, 'foo');
79 can_ok($test2, 'bar');
81 is($test2->foo, 'Role::Bar::foo', '... $test2->foo worked');
82 is($test2->bar, 'Role::Foo::bar', '... $test2->bar worked');
84 # check some meta-stuff
86 ok(Role::Foo->meta->has_method('bar'), '... it still has the bar method');
87 ok(Role::Foo->meta->requires_method('foo'), '... it still has the required foo method');
89 ok(Role::Bar->meta->has_method('foo'), '... it still has the foo method');
90 ok(Role::Bar->meta->requires_method('bar'), '... it still has the required bar method');
104 sub bling { 'Role::Bling::bling' }
106 package Role::Bling::Bling;
111 sub bling { 'Role::Bling::Bling::bling' }
121 with 'Role::Bling', 'Role::Bling::Bling';
122 } qr/requires the method \'bling\' to be implemented/, '... role methods conflicted and method was required';
131 with 'Role::Bling::Bling';
132 } '... role methods didnt conflict when manually combined';
140 with 'Role::Bling::Bling';
142 } '... role methods didnt conflict when manually combined (in opposite order)';
150 with 'Role::Bling::Bling', 'Role::Bling';
151 } '... role methods didnt conflict when manually resolved';
153 sub bling { 'My::Test6::bling' }
156 ok(!My::Test3->meta->has_method('bling'), '... we didnt get any methods in the conflict');
157 ok(My::Test4->meta->has_method('bling'), '... we did get the method when manually dealt with');
158 ok(My::Test5->meta->has_method('bling'), '... we did get the method when manually dealt with');
159 ok(My::Test6->meta->has_method('bling'), '... we did get the method when manually dealt with');
161 is(My::Test4->bling, 'Role::Bling::bling', '... and we got the first method that was added');
162 is(My::Test5->bling, 'Role::Bling::Bling::bling', '... and we got the first method that was added');
163 is(My::Test6->bling, 'My::Test6::bling', '... and we got the local method');
165 # check how this affects role compostion
168 package Role::Bling::Bling::Bling;
173 with 'Role::Bling::Bling';
175 sub bling { 'Role::Bling::Bling::Bling::bling' }
178 ok(Role::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling');
179 ok(Role::Bling::Bling::Bling->meta->has_method('bling'), '... still got the bling method in Role::Bling::Bling::Bling');
183 Role attribute conflicts
193 has 'ghost' => (is => 'ro', default => 'Role::Boo::ghost');
195 package Role::Boo::Hoo;
200 has 'ghost' => (is => 'ro', default => 'Role::Boo::Hoo::ghost');
210 with 'Role::Boo', 'Role::Boo::Hoo';
211 } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/,
212 '... role attrs conflicted and method was required';
221 with 'Role::Boo::Hoo';
222 } '... role attrs didnt conflict when manually combined';
230 with 'Role::Boo::Hoo';
232 } '... role attrs didnt conflict when manually combined';
239 has 'ghost' => (is => 'ro', default => 'My::Test10::ghost');
242 with 'Role::Boo', 'Role::Boo::Hoo';
243 } qr/Role \'Role::Boo::Hoo\' has encountered an attribute conflict/,
244 '... role attrs conflicted and cannot be manually disambiguted';
248 ok(!My::Test7->meta->has_attribute('ghost'), '... we didnt get any attributes in the conflict');
249 ok(My::Test8->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
250 ok(My::Test9->meta->has_attribute('ghost'), '... we did get an attributes when manually composed');
251 ok(My::Test10->meta->has_attribute('ghost'), '... we did still have an attribute ghost (conflict does not mess with class)');
253 can_ok('My::Test8', 'ghost');
254 can_ok('My::Test9', 'ghost');
255 can_ok('My::Test10', 'ghost');
257 is(My::Test8->new->ghost, 'Role::Boo::ghost', '... got the expected default attr value');
258 is(My::Test9->new->ghost, 'Role::Boo::Hoo::ghost', '... got the expected default attr value');
259 is(My::Test10->new->ghost, 'My::Test10::ghost', '... got the expected default attr value');