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