tests for _load_module
[gitmo/Role-Tiny.git] / t / role-basic-bugs.t
1 use lib 'lib', 't/role-basic/lib', 't/lib';
2 use MyTests;
3
4 # multiple roles with the same role
5 {
6     package RoleC;
7     use Role::Tiny;
8     sub baz { 'baz' }
9
10     package RoleB;
11     use Role::Tiny;
12     with 'RoleC';
13     sub bar { 'bar' }
14
15     package RoleA;
16     use Role::Tiny;
17     with 'RoleC';
18     sub foo { 'foo' }
19
20     package Foo;
21     use strict;
22     use warnings;
23     use Role::Tiny 'with';
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;
42     use Role::Tiny 'with';
43     with 'A::NonExistent::Role';
44     END
45 }
46
47 {
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
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 }
78
79 can_ok(Role::Tiny->create_class_with_roles(qw(Bar Baz))->new, qw(bar baz));
80 done_testing;