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