test array delegation edge cases
[gitmo/Moose.git] / t / 030_roles / 012_method_exclusion_in_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9
10 {
11     package My::Role;
12     use Moose::Role;
13
14     sub foo { 'Foo::foo' }
15     sub bar { 'Foo::bar' }
16     sub baz { 'Foo::baz' }
17
18     package My::Class;
19     use Moose;
20
21     with 'My::Role' => { -excludes => 'bar' };
22 }
23
24 ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz);
25 ok(!My::Class->meta->has_method('bar'), '... but we excluded bar');
26
27 {
28     package My::OtherRole;
29     use Moose::Role;
30
31     with 'My::Role' => { -excludes => 'foo' };
32
33     sub foo { 'My::OtherRole::foo' }
34     sub bar { 'My::OtherRole::bar' }
35 }
36
37 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo bar baz);
38
39 ok(!My::OtherRole->meta->requires_method('foo'), '... and the &foo method is not required');
40 ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
41
42 {
43     package Foo::Role;
44     use Moose::Role;
45
46     sub foo { 'Foo::Role::foo' }
47
48     package Bar::Role;
49     use Moose::Role;
50
51     sub foo { 'Bar::Role::foo' }
52
53     package Baz::Role;
54     use Moose::Role;
55
56     sub foo { 'Baz::Role::foo' }
57
58     package My::Foo::Class;
59     use Moose;
60
61     ::lives_ok {
62         with 'Foo::Role' => { -excludes => 'foo' },
63              'Bar::Role' => { -excludes => 'foo' },
64              'Baz::Role';
65     } '... composed our roles correctly';
66
67     package My::Foo::Class::Broken;
68     use Moose;
69
70     ::throws_ok {
71         with 'Foo::Role',
72              'Bar::Role' => { -excludes => 'foo' },
73              'Baz::Role';
74     } qr/Due to a method name conflict in roles 'Baz::Role' and 'Foo::Role', the method 'foo' must be implemented or excluded by 'My::Foo::Class::Broken'/,
75       '... composed our roles correctly';
76 }
77
78 {
79     my $foo = My::Foo::Class->new;
80     isa_ok($foo, 'My::Foo::Class');
81     can_ok($foo, 'foo');
82     is($foo->foo, 'Baz::Role::foo', '... got the right method');
83 }
84
85 {
86     package My::Foo::Role;
87     use Moose::Role;
88
89     ::lives_ok {
90         with 'Foo::Role' => { -excludes => 'foo' },
91              'Bar::Role' => { -excludes => 'foo' },
92              'Baz::Role';
93     } '... composed our roles correctly';
94 }
95
96 ok(My::Foo::Role->meta->has_method('foo'), "we have a foo method");
97 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
98
99 {
100     package My::Foo::Role::Other;
101     use Moose::Role;
102
103     ::lives_ok {
104         with 'Foo::Role',
105              'Bar::Role' => { -excludes => 'foo' },
106              'Baz::Role';
107     } '... composed our roles correctly';
108 }
109
110 ok(!My::Foo::Role::Other->meta->has_method('foo'), "we dont have a foo method");
111 ok(My::Foo::Role::Other->meta->requires_method('foo'), '... and the &foo method is required');
112
113 done_testing;