File: types_moose_3.t

package info (click to toggle)
libfunction-parameters-perl 2.002005-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: perl: 3,945; makefile: 3
file content (133 lines) | stat: -rw-r--r-- 3,297 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!perl
use warnings FATAL => 'all';
use strict;

use Test::More
    eval { require Moose }
    ? (tests => 49)
    : (skip_all => "Moose required for testing types")
;
use Test::Fatal;

use Function::Parameters {
    def => { strict => 1, reify_type => 'moose' },
};

def foo(Int $n, CodeRef $f, $x) {
    $x = $f->($x) for 1 .. $n;
    $x
}

is foo(0, def (@) {}, undef), undef;
is foo(0, def (@) {}, "o hai"), "o hai";
is foo(3, def ($x) { "($x)" }, 1.5), "(((1.5)))";
is foo(3, def (Str $x) { "($x)" }, 1.5), "(((1.5)))";

{
    my $info = Function::Parameters::info \&foo;
    is $info->invocant, undef;
    is $info->slurpy, undef;
    is $info->positional_optional, 0;
    is $info->named_required, 0;
    is $info->named_optional, 0;
    my @req = $info->positional_required;
    is @req, 3;
    is $req[0]->name, '$n';
    ok $req[0]->type->equals('Int');
    is $req[1]->name, '$f';
    ok $req[1]->type->equals('CodeRef');
    is $req[2]->name, '$x';
    is $req[2]->type, undef;
}

like exception { foo("ermagerd", def (@) {}, undef) }, qr/\bparameter 1.+\$n\b.+\bValidation failed\b.+\bInt\b.+ermagerd/;
like exception { foo(0, {}, undef) }, qr/\bparameter 2.+\$f\b.+\bValidation failed\b.+\bCodeRef\b/;

def bar(((Function::Parameters::info(\&foo)->positional_required)[0]->type) $whoa) { $whoa * 2 }

is bar(21), 42;
{
    my $info = Function::Parameters::info \&bar;
    is $info->invocant, undef;
    is $info->slurpy, undef;
    is $info->positional_optional, 0;
    is $info->named_required, 0;
    is $info->named_optional, 0;
    my @req = $info->positional_required;
    is @req, 1;
    is $req[0]->name, '$whoa';
    ok $req[0]->type->equals('Int');
}

{
    my $info = Function::Parameters::info(def ( ArrayRef [ Int | CodeRef ]@nom) {});
    is $info->invocant, undef;
    is $info->positional_required, 0;
    is $info->positional_optional, 0;
    is $info->named_required, 0;
    is $info->named_optional, 0;
    my $slurpy = $info->slurpy;
    is $slurpy->name, '@nom';
    ok $slurpy->type->equals(Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int|CodeRef]'));
}

{
    my $phase = 'runtime';
    BEGIN { $phase = 'A'; }
    def
     baz
      (
       (
        is
         (
          $phase
           ++
            ,
             'A'
         )
          ,
           'Int'
       )
        :
         $marco
          ,
           (
            is
             (
              $phase
               ++
                ,
                 'B'
             )
              ,
               q
                $ArrayRef[Str]$
           )
            :
             $polo
         )
          {
           [
            $marco
             ,
              $polo
          ]
      }
    BEGIN { is $phase, 'C'; }
    is $phase, 'runtime';

    is_deeply baz(polo => [qw(foo bar baz)], marco => 11), [11, [qw(foo bar baz)]];

    my $info = Function::Parameters::info \&baz;
    is $info->invocant, undef;
    is $info->slurpy, undef;
    is $info->positional_required, 0;
    is $info->positional_optional, 0;
    is $info->named_optional, 0;
    my @req = $info->named_required;
    is @req, 2;
    is $req[0]->name, '$marco';
    ok $req[0]->type->equals('Int');
    is $req[1]->name, '$polo';
    ok $req[1]->type->equals('ArrayRef[Str]');
}