Perltidy this code a bit.
[gitmo/Moose.git] / t / 030_roles / 013_method_aliasing_in_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 36;
7 use Test::Exception;
8
9 BEGIN {
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';
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' }
38 }
39
40 ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar);
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' }
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' }    
60 }
61
62 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
63 ok(!My::OtherRole->meta->requires_method('bar'), '... and the &bar method is not required');
64 ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required');
65
66 {
67     package My::AliasingRole;
68     use Moose::Role;
69
70     ::lives_ok {
71         with 'My::Role' => { alias => { bar => 'role_bar' } };
72     } '... this succeeds';
73 }
74
75 ok(My::AliasingRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar);
76 ok(My::AliasingRole->meta->requires_method('bar'), '... and the &bar method is required');
77
78 {
79     package Foo::Role;
80     use Moose::Role;
81     
82     sub foo { 'Foo::Role::foo' }
83     
84     package Bar::Role;
85     use Moose::Role;
86     
87     sub foo { 'Bar::Role::foo' }    
88
89     package Baz::Role;
90     use Moose::Role;
91     
92     sub foo { 'Baz::Role::foo' }   
93     
94     package My::Foo::Class;
95     use Moose;
96     
97     ::lives_ok {
98         with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
99              'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' }, 
100              'Baz::Role';
101     } '... composed our roles correctly';   
102     
103     package My::Foo::Class::Broken;
104     use Moose;
105     
106     ::throws_ok {
107         with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
108              'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, 
109              'Baz::Role';
110     } qr/\'Foo::Role\|Bar::Role\|Baz::Role\' requires the method \'foo_foo\' to be implemented by \'My::Foo::Class::Broken\'/, 
111       '... composed our roles correctly';    
112 }
113
114 {
115     my $foo = My::Foo::Class->new;
116     isa_ok($foo, 'My::Foo::Class');
117     can_ok($foo, $_) for qw/foo foo_foo bar_foo/;
118     is($foo->foo, 'Baz::Role::foo', '... got the right method');
119     is($foo->foo_foo, 'Foo::Role::foo', '... got the right method');    
120     is($foo->bar_foo, 'Bar::Role::foo', '... got the right method');        
121 }
122
123 {
124     package My::Foo::Role;
125     use Moose::Role;
126
127     ::lives_ok {
128         with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
129              'Bar::Role' => { alias => { 'foo' => 'bar_foo' }, excludes => 'foo' }, 
130              'Baz::Role';
131     } '... composed our roles correctly';
132 }
133
134 ok(My::Foo::Role->meta->has_method($_), "we have a $_ method") for qw/foo foo_foo bar_foo/;;
135 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
136
137
138 {
139     package My::Foo::Role::Other;
140     use Moose::Role;
141
142     ::lives_ok {
143         with 'Foo::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' },
144              'Bar::Role' => { alias => { 'foo' => 'foo_foo' }, excludes => 'foo' }, 
145              'Baz::Role';
146     } '... composed our roles correctly';
147 }
148
149 ok(!My::Foo::Role::Other->meta->has_method('foo_foo'), "we dont have a foo_foo method");
150 ok(My::Foo::Role::Other->meta->requires_method('foo_foo'), '... and the &foo method is required');
151