This change gets Recipe 11 working. Here are the details ...
[gitmo/Moose.git] / t / 000_recipes / 011_advanced_role_composition.t
CommitLineData
29b21010 1use strict;
2use warnings;
db9476b1 3use Test::More tests => 5;
29b21010 4use Class::MOP;
5
db9476b1 6# This is copied directly from recipe 11
29b21010 7{
8 package Restartable;
9 use Moose::Role;
10
11 has 'is_paused' => (
12 is => 'rw',
db9476b1 13 isa => 'Bool',
29b21010 14 default => 0,
15 );
16
17 requires 'save_state', 'load_state';
18
19 sub stop { }
20
21 sub start { }
22
23 package Restartable::ButUnreliable;
24 use Moose::Role;
25
26 with 'Restartable' => {
27 alias => {
28 stop => '_stop',
29 start => '_start'
30 }
31 };
32
33 sub stop {
34 my $self = shift;
35
36 $self->explode() if rand(1) > .5;
37
38 $self->_stop();
39 }
40
41 sub start {
42 my $self = shift;
43
44 $self->explode() if rand(1) > .5;
45
46 $self->_start();
47 }
48
49 package Restartable::ButBroken;
50 use Moose::Role;
51
52 with 'Restartable' => { excludes => [ 'stop', 'start' ] };
53
54 sub stop {
55 my $self = shift;
56
57 $self->explode();
58 }
59
60 sub start {
61 my $self = shift;
62
63 $self->explode();
64 }
65}
66
db9476b1 67# This is the actual tests
68{
29b21010 69 my $unreliable = Moose::Meta::Class->create_anon_class(
70 superclasses => [],
71 roles => [qw/Restartable::ButUnreliable/],
72 methods => {
73 explode => sub { }, # nop.
74 'save_state' => sub { },
75 'load_state' => sub { },
76 },
77 )->new_object();
db9476b1 78 ok $unreliable, 'made anon class with Restartable::ButUnreliable role';
79 can_ok $unreliable, qw/start stop/;
80}
29b21010 81
db9476b1 82{
29b21010 83 my $cnt = 0;
84 my $broken = Moose::Meta::Class->create_anon_class(
85 superclasses => [],
86 roles => [qw/Restartable::ButBroken/],
87 methods => {
88 explode => sub { $cnt++ },
89 'save_state' => sub { },
90 'load_state' => sub { },
91 },
92 )->new_object();
db9476b1 93 ok $broken, 'made anon class with Restartable::ButBroken role';
29b21010 94 $broken->start();
db9476b1 95 is $cnt, 1, '... start called explode';
29b21010 96 $broken->stop();
db9476b1 97 is $cnt, 2, '... stop also called explode';
98}