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