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