Commit | Line | Data |
c4538447 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
c4538447 |
8 | |
7ff56534 |
9 | |
c4538447 |
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 | |
c8b8d92f |
21 | with 'My::Role' => { -excludes => 'bar' }; |
c4538447 |
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 | |
c8b8d92f |
31 | with 'My::Role' => { -excludes => 'foo' }; |
c4538447 |
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 | |
28412c0b |
42 | { |
43 | package Foo::Role; |
44 | use Moose::Role; |
d03bd989 |
45 | |
28412c0b |
46 | sub foo { 'Foo::Role::foo' } |
d03bd989 |
47 | |
28412c0b |
48 | package Bar::Role; |
49 | use Moose::Role; |
d03bd989 |
50 | |
51 | sub foo { 'Bar::Role::foo' } |
28412c0b |
52 | |
53 | package Baz::Role; |
54 | use Moose::Role; |
d03bd989 |
55 | |
56 | sub foo { 'Baz::Role::foo' } |
57 | |
28412c0b |
58 | package My::Foo::Class; |
59 | use Moose; |
d03bd989 |
60 | |
b10dde3a |
61 | ::is( ::exception { |
c8b8d92f |
62 | with 'Foo::Role' => { -excludes => 'foo' }, |
63 | 'Bar::Role' => { -excludes => 'foo' }, |
28412c0b |
64 | 'Baz::Role'; |
b10dde3a |
65 | }, undef, '... composed our roles correctly' ); |
d03bd989 |
66 | |
28412c0b |
67 | package My::Foo::Class::Broken; |
68 | use Moose; |
d03bd989 |
69 | |
b10dde3a |
70 | ::like( ::exception { |
28412c0b |
71 | with 'Foo::Role', |
c8b8d92f |
72 | 'Bar::Role' => { -excludes => 'foo' }, |
28412c0b |
73 | 'Baz::Role'; |
b10dde3a |
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' ); |
28412c0b |
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 | |
b10dde3a |
88 | ::is( ::exception { |
c8b8d92f |
89 | with 'Foo::Role' => { -excludes => 'foo' }, |
90 | 'Bar::Role' => { -excludes => 'foo' }, |
28412c0b |
91 | 'Baz::Role'; |
b10dde3a |
92 | }, undef, '... composed our roles correctly' ); |
28412c0b |
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 | |
b10dde3a |
102 | ::is( ::exception { |
28412c0b |
103 | with 'Foo::Role', |
c8b8d92f |
104 | 'Bar::Role' => { -excludes => 'foo' }, |
28412c0b |
105 | 'Baz::Role'; |
b10dde3a |
106 | }, undef, '... composed our roles correctly' ); |
28412c0b |
107 | } |
c4538447 |
108 | |
28412c0b |
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'); |
c4538447 |
111 | |
a28e50e4 |
112 | done_testing; |