multitest more perl versions; also list successes
[p5sagit/Function-Parameters.git] / t / named_params.t
CommitLineData
e158cf8f 1#!perl
2use warnings FATAL => 'all';
3use strict;
4
5use Test::More tests => 134;
6use Test::Fatal;
7
8use Function::Parameters qw(:strict);
9
10sub compile_fail {
11 my ($src, $re, $name) = @_;
12 is eval $src, undef;
13 like $@, $re, $name || ();
14}
15
16
17compile_fail 'fun (:$n1, $p1) {}', qr/\bpositional\b.+\bnamed\b/;
18compile_fail 'fun (@rest, :$n1) {}', qr/\@rest\b.+\$n1\b/;
19compile_fail 'fun (:$n1, :$n1) {}', qr/\$n1\b.+\btwice\b/;
20compile_fail 'method (:$ni:) {}', qr/\binvocant\b.+\$ni\b.+\bnamed\b/;
21
22
23fun name_1(:$n1) { [$n1, @_] }
24
25like exception { name_1 }, qr/Not enough arguments/;
26like exception { name_1 'n1' }, qr/Not enough arguments/;
27like exception { name_1 'asdf' }, qr/Not enough arguments/;
fc634bba 28like exception { name_1 n1 => 0, huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
e158cf8f 29is_deeply name_1(n1 => undef), [undef, n1 => undef];
30is_deeply name_1(n1 => 'a'), ['a', n1 => 'a'];
31is_deeply name_1(n1 => 'a', n1 => 'b'), ['b', n1 => 'a', n1 => 'b'];
32is_deeply name_1(n1 => 'a', n1 => undef), [undef, n1 => 'a', n1 => undef];
33
34
35fun name_0_1(:$n1 = 'd') { [$n1, @_] }
36
37is_deeply name_0_1, ['d'];
38like exception { name_0_1 'n1' }, qr/Odd number/;
39like exception { name_0_1 'asdf' }, qr/Odd number/;
40like exception { name_0_1 huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
41is_deeply name_0_1(n1 => 'a'), ['a', n1 => 'a'];
42is_deeply name_0_1(n1 => 'a', n1 => 'b'), ['b', n1 => 'a', n1 => 'b'];
43is_deeply name_0_1(n1 => 'a', n1 => undef), [undef, n1 => 'a', n1 => undef];
44
45
46fun pos_1_name_1($p1, :$n1) { [$p1, $n1, @_] }
47
48like exception { pos_1_name_1 }, qr/Not enough arguments/;
49like exception { pos_1_name_1 42 }, qr/Not enough arguments/;
50like exception { pos_1_name_1 42, 'n1' }, qr/Not enough arguments/;
51like exception { pos_1_name_1 42, 'asdf' }, qr/Not enough arguments/;
fc634bba 52like exception { pos_1_name_1 42, n1 => 0, huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
e158cf8f 53is_deeply pos_1_name_1(42, n1 => undef), [42, undef, 42, n1 => undef];
54is_deeply pos_1_name_1(42, n1 => 'a'), [42, 'a', 42, n1 => 'a'];
55is_deeply pos_1_name_1(42, n1 => 'a', n1 => 'b'), [42, 'b', 42, n1 => 'a', n1 => 'b'];
56is_deeply pos_1_name_1(42, n1 => 'a', n1 => undef), [42, undef, 42, n1 => 'a', n1 => undef];
57
58
59compile_fail 'fun pos_0_1_name_1($p1 = "e", :$n1) { [$p1, $n1, @_] }', qr/\boptional positional\b.+\brequired named\b/;
60
61
62fun pos_1_name_0_1($p1, :$n1 = 'd') { [$p1, $n1, @_] }
63
64like exception { pos_1_name_0_1 }, qr/Not enough arguments/;
65is_deeply pos_1_name_0_1(42), [42, 'd', 42];
66like exception { pos_1_name_0_1 42, 'n1' }, qr/Odd number/;
67like exception { pos_1_name_0_1 42, 'asdf' }, qr/Odd number/;
68like exception { pos_1_name_0_1 42, huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
69is_deeply pos_1_name_0_1(42, n1 => undef), [42, undef, 42, n1 => undef];
70is_deeply pos_1_name_0_1(42, n1 => 'a'), [42, 'a', 42, n1 => 'a'];
71is_deeply pos_1_name_0_1(42, n1 => 'a', n1 => 'b'), [42, 'b', 42, n1 => 'a', n1 => 'b'];
72is_deeply pos_1_name_0_1(42, n1 => 'a', n1 => undef), [42, undef, 42, n1 => 'a', n1 => undef];
73
74
75fun pos_0_1_name_0_1($p1 = 'e', :$n1 = 'd') { [$p1, $n1, @_] }
76
77is_deeply pos_0_1_name_0_1, ['e', 'd'];
78is_deeply pos_0_1_name_0_1(42), [42, 'd', 42];
79like exception { pos_0_1_name_0_1 42, 'n1' }, qr/Odd number/;
80like exception { pos_0_1_name_0_1 42, 'asdf' }, qr/Odd number/;
81like exception { pos_0_1_name_0_1 42, huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
82is_deeply pos_0_1_name_0_1(42, n1 => undef), [42, undef, 42, n1 => undef];
83is_deeply pos_0_1_name_0_1(42, n1 => 'a'), [42, 'a', 42, n1 => 'a'];
84is_deeply pos_0_1_name_0_1(42, n1 => 'a', n1 => 'b'), [42, 'b', 42, n1 => 'a', n1 => 'b'];
85is_deeply pos_0_1_name_0_1(42, n1 => 'a', n1 => undef), [42, undef, 42, n1 => 'a', n1 => undef];
86
87
88fun name_1_slurp(:$n1, @rest) { [$n1, \@rest, @_] }
89
90like exception { name_1_slurp }, qr/Not enough arguments/;
91like exception { name_1_slurp 'n1' }, qr/Not enough arguments/;
92like exception { name_1_slurp 'asdf' }, qr/Not enough arguments/;
f0c98476 93like exception { name_1_slurp huh => 1 }, qr/missing named\b.+\bn1\b/;
e158cf8f 94is_deeply name_1_slurp(n1 => 'a'), ['a', [], n1 => 'a'];
95like exception { name_1_slurp n1 => 'a', 'n1' }, qr/Odd number/;
96is_deeply name_1_slurp(n1 => 'a', foo => 'bar'), ['a', [foo => 'bar'], n1 => 'a', foo => 'bar'];
fc634bba 97is_deeply name_1_slurp(foo => 'bar', n1 => 'a', foo => 'quux'), ['a', [foo => 'quux'], foo => 'bar', n1 => 'a', foo => 'quux'];
e158cf8f 98
99
100fun name_0_1_slurp(:$n1 = 'd', @rest) { [$n1, \@rest, @_] }
101
102is_deeply name_0_1_slurp, ['d', []];
103like exception { name_0_1_slurp 'n1' }, qr/Odd number/;
104like exception { name_0_1_slurp 'asdf' }, qr/Odd number/;
105is_deeply name_0_1_slurp(n1 => 'a'), ['a', [], n1 => 'a'];
106like exception { name_0_1_slurp n1 => 'a', 'n1' }, qr/Odd number/;
107is_deeply name_0_1_slurp(a => 'b'), ['d', [a => 'b'], a => 'b'];
108is_deeply name_0_1_slurp(n1 => 'a', foo => 'bar'), ['a', [foo => 'bar'], n1 => 'a', foo => 'bar'];
fc634bba 109is_deeply name_0_1_slurp(foo => 'bar', n1 => 'a', foo => 'quux'), ['a', [foo => 'quux'], foo => 'bar', n1 => 'a', foo => 'quux'];
e158cf8f 110
111
112fun name_2(:$n1, :$n2) { [$n1, $n2, @_] }
113
114like exception { name_2 }, qr/Not enough arguments/;
115like exception { name_2 'n1' }, qr/Not enough arguments/;
116like exception { name_2 'asdf' }, qr/Not enough arguments/;
117like exception { name_2 huh => 1 }, qr/Not enough arguments/;
118like exception { name_2 n1 => 'a' }, qr/Not enough arguments/;
f0c98476 119like exception { name_2 n1 => 'a', n1 => 'b' }, qr/missing named\b.+\bn2\b/;
e158cf8f 120like exception { name_2 n2 => 'a' }, qr/Not enough arguments/;
f0c98476 121like exception { name_2 n2 => 'a', n2 => 'b' }, qr/missing named\b.+\bn1\b/;
e158cf8f 122like exception { name_2 n1 => 'a', 'n2' }, qr/Not enough arguments/;
123like exception { name_2 n1 => 'a', 'asdf' }, qr/Not enough arguments/;
124like exception { name_2 n2 => 'b', n1 => 'a', huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
125is_deeply name_2(n2 => 42, n1 => undef), [undef, 42, n2 => 42, n1 => undef];
126is_deeply name_2(n2 => 42, n1 => 'a'), ['a', 42, n2 => 42, n1 => 'a'];
127is_deeply name_2(n2 => 42, n1 => 'a', n1 => 'b'), ['b', 42, n2 => 42, n1 => 'a', n1 => 'b'];
128is_deeply name_2(n2 => 42, n1 => 'a', n1 => undef), [undef, 42, n2 => 42, n1 => 'a', n1 => undef];
129is_deeply name_2(n1 => undef, n2 => 42), [undef, 42, n1 => undef, n2 => 42];
130is_deeply name_2(n1 => 'a', n2 => 42), ['a', 42, n1 => 'a', n2 => 42];
131is_deeply name_2(n1 => 'a', n1 => 'b', n2 => 42), ['b', 42, n1 => 'a', n1 => 'b', n2 => 42];
132is_deeply name_2(n1 => 'a', n2 => 42, n1 => undef), [undef, 42, n1 => 'a', n2 => 42, n1 => undef];
133
134
135fun name_1_2(:$n1, :$n2 = 'f') { [$n1, $n2, @_] }
136
137like exception { name_1_2 }, qr/Not enough arguments/;
138like exception { name_1_2 'n1' }, qr/Not enough arguments/;
139like exception { name_1_2 'asdf' }, qr/Not enough arguments/;
fc634bba 140like exception { name_1_2 n1 => 0, huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
e158cf8f 141is_deeply name_1_2(n1 => 'a'), ['a', 'f', n1 => 'a'];
142is_deeply name_1_2(n1 => 'a', n1 => 'b'), ['b', 'f', n1 => 'a', n1 => 'b'];
f0c98476 143like exception { name_1_2 n2 => 'a' }, qr/missing named\b.+\bn1\b/;
144like exception { name_1_2 n2 => 'a', n2 => 'b' }, qr/missing named\b.+\bn1\b/;
e158cf8f 145like exception { name_1_2 n1 => 'a', 'n2' }, qr/Odd number/;
146like exception { name_1_2 n1 => 'a', 'asdf' }, qr/Odd number/;
147like exception { name_1_2 n2 => 'b', n1 => 'a', huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
148is_deeply name_1_2(n2 => 42, n1 => undef), [undef, 42, n2 => 42, n1 => undef];
149is_deeply name_1_2(n2 => 42, n1 => 'a'), ['a', 42, n2 => 42, n1 => 'a'];
150is_deeply name_1_2(n2 => 42, n1 => 'a', n1 => 'b'), ['b', 42, n2 => 42, n1 => 'a', n1 => 'b'];
151is_deeply name_1_2(n2 => 42, n1 => 'a', n1 => undef), [undef, 42, n2 => 42, n1 => 'a', n1 => undef];
152is_deeply name_1_2(n1 => undef, n2 => 42), [undef, 42, n1 => undef, n2 => 42];
153is_deeply name_1_2(n1 => 'a', n2 => 42), ['a', 42, n1 => 'a', n2 => 42];
154is_deeply name_1_2(n1 => 'a', n1 => 'b', n2 => 42), ['b', 42, n1 => 'a', n1 => 'b', n2 => 42];
155is_deeply name_1_2(n1 => 'a', n2 => 42, n1 => undef), [undef, 42, n1 => 'a', n2 => 42, n1 => undef];
156
157
158fun name_0_2(:$n1 = 'd', :$n2 = 'f') { [$n1, $n2, @_] }
159
160is_deeply name_0_2, ['d', 'f'];
161like exception { name_0_2 'n1' }, qr/Odd number/;
162like exception { name_0_2 'asdf' }, qr/Odd number/;
163like exception { name_0_2 huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
164is_deeply name_0_2(n1 => 'a'), ['a', 'f', n1 => 'a'];
165is_deeply name_0_2(n1 => 'a', n1 => 'b'), ['b', 'f', n1 => 'a', n1 => 'b'];
166is_deeply name_0_2(n2 => 'a'), ['d', 'a', n2 => 'a'];
167is_deeply name_0_2(n2 => 'a', n2 => 'b'), ['d', 'b', n2 => 'a', n2 => 'b'];
168like exception { name_0_2 n1 => 'a', 'n2' }, qr/Odd number/;
169like exception { name_0_2 n1 => 'a', 'asdf' }, qr/Odd number/;
170like exception { name_0_2 n2 => 'b', n1 => 'a', huh => 1 }, qr/\bnamed\b.+\bhuh\b/;
171is_deeply name_0_2(n2 => 42, n1 => undef), [undef, 42, n2 => 42, n1 => undef];
172is_deeply name_0_2(n2 => 42, n1 => 'a'), ['a', 42, n2 => 42, n1 => 'a'];
173is_deeply name_0_2(n2 => 42, n1 => 'a', n1 => 'b'), ['b', 42, n2 => 42, n1 => 'a', n1 => 'b'];
174is_deeply name_0_2(n2 => 42, n1 => 'a', n1 => undef), [undef, 42, n2 => 42, n1 => 'a', n1 => undef];
175is_deeply name_0_2(n1 => undef, n2 => 42), [undef, 42, n1 => undef, n2 => 42];
176is_deeply name_0_2(n1 => 'a', n2 => 42), ['a', 42, n1 => 'a', n2 => 42];
177is_deeply name_0_2(n1 => 'a', n1 => 'b', n2 => 42), ['b', 42, n1 => 'a', n1 => 'b', n2 => 42];
178is_deeply name_0_2(n1 => 'a', n2 => 42, n1 => undef), [undef, 42, n1 => 'a', n2 => 42, n1 => undef];
179
180
fc634bba 181fun pos_1_2_name_0_3_slurp($p1, $p2 = 'E', :$n1 = undef, :$n2 = 'A', :$n3 = 'F', @rest) { [$p1, $p2, $n1, $n2, $n3, {@rest}, @_] }
e158cf8f 182
183like exception { pos_1_2_name_0_3_slurp }, qr/Not enough/;
fc634bba 184is_deeply pos_1_2_name_0_3_slurp('a'), ['a', 'E', undef, 'A', 'F', {}, 'a'];
185is_deeply pos_1_2_name_0_3_slurp('a', 'b'), ['a', 'b', undef, 'A', 'F', {}, 'a', 'b'];
e158cf8f 186like exception { pos_1_2_name_0_3_slurp 'a', 'b', 'c' }, qr/Odd number/;
fc634bba 187is_deeply pos_1_2_name_0_3_slurp('a', 'b', 'c', 'd'), ['a', 'b', undef, 'A', 'F', {'c', 'd'}, 'a', 'b', 'c', 'd'];
e158cf8f 188like exception { pos_1_2_name_0_3_slurp 'a', 'b', 'c', 'd', 'e' }, qr/Odd number/;
fc634bba 189is_deeply pos_1_2_name_0_3_slurp('a', 'b', 'c', 'd', 'e', 'f'), ['a', 'b', undef, 'A', 'F', {'c', 'd', 'e', 'f'}, 'a', 'b', 'c', 'd', 'e', 'f'];
190is_deeply pos_1_2_name_0_3_slurp('a', 'b', n2 => 'c', n1 => 'd'), ['a', 'b', 'd', 'c', 'F', {}, 'a', 'b', n2 => 'c', n1 => 'd'];
191is_deeply pos_1_2_name_0_3_slurp('a', 'b', n2 => 'c', beans => 'legume', n1 => 'd'), ['a', 'b', 'd', 'c', 'F', {beans => 'legume'}, 'a', 'b', n2 => 'c', beans => 'legume', n1 => 'd'];