-
Notifications
You must be signed in to change notification settings - Fork 574
/
roles.t
59 lines (41 loc) · 1.26 KB
/
roles.t
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
use Mojo::Base -strict;
use Test::More;
BEGIN {
plan skip_all => 'Role::Tiny 2.000001+ required for this test!'
unless Mojo::Base->can_roles;
}
package Mojo::RoleTest::LOUD;
use Role::Tiny;
sub yell {'HEY!'}
requires 'name';
sub hello {
my ($self) = @_;
return $self->yell . ' ' . uc($self->name) . '!!!';
}
package Mojo::RoleTest::quiet;
use Role::Tiny;
requires 'name';
sub whisper {
my ($self) = @_;
return 'psst, ' . lc($self->name);
}
package Mojo::RoleTest::Base;
use Mojo::Base -base;
has name => 'bob';
sub hello {
my ($self) = shift;
return 'hello ' . $self->name;
}
package main;
my $obj = Mojo::RoleTest::Base->new(name => 'Ted');
is($obj->name, 'Ted', 'attr works');
is($obj->hello, 'hello Ted', 'class method');
my $obj2 = Mojo::RoleTest::Base->with_roles('Mojo::RoleTest::LOUD')->new;
is($obj2->hello, 'HEY! BOB!!!', 'method from role overrides base method');
is($obj2->yell, 'HEY!', 'new method from role');
my $obj3 = Mojo::RoleTest::Base->with_roles('Mojo::RoleTest::quiet',
'Mojo::RoleTest::LOUD')->new(name => 'Joel');
is($obj3->name, 'Joel', 'attr from base class');
is($obj3->whisper, 'psst, joel', 'method from role1');
is($obj3->hello, 'HEY! JOEL!!!', 'method override from role2');
done_testing();