File: 008-method-modifers.t

package info (click to toggle)
libmoosex-role-parameterized-perl 1.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 464 kB
  • sloc: perl: 439; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 972 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Test::More 0.88;

my @calls;

do {
    package MyRole::LogMethod;
    use MooseX::Role::Parameterized;

    parameter method => (
        isa      => 'Str',
        required => 1,
    );

    role {
        my $p = shift;

        before $p->method => sub {
            push @calls, "calling " . $p->method
        };

        after $p->method => sub {
            push @calls, "called " . $p->method
        };

        around $p->method => sub {
            my $orig = shift;
            my $start = 0; # time
            $orig->(@_);
            my $end = 0; # time

            push @calls, "took " . ($end - $start) . " seconds";
        };
    };
};

do {
    package MyClass;
    use Moose;
    with 'MyRole::LogMethod' => {
        method => 'new',
    };
};

is_deeply([splice @calls], [], "no calls yet");
MyClass->new;
is_deeply([splice @calls], ["calling new", "took 0 seconds", "called new"], "instrumented new");

done_testing;