File: moose_cookbook_roles_recipe3.t

package info (click to toggle)
libmoose-perl 1.09-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,004 kB
  • ctags: 1,472
  • sloc: perl: 25,387; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 1,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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 Moose;

    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 Moose::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;