Doc super() warning in Changes
[gitmo/Moose.git] / t / roles / method_exclusion_in_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Fatal;
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     ::is( ::exception {
62         with 'Foo::Role' => { -excludes => 'foo' },
63              'Bar::Role' => { -excludes => 'foo' },
64              'Baz::Role';
65     }, undef, '... composed our roles correctly' );
66
67     package My::Foo::Class::Broken;
68     use Moose;
69
70     ::like( ::exception {
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'/, '... composed our roles correctly' );
75 }
76
77 {
78     my $foo = My::Foo::Class->new;
79     isa_ok($foo, 'My::Foo::Class');
80     can_ok($foo, 'foo');
81     is($foo->foo, 'Baz::Role::foo', '... got the right method');
82 }
83
84 {
85     package My::Foo::Role;
86     use Moose::Role;
87
88     ::is( ::exception {
89         with 'Foo::Role' => { -excludes => 'foo' },
90              'Bar::Role' => { -excludes => 'foo' },
91              'Baz::Role';
92     }, undef, '... composed our roles correctly' );
93 }
94
95 ok(My::Foo::Role->meta->has_method('foo'), "we have a foo method");
96 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
97
98 {
99     package My::Foo::Role::Other;
100     use Moose::Role;
101
102     ::is( ::exception {
103         with 'Foo::Role',
104              'Bar::Role' => { -excludes => 'foo' },
105              'Baz::Role';
106     }, undef, '... composed our roles correctly' );
107 }
108
109 ok(!My::Foo::Role::Other->meta->has_method('foo'), "we dont have a foo method");
110 ok(My::Foo::Role::Other->meta->requires_method('foo'), '... and the &foo method is required');
111
112 done_testing;