-
Notifications
You must be signed in to change notification settings - Fork 135
/
defaults.t
80 lines (58 loc) · 1.7 KB
/
defaults.t
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
use v6;
use Test;
=begin description
Tests assigning default values to variables of type code in sub definitions.
=end description
# L<S06/Optional parameters/Default values can be calculated at run-time>
plan 11;
sub doubler($x) { return 2 * $x }
sub value_v(Code $func = &doubler) {
return $func(5);
}
is(value_v, 10, "default sub called");
is value_v({3 * $_ }), 15, "default sub can be overridden";
package MyPack {
sub double($x) { return 2 * $x }
our sub val_v(Code :$func = &double) is export {
return $func(5);
}
}
ok((MyPack::val_v), "default sub called in package namespace");
{
sub default_with_list($x = (1, 2)) {
$x[0];
}
is default_with_list(), 1, 'can have a list literal as default value';
}
# RT #69200
{
sub rt69200(Bool :$x) { $x };
is rt69200(:x), True, '":x" is the same as "x => True" in sub call';
}
# RT #118063
{
sub a ( $a=1 --> Hash ) { my %h = ($a => "foo") };
ok a(2)<2> :exists,
'no comma required between parameter with default value and returns-arrow ("-->")';
sub b ( $b=1, --> Hash ) { my %h = ($b => "foo") };
ok b(2)<2> :exists,
'comma allowed between parameter with default value and returns-arrow ("-->")';
}
# RT #123176
{
is -> $a = 0 { $a }(42), 42, "default lambda parameters don't choke on block";
}
# RT #125670
{
sub foo($r = rx{foo}) {
ok $r ~~ Regex, 'rx{foo} works as a default';
}
foo();
}
{
throws-like 'sub foo(|a = 1) { ... }', X::Parameter::Default,
"reject default values for slurpy (|) parameters";
throws-like 'sub foo(*@a = 1) { ... }', X::Parameter::Default,
"reject default values for slurpy (*) parameters";
}
# vim: ft=perl6