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