calculate mro module once
[gitmo/Role-Tiny.git] / t / compose-modifiers.t
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 BEGIN {
6   plan skip_all => "Class::Method::Modifiers not installed or too old"
7     unless eval "use Class::Method::Modifiers 1.05; 1";
8 }
9
10 {
11   package One; use Role::Tiny;
12   around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
13   package Two; use Role::Tiny;
14   around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
15   package Three; use Role::Tiny;
16   around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
17   package Four; use Role::Tiny;
18   around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
19   package BaseClass; sub foo { __PACKAGE__ }
20 }
21
22 foreach my $combo (
23   [ qw(One Two Three Four) ],
24   [ qw(Two Four Three) ],
25   [ qw(One Two) ]
26 ) {
27   my $combined = Role::Tiny->create_class_with_roles('BaseClass', @$combo);
28   is_deeply(
29     [ $combined->foo ], [ reverse(@$combo), 'BaseClass' ],
30     "${combined} ok"
31   );
32   my $object = bless({}, 'BaseClass');
33   Role::Tiny->apply_roles_to_object($object, @$combo);
34   is(ref($object), $combined, 'Object reblessed into correct class');
35 }
36
37 {
38   package Five; use Role::Tiny;
39   requires 'bar';
40   around bar => sub { my $orig = shift; $orig->(@_) };
41 }
42 {
43   is eval {
44     package WithFive;
45     use Role::Tiny::With;
46     use base 'BaseClass';
47     with 'Five';
48   }, undef,
49     "composing an around modifier fails when method doesn't exist";
50   like $@, qr/Can't apply Five to WithFive - missing bar/,
51     ' ... with correct error message';
52 }
53 {
54   is eval {
55     Role::Tiny->create_class_with_roles('BaseClass', 'Five');
56   }, undef,
57     "composing an around modifier fails when method doesn't exist";
58   like $@, qr/Can't apply Five to .* - missing bar/,
59     ' ... with correct error message';
60 }
61
62 done_testing;