1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
use strict;
use warnings;
use Test::More;
require Role::Tiny;
{
package My::Does::Basic1;
use Role::Tiny;
requires 'turbo_charger';
sub method {
return __PACKAGE__ . " method";
}
}
{
package My::Does::Basic2;
use Role::Tiny;
requires 'turbo_charger';
sub method2 {
return __PACKAGE__ . " method2";
}
}
eval <<'END_PACKAGE';
package My::Class1;
use Role::Tiny 'with';
with qw(
My::Does::Basic1
My::Does::Basic2
);
sub turbo_charger {}
END_PACKAGE
ok !$@, 'We should be able to use two roles with the same requirements'
or die $@;
{
package My::Does::Basic3;
use Role::Tiny;
with 'My::Does::Basic2';
sub method3 {
return __PACKAGE__ . " method3";
}
}
eval <<'END_PACKAGE';
package My::Class2;
use Role::Tiny 'with';
with qw(
My::Does::Basic3
);
sub new { bless {} => shift }
sub turbo_charger {}
END_PACKAGE
ok !$@, 'We should be able to use roles which consume roles'
or die $@;
can_ok 'My::Class2', 'method2';
is My::Class2->method2, 'My::Does::Basic2 method2',
'... and it should be the correct method';
can_ok 'My::Class2', 'method3';
is My::Class2->method3, 'My::Does::Basic3 method3',
'... and it should be the correct method';
ok My::Class2->Role::Tiny::does_role('My::Does::Basic3'), 'A class DOES roles which it consumes';
ok My::Class2->Role::Tiny::does_role('My::Does::Basic2'),
'... and should do roles which its roles consumes';
ok !My::Class2->Role::Tiny::does_role('My::Does::Basic1'),
'... but not roles which it never consumed';
my $object = My::Class2->new;
ok $object->Role::Tiny::does_role('My::Does::Basic3'), 'An instance DOES roles which its class consumes';
ok $object->Role::Tiny::does_role('My::Does::Basic2'),
'... and should do roles which its roles consumes';
ok !$object->Role::Tiny::does_role('My::Does::Basic1'),
'... but not roles which it never consumed';
{
package GenAccessors;
BEGIN { $INC{'GenAccessors.pm'} = __FILE__ }
sub import {
my ( $class, @methods ) = @_;
my $target = caller;
foreach my $method (@methods) {
no strict 'refs';
*{"${target}::${method}"} = sub {
@_ > 1 ? $_[0]->{$method} = $_[1] : $_[0]->{$method};
};
}
}
}
{
{
package Role::Which::Imports;
use Role::Tiny;
use GenAccessors qw(this that);
}
{
package Class::With::ImportingRole;
use Role::Tiny 'with';
with 'Role::Which::Imports';
sub new { bless {} => shift }
}
my $o = Class::With::ImportingRole->new;
foreach my $method (qw/this that/) {
can_ok $o, $method;
ok $o->$method($method), '... and calling "allow"ed methods should succeed';
is $o->$method, $method, '... and it should function correctly';
}
}
{
{
package Role::WithImportsOnceRemoved;
use Role::Tiny;
with 'Role::Which::Imports';
}
{
package Class::With::ImportingRole2;
use Role::Tiny 'with';
with 'Role::WithImportsOnceRemoved';
sub new { bless {} => shift }
}
ok my $o = Class::With::ImportingRole2->new,
'We should be able to use roles which compose roles which import';
foreach my $method (qw/this that/) {
can_ok $o, $method;
ok $o->$method($method), '... and calling "allow"ed methods should succeed';
is $o->$method, $method, '... and it should function correctly';
}
}
{
{
package Method::Role1;
use Role::Tiny;
sub method1 { }
requires 'method2';
}
{
package Method::Role2;
use Role::Tiny;
sub method2 { }
requires 'method1';
}
my $success = eval q{
package Class;
use Role::Tiny::With;
with 'Method::Role1', 'Method::Role2';
1;
};
is $success, 1, 'composed mutually dependent methods successfully' or diag "Error: $@";
}
SKIP: {
skip "Class::Method::Modifiers not installed or too old", 1
unless eval "use Class::Method::Modifiers 1.05; 1";
{
package Modifier::Role1;
use Role::Tiny;
sub foo {
}
before 'bar', sub {};
}
{
package Modifier::Role2;
use Role::Tiny;
sub bar {
}
before 'foo', sub {};
}
my $success = eval q{
package Class;
use Role::Tiny::With;
with 'Modifier::Role1', 'Modifier::Role2';
1;
};
is $success, 1, 'composed mutually dependent modifiers successfully' or diag "Error: $@";
}
{
{
package Base::Role;
use Role::Tiny;
requires qw/method1 method2/;
}
{
package Sub::Role1;
use Role::Tiny;
with 'Base::Role';
sub method1 {}
}
{
package Sub::Role2;
use Role::Tiny;
with 'Base::Role';
sub method2 {}
}
my $success = eval q{
package Diamant::Class;
use Role::Tiny::With;
with qw/Sub::Role1 Sub::Role2/;
1;
};
is $success, 1, 'composed diamantly dependent roles successfully' or diag "Error: $@";
}
{
{
package My::Does::Conflict;
use Role::Tiny;
sub method {
return __PACKAGE__ . " method";
}
}
{
package My::Class::Base;
sub turbo_charger {
return __PACKAGE__ . " turbo charger";
}
sub method {
return __PACKAGE__ . " method";
}
}
my $success = eval q{
package My::Class::Child;
use base 'My::Class::Base';
use Role::Tiny::With;
with qw/My::Does::Basic1 My::Does::Conflict/;
1;
};
is $success, 1, 'role conflict resolved by superclass method' or diag "Error: $@";
can_ok 'My::Class::Child', 'method';
is My::Class::Child->method, 'My::Class::Base method', 'inherited method prevails';
$success = eval q{
package My::Class::Child2;
use base 'My::Class::Base';
use Role::Tiny::With;
with qw/My::Does::Basic1/;
1;
};
is $success, 1, 'role composed after conflict resolution' or diag "Error: $@";
can_ok 'My::Class::Child2', 'method';
is My::Class::Child2->method, 'My::Does::Basic1 method', 'role method applied';
}
done_testing;
|