Implemented Mouse::Role->does; modified Mouse::Meta::Class->initialise
[gitmo/Mouse.git] / t / 030_roles / failing / 013_method_aliasing_in_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 35;
7 use 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' }
18     
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';
27     
28     package My::Class::Failure;
29     use Mouse;
30
31     ::throws_ok {
32         with 'My::Role' => { alias => { bar => 'role_bar' } };
33     } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';    
34     
35     sub role_bar { 'FAIL' }
36 }
37
38 ok(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' }
49     
50     package My::OtherRole::Failure;
51     use Mouse::Role;
52
53     ::throws_ok {
54         with 'My::Role' => { alias => { bar => 'role_bar' } };
55     } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds';    
56     
57     sub role_bar { 'FAIL' }    
58 }
59
60 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
61 ok(!My::OtherRole->meta->requires_method('bar'), '... and the &bar method is not required');
62 ok(!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
73 ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
74 ok(My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is required');
75
76 {
77     package Foo::Role;
78     use Mouse::Role;
79     
80     sub foo { 'Foo::Role::foo' }
81     
82     package Bar::Role;
83     use Mouse::Role;
84     
85     sub foo { 'Bar::Role::foo' }    
86
87     package Baz::Role;
88     use Mouse::Role;
89     
90     sub foo { 'Baz::Role::foo' }   
91     
92     package My::Foo::Class;
93     use Mouse;
94     
95     ::lives_ok {
96         with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
97              'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' }, 
98              'Baz::Role';
99     } '... composed our roles correctly';   
100     
101     package My::Foo::Class::Broken;
102     use Mouse;
103     
104     ::throws_ok {
105         with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
106              'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, 
107              'Baz::Role';
108     } qr/\'Foo::Role\|Bar::Role\|Baz::Role\' requires the method \'foo_foo\' to be implemented by \'My::Foo::Class::Broken\'/, 
109       '... composed our roles correctly';    
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');
117     is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');    
118     is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');        
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' },
127              'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' }, 
128              'Baz::Role';
129     } '... composed our roles correctly';
130 }
131
132 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
133 ok(!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' },
142              'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, 
143              'Baz::Role';
144     } '... composed our roles correctly';
145 }
146
147 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
148 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
149