remove redundant '; 1' after require
[p5sagit/Function-Parameters.git] / t / types_moose.t
1 #!perl
2 use warnings FATAL => 'all';
3 use strict;
4
5 use Test::More
6         eval { require Moose }
7         ? (tests => 49)
8         : (skip_all => "Moose required for testing types")
9 ;
10 use Test::Fatal;
11
12 use Function::Parameters qw(:strict);
13
14 fun foo(Int $n, CodeRef $f, $x) {
15         $x = $f->($x) for 1 .. $n;
16         $x
17 }
18
19 is foo(0, fun {}, undef), undef;
20 is foo(0, fun {}, "o hai"), "o hai";
21 is foo(3, fun ($x) { "($x)" }, 1.5), "(((1.5)))";
22 is foo(3, fun (Str $x) { "($x)" }, 1.5), "(((1.5)))";
23
24 {
25         my $info = Function::Parameters::info \&foo;
26         is $info->invocant, undef;
27         is $info->slurpy, undef;
28         is $info->positional_optional, 0;
29         is $info->named_required, 0;
30         is $info->named_optional, 0;
31         my @req = $info->positional_required;
32         is @req, 3;
33         is $req[0]->name, '$n';
34         ok $req[0]->type->equals('Int');
35         is $req[1]->name, '$f';
36         ok $req[1]->type->equals('CodeRef');
37         is $req[2]->name, '$x';
38         is $req[2]->type, undef;
39 }
40
41 like exception { foo("ermagerd", fun {}, undef) }, qr/\bparameter 1.+\$n\b.+\bValidation failed\b.+\bInt\b.+ermagerd/;
42 like exception { foo(0, {}, undef) }, qr/\bparameter 2.+\$f\b.+\bValidation failed\b.+\bCodeRef\b/;
43
44 fun bar(((Function::Parameters::info(\&foo)->positional_required)[0]->type) $whoa) { $whoa * 2 }
45
46 is bar(21), 42;
47 {
48         my $info = Function::Parameters::info \&bar;
49         is $info->invocant, undef;
50         is $info->slurpy, undef;
51         is $info->positional_optional, 0;
52         is $info->named_required, 0;
53         is $info->named_optional, 0;
54         my @req = $info->positional_required;
55         is @req, 1;
56         is $req[0]->name, '$whoa';
57         ok $req[0]->type->equals('Int');
58 }
59
60 {
61         my $info = Function::Parameters::info(fun ( ArrayRef [ Int | CodeRef ]@nom) {});
62         is $info->invocant, undef;
63         is $info->positional_required, 0;
64         is $info->positional_optional, 0;
65         is $info->named_required, 0;
66         is $info->named_optional, 0;
67         my $slurpy = $info->slurpy;
68         is $slurpy->name, '@nom';
69         ok $slurpy->type->equals(Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Int|CodeRef]'));
70 }
71
72 {
73         my $phase = 'runtime';
74         BEGIN { $phase = 'A'; }
75         fun
76          baz
77           (
78            (
79             is
80              (
81               $phase
82                ++
83                 ,
84                  'A'
85              )
86               ,
87                'Int'
88            )
89             :
90              $marco
91               ,
92                (
93                 is
94                  (
95                   $phase
96                    ++
97                     ,
98                      'B'
99                  )
100                   ,
101                    q
102                     $ArrayRef[Str]$
103                )
104                 :
105                  $polo
106              )
107               {
108                [
109                 $marco
110                  ,
111                   $polo
112               ]
113           }
114         BEGIN { is $phase, 'C'; }
115         is $phase, 'runtime';
116
117         is_deeply baz(polo => [qw(foo bar baz)], marco => 11), [11, [qw(foo bar baz)]];
118
119         my $info = Function::Parameters::info \&baz;
120         is $info->invocant, undef;
121         is $info->slurpy, undef;
122         is $info->positional_required, 0;
123         is $info->positional_optional, 0;
124         is $info->named_optional, 0;
125         my @req = $info->named_required;
126         is @req, 2;
127         is $req[0]->name, '$marco';
128         ok $req[0]->type->equals('Int');
129         is $req[1]->name, '$polo';
130         ok $req[1]->type->equals('ArrayRef[Str]');
131 }