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