File: chained.t

package info (click to toggle)
libmoosex-attribute-chained-perl 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 212 kB
  • sloc: perl: 425; makefile: 2
file content (75 lines) | stat: -rw-r--r-- 1,617 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use Test::More;

use_ok('MooseX::Attribute::Chained');
use_ok('MooseX::ChainedAccessors::Accessor');
use_ok('MooseX::Traits::Attribute::Chained');

{
    package SimpleChained;
    use Moose;
        
    has 'regular_attr' => (
        is => 'rw',
        isa => 'Str',
        default => sub { 'hello'; },
    );
    
    has 'chained_attr' => (
        traits => ['Chained'],
        is => 'rw',
        isa => 'Bool',    
        lazy => 1,
        default => sub { 0; },
    );
    
    has 'writer_attr' => (
        traits => ['Chained'],
        is => 'rw',
        isa => 'Str',
        reader => 'get_writer_attr',
        writer => 'set_writer_attr',
    );
}


my $simple = SimpleChained->new();
is($simple->chained_attr(1)->regular_attr, 'hello', 'chained accessor attribute');
is($simple->chained_attr(0)->set_writer_attr('world')->get_writer_attr, 'world', 'chained writer attribute');


{
    package Debug;
    use Moose::Role;
    
    has 'debug' => (
        traits => ['Chained'],
        is => 'rw',
        isa => 'Bool',
        default => sub { 0; },
    );
}


{
    package ChainedFromRole;
    use Moose;
    
    with 'Debug';
    
    sub message 
    {
        my $self = shift;
        return 'hello' if $self->debug;
        return 'world';
    }
}

my $rolechained = ChainedFromRole->new();
is($rolechained->debug, 0, "debugging is disabled");
is($rolechained->message, 'world', 'normal access..');
is($rolechained->debug(1)->message, 'hello', 'chained write affects method call..');
is($rolechained->debug, 1, 'chained attribute reads ok.');

done_testing;