add begin.t from Method::Signatures
[p5sagit/Function-Parameters.git] / t / bonus.t
CommitLineData
23658383 1#!perl
2
3use Test::More tests => 13;
4
5use warnings FATAL => 'all';
6use strict;
7
8use Function::Parameters {
9 fun => {
f7651a6e 10 defaults => 'function_strict',
23658383 11 },
12};
13
14fun filter($f = fun ($x) { 1 }, @xs) {
15 !@xs
16 ? ()
17 : (($f->($xs[0]) ? $xs[0] : ()), filter $f, @xs[1 .. $#xs])
18}
19
20is_deeply [filter], [];
21is_deeply [filter fun { 1 }, 2 .. 3], [2 .. 3];
22is_deeply [filter fun ($x) { $x % 2 }, 1 .. 10], [1, 3, 5, 7, 9];
23
24fun fact($k, $n) :(&$) {
25 $n < 2
26 ? $k->(1)
27 : fact { $k->($n * $_[0]) } $n - 1
28}
29
30is +(fact { "~@_~" } 5), "~120~";
31is +(fact { $_[0] / 2 } 6), 360;
32
33fun write_to($ref) :(\$) :lvalue { $$ref }
34
35{
36 my $x = 2;
37 is $x, 2;
38 write_to($x) = "hi";
39 is $x, "hi";
40 write_to($x)++;
41 is $x, "hj";
42}
43
44{
45 my $c = 0;
46 fun horf_dorf($ref, $val = $c++) :(\@;$) :lvalue {
47 push @$ref, $val;
48 $ref->[-1]
49 }
50}
51
52{
53 my @asdf = "A";
54 is_deeply \@asdf, ["A"];
55 horf_dorf(@asdf) = "b";
56 is_deeply \@asdf, ["A", "b"];
57 ++horf_dorf @asdf;
58 is_deeply \@asdf, ["A", "b", 2];
59 horf_dorf @asdf, 100;
60 is_deeply \@asdf, ["A", "b", 2, 100];
61 splice @asdf, 1, 1;
62 horf_dorf(@asdf) *= 3;
63 is_deeply \@asdf, ["A", 2, 100, 6];
64}