remove redundant semicolon
[p5sagit/Function-Parameters.git] / t / checkered.t
CommitLineData
63915d26 1#!perl
2
3use Test::More tests => 108;
4
5use warnings FATAL => 'all';
6use strict;
7
8use Function::Parameters {
9 fun => {
10 check_argument_count => 1,
11 default_arguments => 1,
12 },
13
14 sad => {
15 check_argument_count => 0,
16 },
17};
18
19fun error_like($re, $body, $name = undef) {
20 local $@;
21 ok !eval { $body->(); 1 };
22 like $@, $re, $name;
23}
24
25fun foo_any { [@_] }
26fun foo_any_a(@args) { [@args] }
27fun foo_any_b($x = undef, @rest) { [@_] }
28fun foo_0() { [@_] }
29fun foo_1($x) { [@_] }
30fun foo_2($x, $y) { [@_] }
31fun foo_3($x, $y, $z) { [@_] }
32fun foo_0_1($x = 'D0') { [$x] }
33fun foo_0_2($x = 'D0', $y = 'D1') { [$x, $y] }
34fun foo_0_3($x = 'D0', $y, $z = 'D2') { [$x, $y, $z] }
35fun foo_1_2($x, $y = 'D1') { [$x, $y] }
36fun foo_1_3($x, $y = 'D1', $z = 'D2') { [$x, $y, $z] }
37fun foo_2_3($x, $y, $z = 'D2') { [$x, $y, $z] }
38fun foo_1_($x, @y) { [@_] }
39
40is_deeply foo_any, [];
41is_deeply foo_any('a'), ['a'];
42is_deeply foo_any('a', 'b'), ['a', 'b'];
43is_deeply foo_any('a', 'b', 'c'), ['a', 'b', 'c'];
44is_deeply foo_any('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd'];
45
46is_deeply foo_any_a, [];
47is_deeply foo_any_a('a'), ['a'];
48is_deeply foo_any_a('a', 'b'), ['a', 'b'];
49is_deeply foo_any_a('a', 'b', 'c'), ['a', 'b', 'c'];
50is_deeply foo_any_a('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd'];
51
52is_deeply foo_any_b, [];
53is_deeply foo_any_b('a'), ['a'];
54is_deeply foo_any_b('a', 'b'), ['a', 'b'];
55is_deeply foo_any_b('a', 'b', 'c'), ['a', 'b', 'c'];
56is_deeply foo_any_b('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd'];
57
58is_deeply foo_0, [];
59error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a' };
60error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a', 'b' };
61error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a', 'b', 'c' };
62error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a', 'b', 'c', 'd' };
63
64error_like qr/Not enough arguments.*foo_1/, fun { foo_1 };
65is_deeply foo_1('a'), ['a'];
66error_like qr/Too many arguments.*foo_1/, fun { foo_1 'a', 'b' };
67error_like qr/Too many arguments.*foo_1/, fun { foo_1 'a', 'b', 'c' };
68error_like qr/Too many arguments.*foo_1/, fun { foo_1 'a', 'b', 'c', 'd' };
69
70error_like qr/Not enough arguments.*foo_2/, fun { foo_2 };
71error_like qr/Not enough arguments.*foo_2/, fun { foo_2 'a' };
72is_deeply foo_2('a', 'b'), ['a', 'b'];
73error_like qr/Too many arguments.*foo_2/, fun { foo_2 'a', 'b', 'c' };
74error_like qr/Too many arguments.*foo_2/, fun { foo_2 'a', 'b', 'c', 'd' };
75
76error_like qr/Not enough arguments.*foo_3/, fun { foo_3 };
77error_like qr/Not enough arguments.*foo_3/, fun { foo_3 'a' };
78error_like qr/Not enough arguments.*foo_3/, fun { foo_3 'a', 'b' };
79is_deeply foo_3('a', 'b', 'c'), ['a', 'b', 'c'];
80error_like qr/Too many arguments.*foo_3/, fun { foo_3 'a', 'b', 'c', 'd' };
81
82is_deeply foo_0_1, ['D0'];
83is_deeply foo_0_1('a'), ['a'];
84error_like qr/Too many arguments.*foo_0_1/, fun { foo_0_1 'a', 'b' };
85error_like qr/Too many arguments.*foo_0_1/, fun { foo_0_1 'a', 'b', 'c' };
86error_like qr/Too many arguments.*foo_0_1/, fun { foo_0_1 'a', 'b', 'c', 'd' };
87
88is_deeply foo_0_2, ['D0', 'D1'];
89is_deeply foo_0_2('a'), ['a', 'D1'];
90is_deeply foo_0_2('a', 'b'), ['a', 'b'];
91error_like qr/Too many arguments.*foo_0_2/, fun { foo_0_2 'a', 'b', 'c' };
92error_like qr/Too many arguments.*foo_0_2/, fun { foo_0_2 'a', 'b', 'c', 'd' };
93
94is_deeply foo_0_3, ['D0', undef, 'D2'];
95is_deeply foo_0_3('a'), ['a', undef, 'D2'];
96is_deeply foo_0_3('a', 'b'), ['a', 'b', 'D2'];
97is_deeply foo_0_3('a', 'b', 'c'), ['a', 'b', 'c'];
98error_like qr/Too many arguments.*foo_0_3/, fun { foo_0_3 'a', 'b', 'c', 'd' };
99
100error_like qr/Not enough arguments.*foo_1_2/, fun { foo_1_2 };
101is_deeply foo_1_2('a'), ['a', 'D1'];
102is_deeply foo_1_2('a', 'b'), ['a', 'b'];
103error_like qr/Too many arguments.*foo_1_2/, fun { foo_1_2 'a', 'b', 'c' };
104error_like qr/Too many arguments.*foo_1_2/, fun { foo_1_2 'a', 'b', 'c', 'd' };
105
106error_like qr/Not enough arguments.*foo_1_3/, fun { foo_1_3 };
107is_deeply foo_1_3('a'), ['a', 'D1', 'D2'];
108is_deeply foo_1_3('a', 'b'), ['a', 'b', 'D2'];
109is_deeply foo_1_3('a', 'b', 'c'), ['a', 'b', 'c'];
110error_like qr/Too many arguments.*foo_1_3/, fun { foo_1_3 'a', 'b', 'c', 'd' };
111
112error_like qr/Not enough arguments.*foo_2_3/, fun { foo_2_3 };
113error_like qr/Not enough arguments.*foo_2_3/, fun { foo_2_3 'a' };
114is_deeply foo_2_3('a', 'b'), ['a', 'b', 'D2'];
115is_deeply foo_2_3('a', 'b', 'c'), ['a', 'b', 'c'];
116error_like qr/Too many arguments.*foo_2_3/, fun { foo_2_3 'a', 'b', 'c', 'd' };
117
118error_like qr/Not enough arguments.*foo_1_/, fun { foo_1_ };
119is_deeply foo_1_('a'), ['a'];
120is_deeply foo_1_('a', 'b'), ['a', 'b'];
121is_deeply foo_1_('a', 'b', 'c'), ['a', 'b', 'c'];
122is_deeply foo_1_('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd'];
123
124
125sad puppy($eyes) { [@_] }
126sad frog($will, $never) { $will * 3 + (pop) - $never }
127
128is_deeply puppy, [];
129is_deeply puppy('a'), ['a'];
130is_deeply puppy('a', 'b'), ['a', 'b'];
131is_deeply puppy('a', 'b', 'c'), ['a', 'b', 'c'];
132is_deeply puppy('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd'];
133
134is frog(7, 4, 1), 18;
135is frog(7, 4), 21;