exception tests
[gitmo/Role-Tiny.git] / t / role-basic / exceptions.t
1 #!/usr/bin/env perl
2
3 use lib 'lib', 't/role-basic/lib';
4 use MyTests;
5 require Role::Tiny::Restricted;
6
7 {
8     package My::Does::Basic;
9
10     use Role::Tiny::Restricted;
11
12     requires 'turbo_charger';
13
14     sub conflict {
15         return "My::Does::Basic::conflict";
16     }
17 }
18
19 eval <<'END_PACKAGE';
20 package My::Bad::MultipleWith;
21 use Role::Tiny::Restricted::With;
22 with 'My::Does::Basic';
23 with 'My::Does::Basic';  # can't use with() more than once
24 sub turbo_charger {}
25 END_PACKAGE
26 like $@,
27   qr/with\(\) may not be called more than once for My::Bad::MultipleWith/,
28   'Trying to use with() more than once in a package should fail';
29
30 eval <<'END_PACKAGE';
31 package My::Bad::Requirement;
32 use Role::Tiny::Restricted::With;
33 with 'My::Does::Basic'; # requires turbo_charger
34 END_PACKAGE
35 like $@,
36 qr/missing turbo_charger/,
37   'Trying to use a role without providing required methods should fail';
38
39 {
40     {
41         package My::Conflict;
42         use Role::Tiny::Restricted;
43         sub conflict {};
44     }
45     eval <<'    END_PACKAGE';
46     package My::Bad::MethodConflicts;
47     use Role::Tiny::Restricted::With;
48     with qw(My::Does::Basic My::Conflict);
49     sub turbo_charger {}
50     END_PACKAGE
51     like $@,
52     qr/.*/,
53       'Trying to use multiple roles with the same method should fail';
54 }
55
56
57 {
58     {
59         package Role1;
60         use Role::Tiny::Restricted;
61         requires 'missing_method';
62         sub method1 { 'method1' }
63     }
64     {
65         package Role2;
66         use Role::Tiny::Restricted;
67         with 'Role1';
68         sub method2 { 'method2' }
69     }
70     eval <<"    END";
71     package My::Class::Missing1;
72     use Role::Tiny::Restricted::With;
73     with 'Role2';
74     END
75     like $@,
76     qr/missing missing_method/,
77       'Roles composed from roles should propogate requirements upwards';
78 }
79 {
80     {
81         package Role3;
82         use Role::Tiny::Restricted;
83         requires qw(this that);
84     }
85     eval <<"    END";
86     package My::Class::Missing2;
87     use Role::Tiny::Restricted::With;
88     with 'Role3';
89     END
90     like $@,
91     qr/missing this, that/,
92       'Roles should be able to require multiple methods';
93 }
94
95 done_testing;