summaryrefslogtreecommitdiff
path: root/t/basics/wrapped_method_cxt_propagation.t
blob: ce1e2430b3ca074484e5b786837e0cdb301eb9fb (plain)
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
use strict;
use warnings;

use Test::More;


{
    package TouchyBase;
    use Moose;

    has x => ( is => 'rw', default => 0 );

    sub inc { $_[0]->x( 1 + $_[0]->x ) }

    sub scalar_or_array {
        wantarray ? (qw/a b c/) : "x";
    }

    sub void {
        die "this must be void context" if defined wantarray;
    }

    package AfterSub;
    use Moose;

    extends "TouchyBase";

    after qw/scalar_or_array void/ => sub {
        my $self = shift;
        $self->inc;
    }
}

my $base = TouchyBase->new;
my $after = AfterSub->new;

foreach my $obj ( $base, $after ) {
    my $class = ref $obj;
    my @array = $obj->scalar_or_array;
    my $scalar = $obj->scalar_or_array;

    is_deeply(\@array, [qw/a b c/], "array context ($class)");
    is($scalar, "x", "scalar context ($class)");

    {
        local $@;
        eval { $obj->void };
        ok( !$@, "void context ($class)" );
    }

    if ( $obj->isa("AfterSub") ) {
        is( $obj->x, 3, "methods were wrapped" );
    }
}

done_testing;