multitest more perl versions; also list successes
[p5sagit/Function-Parameters.git] / t / defaults.t
CommitLineData
63915d26 1#!perl
2
1e0f1595 3use Test::More tests => 46;
63915d26 4
5use warnings FATAL => 'all';
6use strict;
7
8use Function::Parameters {
9 fun => {
10 default_arguments => 1,
11 },
12
13 nofun => {
14 default_arguments => 0,
15 },
16};
17
18fun foo0($x, $y = 1, $z = 3) { $x * 5 + $y * 2 + $z }
19
20is foo0(10), 55;
21is foo0(5, -2), 24;
22is foo0(6, 10, 1), 51;
23
24is fun ($answer = 42) { $answer }->(), 42;
25
26fun sharingan($input, $x = [], $y = {}) {
27 push @$x, $input;
28 $y->{$#$x} = $input;
29 $x, $y
30}
31
32{
33 is_deeply [sharingan 'e'], [['e'], {0 => 'e'}];
34 my $sneaky = ['ants'];
35 is_deeply [sharingan $sneaky], [[['ants']], {0 => ['ants']}];
36 unshift @$sneaky, 'thanks';
37 is_deeply [sharingan $sneaky], [[['thanks', 'ants']], {0 => ['thanks', 'ants']}];
38 @$sneaky = 'thants';
39 is_deeply [sharingan $sneaky], [[['thants']], {0 => ['thants']}];
40}
41
1e0f1595 42is eval('fun ($x, $y = $powersauce) {}'), undef;
63915d26 43like $@, qr/^Global symbol.*explicit package name/;
44
45{
46 my $d = 'outer';
47 my $f;
48 {
49 my $d = 'herp';
50 fun guy($d = $d, $x = $d . '2') {
51 return [$d, $x];
52 }
53
54 is_deeply guy('a', 'b'), ['a', 'b'];
1e0f1595 55 is_deeply guy('c'), ['c', 'c2'];
63915d26 56 is_deeply guy, ['herp', 'herp2'];
57
58 $d = 'ort';
59 is_deeply guy('a', 'b'), ['a', 'b'];
1e0f1595 60 is_deeply guy('c'), ['c', 'c2'];
63915d26 61 is_deeply guy, ['ort', 'ort2'];
62
63 my $g = fun ($alarum = $d) { "[$alarum]" };
64 is $g->(""), "[]";
65 is $g->(), "[ort]";
66
67 $d = 'flowerpot';
1e0f1595 68 is_deeply guy('bloodstain'), ['bloodstain', 'bloodstain2'];
63915d26 69 is $g->(), "[flowerpot]";
70
71 $f = $g;
72 }
73
74 is $f->(), "[flowerpot]";
75 is $f->("Q"), "[Q]";
76}
77
78{
79 my $c = 0;
80 fun edelweiss($x = $c++) :(;$) { $x }
81}
82
83is edelweiss "AAAAA", "AAAAA";
84is_deeply edelweiss [], [];
85is edelweiss, 0;
86is edelweiss, 1;
87is_deeply edelweiss {}, {};
88is edelweiss 0, 0;
89is edelweiss, 2;
90
91for my $f (fun ($wtf = return 'ohi') { "~$wtf" }) {
92 is $f->(""), "~";
93 is $f->("a"), "~a";
94 is $f->(), "ohi";
95}
96
97is eval('fun (@x = 42) {}'), undef;
98like $@, qr/default value/;
99
100is eval('fun ($x, %y = ()) {}'), undef;
101like $@, qr/default value/;
102
103is eval('nofun ($x = 42) {}'), undef;
e158cf8f 104like $@, qr/nofun.*default argument/;
1e0f1595 105
106
107{
108 my $var = "outer";
109
110 fun scope_check(
111 $var, # inner
112 $snd = "${var}2", # initialized from $var)
113 $both = "$var and $snd",
114 ) {
115 return $var, $snd, $both;
116 }
117
118 is_deeply [scope_check 'A'], ['A', 'A2', 'A and A2'];
119 is_deeply [scope_check 'B', 'C'], ['B', 'C', 'B and C'];
120 is_deeply [scope_check 4, 5, 6], [4, 5, 6];
121
122 is eval('fun ($QQQ = $QQQ) {}; 1'), undef;
123 like $@, qr/Global symbol.*\$QQQ.*explicit package name/;
124
125
126 use Function::Parameters { method => 'method' };
127
128 method mscope_check(
129 $var, # inner
130 $snd = "${var}2", # initialized from $var
131 $both = "($self) $var and $snd", # and $self!
132 ) {
133 return $self, $var, $snd, $both;
134 }
135
136 is_deeply [mscope_check '$x', 'A'], ['$x', 'A', 'A2', '($x) A and A2'];
137 is_deeply [mscope_check '$x', 'B', 'C'], ['$x', 'B', 'C', '($x) B and C'];
138 is_deeply [mscope_check '$x', 4, 5, 6], ['$x', 4, 5, 6];
139}