Add various things
[gitmo/Mouse.git] / t / 030_roles / failing / 012_method_exclusion_in_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 19;
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     package My::Class;
20     use Mouse;
21
22     with 'My::Role' => { -excludes => 'bar' };
23 }
24
25 ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz);
26 ok(!My::Class->meta->has_method('bar'), '... but we excluded bar');
27
28 {
29     package My::OtherRole;
30     use Mouse::Role;
31
32     with 'My::Role' => { -excludes => 'foo' };
33
34     sub foo { 'My::OtherRole::foo' }
35     sub bar { 'My::OtherRole::bar' }
36 }
37
38 ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo bar baz);
39
40 ok(!My::OtherRole->meta->requires_method('foo'), '... and the &foo method is not required');
41 ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required');
42 use Data::Dumper; print Dumper(My::OtherRole->meta->{required_methods});
43 {
44     package Foo::Role;
45     use Mouse::Role;
46
47     sub foo { 'Foo::Role::foo' }
48
49     package Bar::Role;
50     use Mouse::Role;
51
52     sub foo { 'Bar::Role::foo' }
53
54     package Baz::Role;
55     use Mouse::Role;
56
57     sub foo { 'Baz::Role::foo' }
58
59     package My::Foo::Class;
60     use Mouse;
61
62     ::lives_ok {
63         with 'Foo::Role' => { -excludes => 'foo' },
64              'Bar::Role' => { -excludes => 'foo' },
65              'Baz::Role';
66     } '... composed our roles correctly';
67
68     package My::Foo::Class::Broken;
69     use Mouse;
70
71     ::throws_ok {
72         with 'Foo::Role',
73              'Bar::Role' => { -excludes => 'foo' },
74              'Baz::Role';
75     } 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'/,
76       '... composed our roles correctly';
77 }
78
79 {
80     my $foo = My::Foo::Class->new;
81     isa_ok($foo, 'My::Foo::Class');
82     can_ok($foo, 'foo');
83     is($foo->foo, 'Baz::Role::foo', '... got the right method');
84 }
85
86 {
87     package My::Foo::Role;
88     use Mouse::Role;
89
90     ::lives_ok {
91         with 'Foo::Role' => { -excludes => 'foo' },
92              'Bar::Role' => { -excludes => 'foo' },
93              'Baz::Role';
94     } '... composed our roles correctly';
95 }
96
97 ok(My::Foo::Role->meta->has_method('foo'), "we have a foo method");
98 ok(!My::Foo::Role->meta->requires_method('foo'), '... and the &foo method is not required');
99
100 {
101     package My::Foo::Role::Other;
102     use Mouse::Role;
103
104     ::lives_ok {
105         with 'Foo::Role',
106              'Bar::Role' => { -excludes => 'foo' },
107              'Baz::Role';
108     } '... composed our roles correctly';
109 }
110
111 ok(!My::Foo::Role::Other->meta->has_method('foo'), "we dont have a foo method");
112 ok(My::Foo::Role::Other->meta->requires_method('foo'), '... and the &foo method is required');
113
114
115