File: moose-method-modifiers.t

package info (click to toggle)
libmoo-perl 2.004004-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 844 kB
  • sloc: perl: 2,449; makefile: 6
file content (61 lines) | stat: -rw-r--r-- 1,196 bytes parent folder | download | duplicates (3)
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
60
61
use Moo::_strictures;
use Test::More;

{
   package ModifyFoo;
   use Moo::Role;

   our $before_ran = 0;
   our $around_ran = 0;
   our $after_ran = 0;

   before foo => sub { $before_ran = 1 };
   after foo => sub { $after_ran = 1 };
   around foo => sub {
      my ($orig, $self, @rest) = @_;
      $self->$orig(@rest);
      $around_ran = 1;
   };

   package Bar;
   use Moose;
   with 'ModifyFoo';

   sub foo { }
}

my $bar = Bar->new;

ok(!$ModifyFoo::before_ran, 'before has not run yet');
ok(!$ModifyFoo::after_ran, 'after has not run yet');
ok(!$ModifyFoo::around_ran, 'around has not run yet');
$bar->foo;
ok($ModifyFoo::before_ran, 'before ran');
ok($ModifyFoo::after_ran, 'after ran');
ok($ModifyFoo::around_ran, 'around ran');

{
  package ModifyMultiple;
  use Moo::Role;
  our $before = 0;

  before 'foo', 'bar' => sub {
    $before++;
  };

  package Baz;
  use Moose;
  with 'ModifyMultiple';

  sub foo {}
  sub bar {}
}

my $baz = Baz->new;
my $pre = $ModifyMultiple::before;
$baz->foo;
is $ModifyMultiple::before, $pre+1, "before applies to first of multiple subs";
$baz->bar;
is $ModifyMultiple::before, $pre+2, "before applies to second of multiple subs";

done_testing;