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