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