test composing modifiers into role
[gitmo/Role-Tiny.git] / t / modifiers.t
CommitLineData
e32a2123 1use strict;
2use warnings FATAL => 'all';
3use Test::More;
4use Test::Fatal;
5
6BEGIN {
3117a19e 7 plan skip_all => "Class::Method::Modifiers not installed or too old"
8 unless eval "use Class::Method::Modifiers 1.05; 1";
e32a2123 9}
10
11BEGIN {
12 package MyRole;
13
14 use Role::Tiny;
15
16 around foo => sub { my $orig = shift; join ' ', 'role foo', $orig->(@_) };
17}
18
19BEGIN {
06b06c8e 20 package ExtraRole;
21
22 use Role::Tiny;
23}
24
25BEGIN {
e32a2123 26 package MyClass;
27
28 sub foo { 'class foo' }
29}
30
31BEGIN {
06b06c8e 32 package ExtraClass;
33
34 use Role::Tiny::With;
35
36 with qw(MyRole ExtraRole);
37
38 sub foo { 'class foo' }
39}
40
41BEGIN {
e32a2123 42 package BrokenRole;
43 use Role::Tiny;
44
45 around 'broken modifier' => sub { my $orig = shift; $orig->(@_) };
46}
47
291d3b5e 48BEGIN {
49 package MyRole2;
50 use Role::Tiny;
51 with 'MyRole';
52}
53
54BEGIN {
55 package ExtraClass2;
56 use Role::Tiny::With;
57 with 'MyRole2';
58 sub foo { 'class foo' }
59}
60
e32a2123 61sub try_apply_to {
62 my $to = shift;
63 exception { Role::Tiny->apply_role_to_package($to, 'MyRole') }
64}
65
66is(try_apply_to('MyClass'), undef, 'role applies cleanly');
67is(MyClass->foo, 'role foo class foo', 'method modifier');
06b06c8e 68is(ExtraClass->foo, 'role foo class foo', 'method modifier with composition');
e32a2123 69
291d3b5e 70is(ExtraClass2->foo, 'role foo class foo',
71 'method modifier with role composed into role');
72
e32a2123 73ok(exception {
74 my $new_class = Role::Tiny->create_class_with_roles('MyClass', 'BrokenRole');
75}, 'exception caught creating class with broken modifier in a role');
76
77done_testing;
78