9fec6ddd2b351839a500eeb357833ba30b7a9802
[gitmo/Mouse.git] / t / 000-recipes / moose_cookbook_roles_recipe2.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More 'no_plan';
5 use Test::Exception;
6 $| = 1;
7
8
9
10 # =begin testing SETUP
11 {
12
13   package Restartable;
14   use Mouse::Role;
15
16   has 'is_paused' => (
17       is      => 'rw',
18       isa     => 'Bool',
19       default => 0,
20   );
21
22   requires 'save_state', 'load_state';
23
24   sub stop { 1 }
25
26   sub start { 1 }
27
28   package Restartable::ButUnreliable;
29   use Mouse::Role;
30
31   with 'Restartable' => {
32       -alias => {
33           stop  => '_stop',
34           start => '_start'
35       },
36       -excludes => [ 'stop', 'start' ],
37   };
38
39   sub stop {
40       my $self = shift;
41
42       $self->explode() if rand(1) > .5;
43
44       $self->_stop();
45   }
46
47   sub start {
48       my $self = shift;
49
50       $self->explode() if rand(1) > .5;
51
52       $self->_start();
53   }
54
55   package Restartable::ButBroken;
56   use Mouse::Role;
57
58   with 'Restartable' => { -excludes => [ 'stop', 'start' ] };
59
60   sub stop {
61       my $self = shift;
62
63       $self->explode();
64   }
65
66   sub start {
67       my $self = shift;
68
69       $self->explode();
70   }
71 }
72
73
74
75 # =begin testing
76 {
77 {
78     my $unreliable = Mouse::Meta::Class->create_anon_class(
79         superclasses => [],
80         roles        => [qw/Restartable::ButUnreliable/],
81         methods      => {
82             explode      => sub { },    # nop.
83             'save_state' => sub { },
84             'load_state' => sub { },
85         },
86     )->new_object();
87     ok( $unreliable, 'made anon class with Restartable::ButUnreliable role' );
88     can_ok( $unreliable, qw/start stop/ );
89 }
90
91 {
92     my $cnt    = 0;
93     my $broken = Mouse::Meta::Class->create_anon_class(
94         superclasses => [],
95         roles        => [qw/Restartable::ButBroken/],
96         methods      => {
97             explode      => sub { $cnt++ },
98             'save_state' => sub { },
99             'load_state' => sub { },
100         },
101     )->new_object();
102
103     ok( $broken, 'made anon class with Restartable::ButBroken role' );
104
105     $broken->start();
106
107     is( $cnt, 1, '... start called explode' );
108
109     $broken->stop();
110
111     is( $cnt, 2, '... stop also called explode' );
112 }
113 }
114
115
116
117
118 1;