A tiny tweak
[gitmo/Mouse.git] / t / 030_roles / failing / 013_method_aliasing_in_composition.t
CommitLineData
67199842 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 35;
7use Test::Exception;
8
9
10
11{
12 package My::Role;
13 use Mouse::Role;
14
15 sub foo { 'Foo::foo' }
16 sub bar { 'Foo::bar' }
17 sub baz { 'Foo::baz' }
c2d7552a 18
67199842 19 requires 'role_bar';
20
21 package My::Class;
22 use Mouse;
23
24 ::lives_ok {
25 with 'My::Role' => { alias => { bar => 'role_bar' } };
26 } '... this succeeds';
c2d7552a 27
67199842 28 package My::Class::Failure;
29 use Mouse;
30
31 ::throws_ok {
32 with 'My::Role' => { alias => { bar => 'role_bar' } };
c2d7552a 33 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
34
67199842 35 sub role_bar { 'FAIL' }
36}
37
38ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
39
40{
41 package My::OtherRole;
42 use Mouse::Role;
43
44 ::lives_ok {
45 with 'My::Role' => { alias => { bar => 'role_bar' } };
46 } '... this succeeds';
47
48 sub bar { 'My::OtherRole::bar' }
c2d7552a 49
67199842 50 package My::OtherRole::Failure;
51 use Mouse::Role;
52
53 ::throws_ok {
54 with 'My::Role' => { alias => { bar => 'role_bar' } };
c2d7552a 55 } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';
56
57 sub role_bar { 'FAIL' }
67199842 58}
59
60ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
61ok(!My::OtherRole->meta->requires_method('bar'), '... and the &bar method is not required');
62ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
63
64{
65 package My::AliasingRole;
66 use Mouse::Role;
67
68 ::lives_ok {
69 with 'My::Role' => { alias => { bar => 'role_bar' } };
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{
77 package Foo::Role;
78 use Mouse::Role;
c2d7552a 79
67199842 80 sub foo { 'Foo::Role::foo' }
c2d7552a 81
67199842 82 package Bar::Role;
83 use Mouse::Role;
c2d7552a 84
85 sub foo { 'Bar::Role::foo' }
67199842 86
87 package Baz::Role;
88 use Mouse::Role;
c2d7552a 89
90 sub foo { 'Baz::Role::foo' }
91
67199842 92 package My::Foo::Class;
93 use Mouse;
c2d7552a 94
67199842 95 ::lives_ok {
96 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
c2d7552a 97 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' },
67199842 98 'Baz::Role';
c2d7552a 99 } '... composed our roles correctly';
100
67199842 101 package My::Foo::Class::Broken;
102 use Mouse;
c2d7552a 103
67199842 104 ::throws_ok {
105 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
c2d7552a 106 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
67199842 107 'Baz::Role';
c2d7552a 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'/,
109 '... composed our roles correctly';
67199842 110}
111
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');
c2d7552a 117 is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');
118 is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');
67199842 119}
120
121{
122 package My::Foo::Role;
123 use Mouse::Role;
124
125 ::lives_ok {
126 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
c2d7552a 127 'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' },
67199842 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 Mouse::Role;
139
140 ::lives_ok {
141 with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
c2d7552a 142 'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
67199842 143 'Baz::Role';
144 } '... composed our roles correctly';
145}
146
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');
149