5e182f008895ea0f6cfbff50b6f9ad65de3c4669
[gitmo/Moose.git] / t / 043_role_composition_errors.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
7 use Test::Exception;
8
9 BEGIN {  
10     use_ok('Moose');               
11 }
12
13 {
14     package Foo::Role;
15     use strict;
16     use warnings;
17     use Moose::Role;
18     
19     requires 'foo';
20 }
21
22 is_deeply(
23     [ sort Foo::Role->meta->get_required_method_list ],
24     [ 'foo' ],
25     '... the Foo::Role has a required method (foo)');
26
27 # classes which does not implement required method
28 {
29     package Foo::Class;
30     use strict;
31     use warnings;
32     use Moose;
33     
34     ::dies_ok { with('Foo::Role') } '... no foo method implemented by Foo::Class';
35 }
36
37 # class which does implement required method
38 {
39     package Bar::Class;
40     use strict;
41     use warnings;
42     use Moose;
43     
44     ::dies_ok  { with('Foo::Class') } '... cannot consume a class, it must be a role';
45     ::lives_ok { with('Foo::Role')  } '... has a foo method implemented by Bar::Class';
46     
47     sub foo { 'Bar::Class::foo' }
48 }
49
50 # role which does implement required method
51 {
52     package Bar::Role;
53     use strict;
54     use warnings;
55     use Moose::Role;
56     
57     ::lives_ok { with('Foo::Role') } '... has a foo method implemented by Bar::Role';
58     
59     sub foo { 'Bar::Role::foo' }
60 }
61
62 is_deeply(
63     [ sort Bar::Role->meta->get_required_method_list ],
64     [],
65     '... the Bar::Role has not inherited the required method from Foo::Role');
66
67 # role which does not implement required method
68 {
69     package Baz::Role;
70     use strict;
71     use warnings;
72     use Moose::Role;
73     
74     ::lives_ok { with('Foo::Role') } '... no foo method implemented by Baz::Role';
75 }
76
77 is_deeply(
78     [ sort Baz::Role->meta->get_required_method_list ],
79     [ 'foo' ],
80     '... the Baz::Role has inherited the required method from Foo::Role');
81     
82 # classes which does not implement required method
83 {
84     package Baz::Class;
85     use strict;
86     use warnings;
87     use Moose;
88
89     ::dies_ok { with('Baz::Role') } '... no foo method implemented by Baz::Class2';
90 }
91
92 # class which does implement required method
93 {
94     package Baz::Class2;
95     use strict;
96     use warnings;
97     use Moose;
98
99     ::lives_ok { with('Baz::Role') } '... has a foo method implemented by Baz::Class2';
100
101     sub foo { 'Baz::Class2::foo' }
102 }    
103     
104