fix checking requires when using create_class_with_roles
[gitmo/Role-Tiny.git] / t / compose-modifiers.t
CommitLineData
5897133a 1use strict;
2use warnings FATAL => 'all';
c49b0f72 3use Test::More;
4
5BEGIN {
3117a19e 6 plan skip_all => "Class::Method::Modifiers not installed or too old"
7 unless eval "use Class::Method::Modifiers 1.05; 1";
c49b0f72 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->(@_)) };
f3e87eb4 19 package BaseClass; sub foo { __PACKAGE__ }
c49b0f72 20}
21
22foreach my $combo (
23 [ qw(One Two Three Four) ],
24 [ qw(Two Four Three) ],
25 [ qw(One Two) ]
26) {
f3e87eb4 27 my $combined = Role::Tiny->create_class_with_roles('BaseClass', @$combo);
c49b0f72 28 is_deeply(
f3e87eb4 29 [ $combined->foo ], [ reverse(@$combo), 'BaseClass' ],
c49b0f72 30 "${combined} ok"
31 );
f3e87eb4 32 my $object = bless({}, 'BaseClass');
c49b0f72 33 Role::Tiny->apply_roles_to_object($object, @$combo);
34 is(ref($object), $combined, 'Object reblessed into correct class');
35}
36
c0978659 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;
f3e87eb4 46 use base 'BaseClass';
c0978659 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}
1ad34a6f 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}
c0978659 61
c49b0f72 62done_testing;