Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 000_recipes / moose_cookbook_roles_recipe3.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     # Not in the recipe, but needed for writing tests.
13     package Employee;
14
15     use Mouse;
16
17     has 'name' => (
18         is       => 'ro',
19         isa      => 'Str',
20         required => 1,
21     );
22
23     has 'work' => (
24         is        => 'rw',
25         isa       => 'Str',
26         predicate => 'has_work',
27     );
28 }
29
30
31
32 # =begin testing SETUP
33 {
34
35   package MyApp::Role::Job::Manager;
36
37   use List::Util qw( first );
38
39   use Mouse::Role;
40
41   has 'employees' => (
42       is  => 'rw',
43       isa => 'ArrayRef[Employee]',
44   );
45
46   sub assign_work {
47       my $self = shift;
48       my $work = shift;
49
50       my $employee = first { !$_->has_work } @{ $self->employees };
51
52       die 'All my employees have work to do!' unless $employee;
53
54       $employee->work($work);
55   }
56
57   package main;
58
59   my $lisa = Employee->new( name => 'Lisa' );
60   MyApp::Role::Job::Manager->meta->apply($lisa);
61
62   my $homer = Employee->new( name => 'Homer' );
63   my $bart  = Employee->new( name => 'Bart' );
64   my $marge = Employee->new( name => 'Marge' );
65
66   $lisa->employees( [ $homer, $bart, $marge ] );
67   $lisa->assign_work('mow the lawn');
68 }
69
70
71
72 # =begin testing
73 {
74 {
75     my $lisa = Employee->new( name => 'Lisa' );
76     MyApp::Role::Job::Manager->meta->apply($lisa);
77
78     my $homer = Employee->new( name => 'Homer' );
79     my $bart  = Employee->new( name => 'Bart' );
80     my $marge = Employee->new( name => 'Marge' );
81
82     $lisa->employees( [ $homer, $bart, $marge ] );
83     $lisa->assign_work('mow the lawn');
84
85     ok( $lisa->does('MyApp::Role::Job::Manager'),
86         'lisa now does the manager role' );
87
88     is( $homer->work, 'mow the lawn',
89         'homer was assigned a task by lisa' );
90 }
91 }
92
93
94
95
96 1;