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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
=pod
=encoding utf-8
=head1 PURPOSE
Test named parameters: required versus optional; various types of
defaults; long names.
Tests that C<< %_ >> reflects named parameters.
Checks that named parameters work with an odd or even number of leading
positional parameters and/or invocants.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014, 2017 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{
package Example;
use Kavorka;
our $zzz = 'package variable';
fun foo ($x, :$y) {
return { '@_' => \@_, '%_' => \%_, '$x' => $x, '$y' => $y, };
}
fun bar ($, $x, :$y) {
return { '@_' => \@_, '%_' => \%_, '$x' => $x, '$y' => $y, };
}
fun baz (:$x, :$y!) {
return { '@_' => \@_, '%_' => \%_, '$x' => $x, '$y' => $y, };
}
fun quux (:zzz($z)) {
return { '@_' => \@_, '%_' => \%_, '$zzz' => $zzz, '$z' => $z };
}
}
#diag explain( Kavorka->info(\&Example::baz) );
is_deeply(
Example::foo(666, y => 42),
{ '@_' => [666, y => 42], '%_' => { y => 42 }, '$x' => 666, '$y' => 42 },
'single positional followed by a named parameter',
);
is_deeply(
Example::foo(666),
{ '@_' => [666], '%_' => { }, '$x' => 666, '$y' => undef },
'single positional followed by a named parameter - named parameters are optional',
);
is_deeply(
Example::bar(999, 666, y => 42),
{ '@_' => [999, 666, y => 42], '%_' => { y => 42 }, '$x' => 666, '$y' => 42 },
'two positionals followed by a named parameter',
);
is_deeply(
Example::bar(999, 666),
{ '@_' => [999, 666], '%_' => { }, '$x' => 666, '$y' => undef },
'two positionals followed by a named parameter - named parameters are optional',
);
is_deeply(
Example::baz(x => 666, y => 42),
{ '@_' => [x => 666, y => 42], '%_' => { x => 666, y => 42 }, '$x' => 666, '$y' => 42 },
'two named parameters',
);
is_deeply(
Example::baz({ x => 666, y => 42 }),
{ '@_' => [{ x => 666, y => 42 }], '%_' => { x => 666, y => 42 }, '$x' => 666, '$y' => 42 },
'two named parameters (passed as hashref)',
);
is_deeply(
Example::baz(y => 42),
{ '@_' => [y => 42], '%_' => { y => 42 }, '$x' => undef, '$y' => 42 },
'two named parameters - omit the optional one',
);
like(
exception { Example::baz(x => 666) },
qr{^Named parameter .y. is required},
'two named parameters - omit the required one; throws',
);
is_deeply(
Example::quux(zzz => 42),
{ '@_' => [zzz => 42], '%_' => { zzz => 42 }, '$z' => 42, '$zzz' => 'package variable' },
'long named parameter',
);
like(
exception { Example::quux(z => 666) },
qr{^Unknown named parameter: z},
'long named parameter cannot be invoked with its short name',
);
{
package Example2;
use Kavorka;
fun xxx ( :foo( :bar(:baz($x) )) , ... )
{
return $x;
}
fun yyy ( :foo( :bar(:baz(:$x) )) , ... )
{
return $x;
}
fun zzz ( :foo :bar :baz :$x, ... )
{
return $x;
}
fun www ( :foo :bar :baz $x, ... )
{
return $x;
}
}
is_deeply(
[ Example2::www(foo => 40), Example2::www(bar => 41), Example2::www(baz => 42), Example2::www(x => 43) ],
[ 40 .. 42, undef ],
'multi-named parameters'
);
is_deeply(
[ Example2::xxx(foo => 40), Example2::xxx(bar => 41), Example2::xxx(baz => 42), Example2::xxx(x => 43) ],
[ 40 .. 42, undef ],
'multi-named parameters'
);
is_deeply(
[ Example2::yyy(foo => 40), Example2::yyy(bar => 41), Example2::yyy(baz => 42), Example2::yyy(x => 43) ],
[ 40 .. 42, 43 ],
'multi-named parameters'
);
is_deeply(
[ Example2::zzz(foo => 40), Example2::zzz(bar => 41), Example2::zzz(baz => 42), Example2::zzz(x => 43) ],
[ 40 .. 42, 43 ],
'multi-named parameters'
);
done_testing;
|