File: compose-modifiers.t

package info (click to toggle)
librole-tiny-perl 2.000005-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 240 kB
  • ctags: 32
  • sloc: perl: 380; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use strict;
use warnings;
use Test::More;

use Class::Method::Modifiers 1.05 ();

{
  package One; use Role::Tiny;
  around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
  package Two; use Role::Tiny;
  around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
  package Three; use Role::Tiny;
  around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
  package Four; use Role::Tiny;
  around foo => sub { my $orig = shift; (__PACKAGE__, $orig->(@_)) };
  package BaseClass; sub foo { __PACKAGE__ }
}

foreach my $combo (
  [ qw(One Two Three Four) ],
  [ qw(Two Four Three) ],
  [ qw(One Two) ]
) {
  my $combined = Role::Tiny->create_class_with_roles('BaseClass', @$combo);
  is_deeply(
    [ $combined->foo ], [ reverse(@$combo), 'BaseClass' ],
    "${combined} ok"
  );
  my $object = bless({}, 'BaseClass');
  Role::Tiny->apply_roles_to_object($object, @$combo);
  is(ref($object), $combined, 'Object reblessed into correct class');
}

{
  package Five; use Role::Tiny;
  requires 'bar';
  around bar => sub { my $orig = shift; $orig->(@_) };
}
{
  is eval {
    package WithFive;
    use Role::Tiny::With;
    use base 'BaseClass';
    with 'Five';
  }, undef,
    "composing an around modifier fails when method doesn't exist";
  like $@, qr/Can't apply Five to WithFive - missing bar/,
    ' ... with correct error message';
}
{
  is eval {
    Role::Tiny->create_class_with_roles('BaseClass', 'Five');
  }, undef,
    "composing an around modifier fails when method doesn't exist";
  like $@, qr/Can't apply Five to .* - missing bar/,
    ' ... with correct error message';
}

done_testing;