fix error propagation when creating broken method modifiers
[gitmo/Role-Tiny.git] / t / role-tiny.t
CommitLineData
5ef4ffe7 1use strict;
2use warnings FATAL => 'all';
5f7ac979 3use Test::More;
ab3370e7 4use Test::Fatal;
5
6BEGIN {
7 package MyRole;
8
9 use Role::Tiny;
10
11 requires qw(req1 req2);
12
13 around foo => sub { my $orig = shift; join ' ', 'role foo', $orig->(@_) };
14
15 sub bar { 'role bar' }
16
17 sub baz { 'role baz' }
18}
19
20BEGIN {
21 package MyClass;
22
2f57f81a 23 use constant SIMPLE => 'simple';
24 use constant REF_CONST => [ 'ref_const' ];
7b8177f8 25 use constant VSTRING_CONST => v1;
2f57f81a 26
ab3370e7 27 sub req1 { }
28 sub req2 { }
29 sub foo { 'class foo' }
30 sub baz { 'class baz' }
31
32}
33
34BEGIN {
0c145208 35 package ExtraClass;
36 sub req1 { }
37 sub req2 { }
38 sub req3 { }
39 sub foo { }
40 sub baz { 'class baz' }
41}
42
43BEGIN {
44 package IntermediaryRole;
45 use Role::Tiny;
46 requires 'req3';
47}
48
49BEGIN {
ab3370e7 50 package NoMethods;
51
52 package OneMethod;
53
54 sub req1 { }
55}
56
a8cc1122 57BEGIN {
58 package ExtraRole;
59 use Role::Tiny;
60
61 sub extra1 { 'role extra' }
62}
63
385f5087 64BEGIN {
65 package BrokenRole;
66 use Role::Tiny;
67
68 around 'broken modifier' => sub { my $orig = shift; $orig->(@_) };
69}
70
ab3370e7 71sub try_apply_to {
72 my $to = shift;
369a4c50 73 exception { Role::Tiny->apply_role_to_package($to, 'MyRole') }
ab3370e7 74}
75
76is(try_apply_to('MyClass'), undef, 'role applies cleanly');
77is(MyClass->foo, 'role foo class foo', 'method modifier');
78is(MyClass->bar, 'role bar', 'method from role');
79is(MyClass->baz, 'class baz', 'method from class');
5a247406 80ok(MyClass->does('MyRole'), 'class does role');
a8cc1122 81ok(!MyClass->does('IntermediaryRole'), 'class does not do non-applied role');
5a247406 82ok(!MyClass->does('Random'), 'class does not do non-role');
ab3370e7 83
84like(try_apply_to('NoMethods'), qr/req1, req2/, 'error for both methods');
85like(try_apply_to('OneMethod'), qr/req2/, 'error for one method');
5f7ac979 86
0c145208 87is exception {
c4fd8838 88 Role::Tiny->apply_role_to_package('IntermediaryRole', 'MyRole');
89 Role::Tiny->apply_role_to_package('ExtraClass', 'IntermediaryRole');
0c145208 90}, undef, 'No errors applying roles';
91
92ok(ExtraClass->does('MyRole'), 'ExtraClass does MyRole');
93ok(ExtraClass->does('IntermediaryRole'), 'ExtraClass does IntermediaryRole');
94is(ExtraClass->bar, 'role bar', 'method from role');
95is(ExtraClass->baz, 'class baz', 'method from class');
96
a8cc1122 97my $new_class;
98is exception {
99 $new_class = Role::Tiny->create_class_with_roles('MyClass', 'ExtraRole');
100}, undef, 'No errors creating class with roles';
101
102isa_ok($new_class, 'MyClass');
103is($new_class->extra1, 'role extra', 'method from role');
104
385f5087 105ok(exception {
106 $new_class = Role::Tiny->create_class_with_roles('MyClass', 'BrokenRole');
107}, 'exception caught creating class with broken modifier in a role');
108
5f7ac979 109done_testing;
0c145208 110