File: 011_advanced_role_composition.t

package info (click to toggle)
libmoose-perl 0.54-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,496 kB
  • ctags: 448
  • sloc: perl: 15,125; makefile: 10
file content (98 lines) | stat: -rw-r--r-- 2,048 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
97
98
use strict;
use warnings;
use Test::More tests => 5;
use Class::MOP;

# This is copied directly from recipe 11
{
    package Restartable;
    use Moose::Role;

    has 'is_paused' => (
        is      => 'rw',
        isa     => 'Bool',
        default => 0,
    );

    requires 'save_state', 'load_state';

    sub stop { }

    sub start { }

    package Restartable::ButUnreliable;
    use Moose::Role;

    with 'Restartable' => {
        alias => {
            stop  => '_stop',
            start => '_start'
        }
    };

    sub stop {
        my $self = shift;

        $self->explode() if rand(1) > .5;

        $self->_stop();
    }

    sub start {
        my $self = shift;

        $self->explode() if rand(1) > .5;

        $self->_start();
    }

    package Restartable::ButBroken;
    use Moose::Role;

    with 'Restartable' => { excludes => [ 'stop', 'start' ] };

    sub stop {
        my $self = shift;

        $self->explode();
    }

    sub start {
        my $self = shift;

        $self->explode();
    }
}

# This is the actual tests
{
    my $unreliable = Moose::Meta::Class->create_anon_class(
        superclasses => [],
        roles        => [qw/Restartable::ButUnreliable/],
        methods      => {
            explode      => sub { },    # nop.
            'save_state' => sub { },
            'load_state' => sub { },
        },
    )->new_object();
    ok $unreliable, 'made anon class with Restartable::ButUnreliable role';
    can_ok $unreliable, qw/start stop/;
}

{
    my $cnt = 0;
    my $broken = Moose::Meta::Class->create_anon_class(
        superclasses => [],
        roles        => [qw/Restartable::ButBroken/],
        methods      => {
            explode => sub { $cnt++ },
            'save_state' => sub { },
            'load_state' => sub { },
        },
    )->new_object();
    ok $broken, 'made anon class with Restartable::ButBroken role';
    $broken->start();
    is $cnt, 1, '... start called explode';
    $broken->stop();
    is $cnt, 2, '... stop also called explode';
}