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