Redid conversion to Test::Fatal
[gitmo/Moose.git] / t / 030_roles / 004_role_composition_errors.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Fatal;
8
9
10 {
11
12     package Foo::Role;
13     use Moose::Role;
14
15     requires 'foo';
16 }
17
18 is_deeply(
19     [ sort Foo::Role->meta->get_required_method_list ],
20     ['foo'],
21     '... the Foo::Role has a required method (foo)'
22 );
23
24 # classes which does not implement required method
25 {
26
27     package Foo::Class;
28     use Moose;
29
30     ::isnt( ::exception { with('Foo::Role') }, undef, '... no foo method implemented by Foo::Class' );
31 }
32
33 # class which does implement required method
34 {
35
36     package Bar::Class;
37     use Moose;
38
39     ::isnt( ::exception { with('Foo::Class') }, undef, '... cannot consume a class, it must be a role' );
40     ::is( ::exception { with('Foo::Role') }, undef, '... has a foo method implemented by Bar::Class' );
41
42     sub foo {'Bar::Class::foo'}
43 }
44
45 # role which does implement required method
46 {
47
48     package Bar::Role;
49     use Moose::Role;
50
51     ::is( ::exception { with('Foo::Role') }, undef, '... has a foo method implemented by Bar::Role' );
52
53     sub foo {'Bar::Role::foo'}
54 }
55
56 is_deeply(
57     [ sort Bar::Role->meta->get_required_method_list ],
58     [],
59     '... the Bar::Role has not inherited the required method from Foo::Role'
60 );
61
62 # role which does not implement required method
63 {
64
65     package Baz::Role;
66     use Moose::Role;
67
68     ::is( ::exception { with('Foo::Role') }, undef, '... no foo method implemented by Baz::Role' );
69 }
70
71 is_deeply(
72     [ sort Baz::Role->meta->get_required_method_list ],
73     ['foo'],
74     '... the Baz::Role has inherited the required method from Foo::Role'
75 );
76
77 # classes which does not implement required method
78 {
79
80     package Baz::Class;
81     use Moose;
82
83     ::isnt( ::exception { with('Baz::Role') }, undef, '... no foo method implemented by Baz::Class2' );
84 }
85
86 # class which does implement required method
87 {
88
89     package Baz::Class2;
90     use Moose;
91
92     ::is( ::exception { with('Baz::Role') }, undef, '... has a foo method implemented by Baz::Class2' );
93
94     sub foo {'Baz::Class2::foo'}
95 }
96
97
98 {
99     package Quux::Role;
100     use Moose::Role;
101
102     requires qw( meth1 meth2 meth3 meth4 );
103 }
104
105 # RT #41119
106 {
107
108     package Quux::Class;
109     use Moose;
110
111     ::like( ::exception { with('Quux::Role') }, qr/\Q'Quux::Role' requires the methods 'meth1', 'meth2', 'meth3', and 'meth4' to be implemented by 'Quux::Class'/, 'exception mentions all the missing required methods at once' );
112 }
113
114 {
115     package Quux::Class2;
116     use Moose;
117
118     sub meth1 { }
119
120     ::like( ::exception { with('Quux::Role') }, qr/'Quux::Role' requires the methods 'meth2', 'meth3', and 'meth4' to be implemented by 'Quux::Class2'/, 'exception mentions all the missing required methods at once, but not the one that exists' );
121 }
122
123 {
124     package Quux::Class3;
125     use Moose;
126
127     has 'meth1' => ( is => 'ro' );
128     has 'meth2' => ( is => 'ro' );
129
130     ::like( ::exception { with('Quux::Role') }, qr/'Quux::Role' requires the methods 'meth3' and 'meth4' to be implemented by 'Quux::Class3'/, 'exception mentions all the missing methods at once, but not the accessors' );
131 }
132
133 {
134     package Quux::Class4;
135     use Moose;
136
137     sub meth1 { }
138     has 'meth2' => ( is => 'ro' );
139
140     ::like( ::exception { with('Quux::Role') }, qr/'Quux::Role' requires the methods 'meth3' and 'meth4' to be implemented by 'Quux::Class4'/, 'exception mentions all the require methods that are accessors at once, as well as missing methods, but not the one that exists' );
141 }
142
143 done_testing;