X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F000_recipes%2Fmoose_cookbook_roles_recipe3.t;fp=t%2F000_recipes%2Fmoose_cookbook_roles_recipe3.t;h=750a5435a6c9013629b35827bf403a9436293b38;hb=f9aca0f35658e5e0657e8ce78b783c0185d59969;hp=0000000000000000000000000000000000000000;hpb=7fc043cc981de12e62ece2868f6315292d85eeab;p=gitmo%2FMouse.git diff --git a/t/000_recipes/moose_cookbook_roles_recipe3.t b/t/000_recipes/moose_cookbook_roles_recipe3.t new file mode 100644 index 0000000..750a543 --- /dev/null +++ b/t/000_recipes/moose_cookbook_roles_recipe3.t @@ -0,0 +1,96 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More 'no_plan'; +use Test::Exception; +$| = 1; + + + +# =begin testing SETUP +{ + # Not in the recipe, but needed for writing tests. + package Employee; + + use Mouse; + + has 'name' => ( + is => 'ro', + isa => 'Str', + required => 1, + ); + + has 'work' => ( + is => 'rw', + isa => 'Str', + predicate => 'has_work', + ); +} + + + +# =begin testing SETUP +{ + + package MyApp::Role::Job::Manager; + + use List::Util qw( first ); + + use Mouse::Role; + + has 'employees' => ( + is => 'rw', + isa => 'ArrayRef[Employee]', + ); + + sub assign_work { + my $self = shift; + my $work = shift; + + my $employee = first { !$_->has_work } @{ $self->employees }; + + die 'All my employees have work to do!' unless $employee; + + $employee->work($work); + } + + package main; + + my $lisa = Employee->new( name => 'Lisa' ); + MyApp::Role::Job::Manager->meta->apply($lisa); + + my $homer = Employee->new( name => 'Homer' ); + my $bart = Employee->new( name => 'Bart' ); + my $marge = Employee->new( name => 'Marge' ); + + $lisa->employees( [ $homer, $bart, $marge ] ); + $lisa->assign_work('mow the lawn'); +} + + + +# =begin testing +{ +{ + my $lisa = Employee->new( name => 'Lisa' ); + MyApp::Role::Job::Manager->meta->apply($lisa); + + my $homer = Employee->new( name => 'Homer' ); + my $bart = Employee->new( name => 'Bart' ); + my $marge = Employee->new( name => 'Marge' ); + + $lisa->employees( [ $homer, $bart, $marge ] ); + $lisa->assign_work('mow the lawn'); + + ok( $lisa->does('MyApp::Role::Job::Manager'), + 'lisa now does the manager role' ); + + is( $homer->work, 'mow the lawn', + 'homer was assigned a task by lisa' ); +} +} + + + + +1;