* role exclusion and aliasiing now works in composite roles too
[gitmo/Moose.git] / t / 030_roles / 013_method_aliasing_during_composition.t
CommitLineData
3e19778d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
28412c0b 6use Test::More tests => 31;
3e19778d 7use Test::Exception;
8
9BEGIN {
10 use_ok('Moose');
11}
12
13{
14 package My::Role;
15 use Moose::Role;
16
17 sub foo { 'Foo::foo' }
18 sub bar { 'Foo::bar' }
19 sub baz { 'Foo::baz' }
20
21 requires 'role_bar';
22
23 package My::Class;
24 use Moose;
25
26 ::lives_ok {
27 with 'My::Role' => { alias => { bar => 'role_bar' } };
28 } '... this succeeds';
43a41ede 29
30 package My::Class::Failure;
31 use Moose;
32
33 ::throws_ok {
34 with 'My::Role' => { alias => { bar => 'role_bar' } };
35 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
36
37 sub role_bar { 'FAIL' }
3e19778d 38}
39
43a41ede 40ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
3e19778d 41
42{
43 package My::OtherRole;
44 use Moose::Role;
45
46 ::lives_ok {
47 with 'My::Role' => { alias => { bar => 'role_bar' } };
48 } '... this succeeds';
49
50 sub bar { 'My::OtherRole::bar' }
43a41ede 51
52 package My::OtherRole::Failure;
53 use Moose::Role;
54
55 ::throws_ok {
56 with 'My::Role' => { alias => { bar => 'role_bar' } };
57 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
58
59 sub role_bar { 'FAIL' }
3e19778d 60}
61
43a41ede 62ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
63ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
3e19778d 64ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
65
28412c0b 66{
67 package Foo::Role;
68 use Moose::Role;
69
70 sub foo { 'Foo::Role::foo' }
71
72 package Bar::Role;
73 use Moose::Role;
74
75 sub foo { 'Bar::Role::foo' }
3e19778d 76
28412c0b 77 package Baz::Role;
78 use Moose::Role;
79
80 sub foo { 'Baz::Role::foo' }
81
82 package My::Foo::Class;
83 use Moose;
84
85 ::lives_ok {
86 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
87 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' },
88 'Baz::Role';
89 } '... composed our roles correctly';
90
91 package My::Foo::Class::Broken;
92 use Moose;
93
94 ::throws_ok {
95 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
96 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
97 'Baz::Role';
98 } qr/\'Foo::Role\|Bar::Role\|Baz::Role\' requires the method \'foo_foo\' to be implemented by \'My::Foo::Class::Broken\'/,
99 '... composed our roles correctly';
100}
3e19778d 101
28412c0b 102{
103 my $foo = My::Foo::Class->new;
104 isa_ok($foo, 'My::Foo::Class');
105 can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
106 is($foo->foo, 'Baz::Role::foo', '... got the right method');
107 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
108 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
109}
110
111{
112 package My::Foo::Role;
113 use Moose::Role;
114
115 ::lives_ok {
116 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
117 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' },
118 'Baz::Role';
119 } '... composed our roles correctly';
120}
121
122ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
123ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
124
125
126{
127 package My::Foo::Role::Other;
128 use Moose::Role;
129
130 ::lives_ok {
131 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
132 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
133 'Baz::Role';
134 } '... composed our roles correctly';
135}
3e19778d 136
28412c0b 137ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
138ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
3e19778d 139