bah
[gitmo/Moose.git] / t / 000_recipes / 011_advanced_role_composition.t
1 use strict;
2 use warnings;
3 use Test::More tests => 5;
4 use Class::MOP;
5
6 # This is copied directly from recipe 11
7 {
8     package Restartable;
9     use Moose::Role;
10
11     has 'is_paused' => (
12         is      => 'rw',
13         isa     => 'Bool',
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
67 # This is the actual tests
68 {
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();
78     ok $unreliable, 'made anon class with Restartable::ButUnreliable role';
79     can_ok $unreliable, qw/start stop/;
80 }
81
82 {
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();
93     ok $broken, 'made anon class with Restartable::ButBroken role';
94     $broken->start();
95     is $cnt, 1, '... start called explode';
96     $broken->stop();
97     is $cnt, 2, '... stop also called explode';
98 }