File: named_refs.t

package info (click to toggle)
libmethod-signatures-perl 20170211-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 672 kB
  • sloc: perl: 3,860; makefile: 2
file content (83 lines) | stat: -r--r--r-- 1,564 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w

use strict;
use warnings;

use lib 't/lib';
use Dev::Null;

use Test::More;

BEGIN {
    plan skip_all => "Data::Alias not available" unless eval { require Data::Alias };
    plan 'no_plan';
}

# Test a basic alias.
{
    package Foo;

    use Test::More;

    use Method::Signatures;

    method add_one_to_each(\:@args) {
        $_++ for @args;
        return @args;
    }

    my @input = (1,2,3);
    is_deeply [Foo->add_one_to_each(args=>\@input)], [2,3,4];
    is_deeply \@input, [2,3,4];

    tie *STDERR, "Dev::Null";
    ok !eval q[@args; 1;], '\@args does not leak out of subroutine';
    untie *STDERR;
}


# Try to break the aliasing prototype
{
    package Bar;

    use Test::More;

    use Method::Signatures;

    method break_args($foo, \:@bar, \:%baz, :$num, \:$biff, ...) {
        return {
            foo         => $foo,
            bar         => \@bar,
            baz         => \%baz,
            num         => $num,
            biff        => $biff,
        }
    }

    is_deeply(
      Bar->break_args(1, bar=>[2,3], baz=>{4 => 5}, num=>6, biff=>\7, (8,9)),
      { foo => 1, bar => [2,3], baz => {4 => 5}, num => 6, biff => 7 }
    );
}


# What about closures?
{
    package Stuff;

    use Method::Signatures;

    method make_closure(\:@nums) {
        return sub {
            return @nums;
        };
    }

    my $closure1 = Stuff->make_closure(nums=>[1,2,3]);
    my $closure2 = Stuff->make_closure(nums=>[4,5,6]);

    ::is_deeply [$closure1->()], [1,2,3];
    ::is_deeply [$closure2->()], [4,5,6];
}