Commit | Line | Data |
cdf3cae6 |
1 | use strict; |
2 | use warnings; |
3 | |
a28e50e4 |
4 | use Test::More; |
cdf3cae6 |
5 | |
6 | { |
7 | package Thingy; |
8 | use Moose; |
9 | |
10 | has callback => ( |
d9299060 |
11 | traits => ['Code'], |
d9299060 |
12 | isa => 'CodeRef', |
13 | required => 1, |
14 | handles => { 'invoke_callback' => 'execute' }, |
15 | ); |
16 | |
17 | has callback_method => ( |
18 | traits => ['Code'], |
d9299060 |
19 | isa => 'CodeRef', |
cdf3cae6 |
20 | required => 1, |
d9299060 |
21 | handles => { 'invoke_method_callback' => 'execute_method' }, |
cdf3cae6 |
22 | ); |
63723115 |
23 | |
24 | has multiplier => ( |
25 | traits => ['Code'], |
63723115 |
26 | isa => 'CodeRef', |
27 | required => 1, |
28 | handles => { 'multiply' => 'execute' }, |
29 | ); |
cdf3cae6 |
30 | } |
31 | |
32 | my $i = 0; |
63723115 |
33 | my $thingy = Thingy->new( |
d9299060 |
34 | callback => sub { ++$i }, |
35 | multiplier => sub { $_[0] * 2 }, |
36 | callback_method => sub { shift->multiply(@_) }, |
63723115 |
37 | ); |
cdf3cae6 |
38 | |
39 | is($i, 0); |
40 | $thingy->invoke_callback; |
41 | is($i, 1); |
63723115 |
42 | is($thingy->multiply(3), 6); |
d9299060 |
43 | is($thingy->invoke_method_callback(3), 6); |
a28e50e4 |
44 | |
12885414 |
45 | ok(!$thingy->can($_), "Code trait didn't create reader method for $_") |
46 | for qw(callback callback_method multiplier); |
47 | |
a28e50e4 |
48 | done_testing; |