-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path012-proxy.t
60 lines (52 loc) · 1.43 KB
/
012-proxy.t
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
#!perl -T
#------------------------------------------------------
package Subject;
use Moose::Role;
requires qw( fast_action );
has attr => ( is => 'rw', isa => 'Any' );
#------------------------------------------------------
package RealSubject;
use Moose;
use Data::Dumper;
with 'Subject';
sub BUILD { shift->slow_action }
sub fast_action { printf "%s fast action\n", shift->attr }
sub slow_action { printf "%s slow action\n", shift->attr }
#------------------------------------------------------
package Proxy;
use Moose;
use MooseX::FollowPBP;
with 'Subject';
has real_subject => ( is => 'rw', isa => 'RealSubject' );
sub fast_action {
my $self = shift;
unless ($self->get_real_subject) {
$self->set_real_subject(
RealSubject->new( attr => $self->attr )
);
}
$self->get_real_subject->fast_action;
}
#======================================================
package main;
use strict;
use warnings FATAL => 'all';
use Capture::Tiny 'capture';
use Test::More;
use Test::Exception;
plan tests => 4;
use_ok 'OODP::Proxy';
my $foo = new_ok Proxy => [ attr => 'foo' ];
my $bar = new_ok Proxy => [ attr => 'bar' ];
my ($stdout) = capture {
$foo->fast_action;
$foo->fast_action;
$foo->fast_action;
$bar->fast_action;
$bar->fast_action;
$bar->fast_action;
};
is $stdout,
"foo slow action\n" . "foo fast action\n" x 3
. "bar slow action\n" . "bar fast action\n" x 3
, 'correct STDOUT';