File: moo_lazy_subclass.t

package info (click to toggle)
libcpan-changes-perl 0.500005-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: perl: 988; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,410 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;

use Test::More;

# ABSTRACT: Make sure subclassing the name is easy and lazy

BEGIN {
    eval "require Moo; 1"
      or plan skip_all => "Moo required for this test";
}

use CPAN::Changes::Group;

{

    package CustomGroup;

    use Moo;
    extends 'CPAN::Changes::Group';

    has 'name'    => ( is => ro =>, lazy => 1, builder => '_build_name' );
    has 'flavour' => ( is => ro =>, lazy => 1, builder => '_build_flavour' );

    sub _build_name {
        my ($self) = @_;
        return 'Custom::Name / ' . $self->flavour;
    }

    sub _build_flavour {
        return 'Vanilla';
    }
}

subtest 'nameonly' => sub {
    my $object = CustomGroup->new( name => 'Bob' );

    is( $object->name,    'Bob',     'Constructor attribute passthrough' );
    is( $object->flavour, 'Vanilla', 'Default flavour still exists' );
};

subtest 'flavouronly' => sub {
    my $object = CustomGroup->new( flavour => 'Earwax' );

    is(
        $object->name,
        'Custom::Name / Earwax',
        'Constructor attribute affects name lazily'
    );
    is( $object->flavour, 'Earwax', 'Passed flavour propagates' );
};

subtest 'noargs' => sub {
    my $object = CustomGroup->new();

    is(
        $object->name,
        'Custom::Name / Vanilla',
        'Default attribute affects name lazily'
    );
    is( $object->flavour, 'Vanilla', 'Default flavour propagates' );
};

done_testing;