calculate mro module once
[gitmo/Role-Tiny.git] / t / role-basic-bugs.t
CommitLineData
f52b9821 1use lib 'lib', 't/role-basic/lib', 't/lib';
4a582a37 2use MyTests;
3
4# multiple roles with the same role
5{
6 package RoleC;
2c580674 7 use Role::Tiny;
4a582a37 8 sub baz { 'baz' }
9
10 package RoleB;
2c580674 11 use Role::Tiny;
4a582a37 12 with 'RoleC';
13 sub bar { 'bar' }
14
15 package RoleA;
2c580674 16 use Role::Tiny;
4a582a37 17 with 'RoleC';
18 sub foo { 'foo' }
19
20 package Foo;
21 use strict;
22 use warnings;
2c580674 23 use Role::Tiny 'with';
4a582a37 24 ::is( ::exception {
25 with 'RoleA', 'RoleB';
26 }, undef, 'Composing multiple roles which use the same role should not have conflicts' );
27 sub new { bless {} => shift }
28
29 my $object = Foo->new;
30 foreach my $method (qw/foo bar baz/) {
31 ::can_ok $object, $method;
32 ::is $object->$method, $method,
33 '... and all methods should be composed in correctly';
34 }
35}
36
37{
38 no warnings 'redefine';
39 local *UNIVERSAL::can = sub { 1 };
40 eval <<' END';
41 package Can::Can;
2c580674 42 use Role::Tiny 'with';
4a582a37 43 with 'A::NonExistent::Role';
44 END
0926898f 45}
46
47{
4a582a37 48 my $error = $@ || '';
49 like $error, qr{^Can't locate A/NonExistent/Role.pm},
50 'If ->can always returns true, we should still not think we loaded the role'
51 or diag "Error found: $error";
52}
53
558f9dc9 54{
55 package Role1;
56 use Role::Tiny;
57
58 package Role2;
59 use Role::Tiny;
60
61 package Frew;
62 use strict;
63 use warnings;
64 sub new { bless {} => shift }
65
66 my $object = Frew->new;
67
68 ::ok(!Role::Tiny::does_role($object, 'Role1'), 'no Role1 yet');
69 ::ok(!Role::Tiny::does_role($object, 'Role2'), 'no Role2 yet');
70
71 Role::Tiny->apply_roles_to_object($object, 'Role1');
72 ::ok(Role::Tiny::does_role($object, "Role1"), 'Role1 consumed');
73 ::ok(!Role::Tiny::does_role($object, 'Role2'), 'no Role2 yet');
74 Role::Tiny->apply_roles_to_object($object, 'Role2');
75 ::ok(Role::Tiny::does_role($object, "Role1"), 'Role1 consumed');
76 ::ok(Role::Tiny::does_role($object, 'Role2'), 'Role2 consumed');
77}
f52b9821 78
79can_ok(Role::Tiny->create_class_with_roles(qw(Bar Baz))->new, qw(bar baz));
4a582a37 80done_testing;