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