File: eval.t

package info (click to toggle)
libfunction-parameters-perl 2.002006-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 624 kB
  • sloc: perl: 3,945; makefile: 3
file content (77 lines) | stat: -rw-r--r-- 1,553 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
#!perl

use warnings FATAL => 'all';
use strict;

use Function::Parameters;

{
    package TX;

    method new($class: :$chk) { bless { @_ }, $class }

    method check($x) { $self->{chk}($x) }

    method get_message($x) { die "get_message($x)"; }
}

our @trace;

use Function::Parameters {
    def => {
        defaults    => 'function',
        runtime     => 1,
        shift       => [
            [
                '$self' => TX->new(chk => fun ($x) {
                    push @trace, [self_check => $x];
                    1
                })
            ],
        ],
        install_sub => fun ($name, $body) {
            $name = caller . "::$name" unless $name =~ /::/;
            push @trace, [install => $name];
            my $sym = do { no strict 'refs'; \*$name };
            *$sym = $body;
        },
    }
};

package Groovy;
use constant OtherType => TX->new(
    chk => fun ($x) {
        push @trace, [other_check => $x];
        1
    },
);

use Test::More tests => 5;

is_deeply [ splice @trace ], [];

def foo(OtherType $x) { push @trace, [foo => $self, $x]; }

is_deeply [ splice @trace ], [
    [install => 'Groovy::foo'],
];

is eval q{
    def bar(OtherType $x) { push @trace, [bar => $self, $x]; }
    42
}, 42;

is_deeply [ splice @trace ], [
    [install => 'Groovy::bar'],
];

foo('A1', 'A2');
bar('B1', 'B2');
is_deeply [ splice @trace ], [
    [self_check  => 'A1'],
    [other_check => 'A2'],
    [foo         => qw(A1 A2)],
    [self_check  => 'B1'],
    [other_check => 'B2'],
    [bar         => qw(B1 B2)],
];