rework keyword properties; add 'defaults', 'strict'
[p5sagit/Function-Parameters.git] / t / bonus.t
1 #!perl
2
3 use Test::More tests => 13;
4
5 use warnings FATAL => 'all';
6 use strict;
7
8 use Function::Parameters {
9         fun => {
10                 defaults => 'function_strict',
11         },
12 };
13
14 fun filter($f = fun ($x) { 1 }, @xs) {
15         !@xs
16                 ? ()
17                 : (($f->($xs[0]) ? $xs[0] : ()), filter $f, @xs[1 .. $#xs])
18 }
19
20 is_deeply [filter], [];
21 is_deeply [filter fun { 1 }, 2 .. 3], [2 .. 3];
22 is_deeply [filter fun ($x) { $x % 2 }, 1 .. 10], [1, 3, 5, 7, 9];
23
24 fun fact($k, $n) :(&$) {
25         $n < 2
26                 ? $k->(1)
27                 : fact { $k->($n * $_[0]) } $n - 1
28 }
29
30 is +(fact { "~@_~" } 5), "~120~";
31 is +(fact { $_[0] / 2 } 6), 360;
32
33 fun 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 }