added test for Moose::Cookbook::Recipe11.
[gitmo/Moose.git] / t / 000_recipes / 011_advanced_role_composition.t
CommitLineData
29b21010 1use strict;
2use warnings;
3use Test::More tests => 3;
4use Class::MOP;
5
6# follow is original code
7{
8 package Restartable;
9 use Moose::Role;
10
11 has 'is_paused' => (
12 is => 'rw',
13 isa => 'Boo',
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# follow is test
68do {
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, 'Restartable::ButUnreliable based class';
79 can_ok $unreliable, qw/start stop/, '... can call start and stop';
80};
81
82do {
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, 'Restartable::ButBroken based class';
94 $broken->start();
95 is $cnt, 1, '... start is exploded';
96 $broken->stop();
97 is $cnt, 2, '... stop is also exploeded';
98};