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