File: 011-reference-parameters.t

package info (click to toggle)
libmoosex-role-parameterized-perl 1.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 464 kB
  • sloc: perl: 439; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 1,419 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use strict;
use warnings;
use Test::More 0.88;

do {
    package MyRole::Delegator;
    use MooseX::Role::Parameterized;

    parameter handles => (
        is       => 'rw',
        required => 1,
    );

    role {
        my $p = shift;

        has attr => (
            is      => 'rw',
            isa     => 'MyClass::WithMethods',
            handles => $p->handles,
        );
    };
};

do {
    package MyClass::WithMethods;
    use Moose;

    sub foo { "foo" }
    sub bar { "bar" }
    sub baz { "baz" }
};

do {
    package MyArrayConsumer;
    use Moose;
    with 'MyRole::Delegator' => {
        handles => ['foo', 'bar'],
    };
};

can_ok(MyArrayConsumer => 'foo', 'bar');
cant_ok(MyArrayConsumer => 'baz');

do {
    package MyRegexConsumer;
    use Moose;
    with 'MyRole::Delegator' => {
        handles => qr/^ba/,
    };
};

can_ok(MyRegexConsumer => 'bar', 'baz');
cant_ok(MyRegexConsumer => 'foo');

do {
    package MyHashConsumer;
    use Moose;
    with 'MyRole::Delegator' => {
        handles => {
            my_foo => 'foo',
            his_baz => 'baz',
        },
    };
};

can_ok(MyHashConsumer => 'my_foo', 'his_baz');
cant_ok(MyHashConsumer => qw/foo bar baz/);

sub cant_ok {
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $instance = shift;
    for my $method (@_) {
        ok(!$instance->can($method), "$instance cannot $method");
    }
}

done_testing;