fix punctuation
[gitmo/Moose.git] / t / roles / role_composition_errors.t
CommitLineData
e46edf94 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
e46edf94 8
7ff56534 9
e46edf94 10{
4c035b82 11
e46edf94 12 package Foo::Role;
e46edf94 13 use Moose::Role;
4c035b82 14
e46edf94 15 requires 'foo';
16}
17
fa1be058 18is_deeply(
19 [ sort Foo::Role->meta->get_required_method_list ],
4c035b82 20 ['foo'],
21 '... the Foo::Role has a required method (foo)'
22);
fa1be058 23
e46edf94 24# classes which does not implement required method
25{
4c035b82 26
e46edf94 27 package Foo::Class;
e46edf94 28 use Moose;
4c035b82 29
b10dde3a 30 ::isnt( ::exception { with('Foo::Role') }, undef, '... no foo method implemented by Foo::Class' );
e46edf94 31}
32
33# class which does implement required method
34{
4c035b82 35
e46edf94 36 package Bar::Class;
e46edf94 37 use Moose;
4c035b82 38
b10dde3a 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' );
4c035b82 41
42 sub foo {'Bar::Class::foo'}
e46edf94 43}
44
45# role which does implement required method
46{
4c035b82 47
e46edf94 48 package Bar::Role;
e46edf94 49 use Moose::Role;
4c035b82 50
b10dde3a 51 ::is( ::exception { with('Foo::Role') }, undef, '... has a foo method implemented by Bar::Role' );
4c035b82 52
53 sub foo {'Bar::Role::foo'}
e46edf94 54}
55
fa1be058 56is_deeply(
57 [ sort Bar::Role->meta->get_required_method_list ],
58 [],
4c035b82 59 '... the Bar::Role has not inherited the required method from Foo::Role'
60);
fa1be058 61
e46edf94 62# role which does not implement required method
63{
4c035b82 64
e46edf94 65 package Baz::Role;
fa1be058 66 use Moose::Role;
4c035b82 67
b10dde3a 68 ::is( ::exception { with('Foo::Role') }, undef, '... no foo method implemented by Baz::Role' );
e46edf94 69}
70
fa1be058 71is_deeply(
72 [ sort Baz::Role->meta->get_required_method_list ],
4c035b82 73 ['foo'],
74 '... the Baz::Role has inherited the required method from Foo::Role'
75);
76
fa1be058 77# classes which does not implement required method
78{
4c035b82 79
fa1be058 80 package Baz::Class;
fa1be058 81 use Moose;
82
b10dde3a 83 ::isnt( ::exception { with('Baz::Role') }, undef, '... no foo method implemented by Baz::Class2' );
fa1be058 84}
85
86# class which does implement required method
87{
4c035b82 88
fa1be058 89 package Baz::Class2;
fa1be058 90 use Moose;
91
b10dde3a 92 ::is( ::exception { with('Baz::Role') }, undef, '... has a foo method implemented by Baz::Class2' );
fa1be058 93
4c035b82 94 sub foo {'Baz::Class2::foo'}
95}
fa1be058 96
d939e016 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
b10dde3a 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' );
d939e016 112}
113
114{
115 package Quux::Class2;
116 use Moose;
117
118 sub meth1 { }
119
b10dde3a 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' );
d939e016 121}
122
123{
124 package Quux::Class3;
125 use Moose;
126
127 has 'meth1' => ( is => 'ro' );
128 has 'meth2' => ( is => 'ro' );
129
b10dde3a 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' );
d939e016 131}
132
133{
134 package Quux::Class4;
135 use Moose;
136
137 sub meth1 { }
138 has 'meth2' => ( is => 'ro' );
139
b10dde3a 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' );
d939e016 141}
a28e50e4 142
143done_testing;