File: modify_lazy_handlers.t

package info (click to toggle)
libmoo-perl 2.002005-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 856 kB
  • ctags: 192
  • sloc: perl: 2,561; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 912 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
use Moo::_strictures;
use Test::More;

BEGIN {
  package ClassicObject;

  sub new {
      my ($class, %args) = @_;
      bless \%args, 'ClassicObject';
  }

  sub connect { 'a' }
}

BEGIN {
  package MooObjectWithDelegate;
  use Scalar::Util ();
  use Moo;

  has 'delegated' => (
    is => 'ro',
    isa => sub {
      do { $_[0] && Scalar::Util::blessed($_[0]) }
        or die "Not an Object!";
    },
    lazy => 1,
    builder => '_build_delegated',
    handles => [qw/connect/],
  );

  sub _build_delegated {
    my $self = shift;
    return ClassicObject->new;
  }

  around 'connect', sub {
    my ($orig, $self, @args) = @_;
    return $self->$orig(@args) . 'b';
  };

  around 'connect', sub {
    my ($orig, $self, @args) = @_;
    return $self->$orig(@args) . 'c';
  };
}

ok my $moo_object = MooObjectWithDelegate->new,
  'got object';

is $moo_object->connect, 'abc',
  'got abc';

done_testing;