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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
=pod
=encoding utf-8
=head1 PURPOSE
Test positional parameters: required versus optional; lexical
versus localized versus anonymous; various types of defaults.
=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;
fun foo ($x) {
return { '@_' => \@_, '$x' => $x, };
}
fun bar ($, $x) {
return { '@_' => \@_, '$x' => $x, };
}
fun baz ($x, $y) {
return { '@_' => \@_, '$x' => $x, '$y' => $y, };
}
fun quux (${^ONE}, $_) {
return { '@_' => \@_, '${^ONE}' => ${^ONE}, '$_' => $_ };
}
}
is_deeply(
Example::foo('A'),
{ '@_' => ['A'], '$x' => 'A' },
'function with one positional parameter'
);
is_deeply(
Example::bar('A', 'B'),
{ '@_' => ['A', 'B'], '$x' => 'B' },
'function with two positional parameters, the first of which is anonymous'
);
is_deeply(
Example::baz('A', 'B'),
{ '@_' => ['A', 'B'], '$x' => 'A', '$y' => 'B' },
'function with two positional parameters'
);
is_deeply(
Example::quux('A', 'B'),
{ '@_' => ['A', 'B'], '${^ONE}' => 'A', '$_' => 'B' },
'function with two positional parameters using localized global variables'
);
{
package Example2;
use Kavorka;
fun foo ($x?) {
return { '@_' => \@_, '$x' => $x, };
}
fun bar ($x = 42) {
return { '@_' => \@_, '$x' => $x, };
}
fun baz ($x //= 42) {
return { '@_' => \@_, '$x' => $x, };
}
fun quux ($x ||= 42) {
return { '@_' => \@_, '$x' => $x, };
}
fun xyzzy ($x=,$=,$y=) {
return { '@_' => \@_, '$x' => $x, '$y' => $y };
}
}
is_deeply(
Example2::foo(666),
{ '@_' => [666], '$x' => '666' },
'optional positional parameter supplied'
);
is_deeply(
Example2::foo(undef),
{ '@_' => [undef], '$x' => undef },
'optional positional parameter supplied undef'
);
is_deeply(
Example2::foo(),
{ '@_' => [], '$x' => undef },
'optional positional parameter omitted'
);
is_deeply(
Example2::bar(666),
{ '@_' => [666], '$x' => '666' },
'positional parameter with default supplied'
);
is_deeply(
Example2::bar(undef),
{ '@_' => [undef], '$x' => undef },
'positional parameter with default supplied undef'
);
is_deeply(
Example2::bar(),
{ '@_' => [], '$x' => 42 },
'positional parameter with default omitted'
);
is_deeply(
Example2::baz(666),
{ '@_' => [666], '$x' => '666' },
'positional parameter with //=default supplied'
);
is_deeply(
Example2::baz(undef),
{ '@_' => [undef], '$x' => 42 },
'positional parameter with //=default supplied undef'
);
is_deeply(
Example2::baz(0),
{ '@_' => [0], '$x' => 0 },
'positional parameter with //=default supplied false'
);
is_deeply(
Example2::baz(),
{ '@_' => [], '$x' => 42 },
'positional parameter with //=default omitted'
);
is_deeply(
Example2::quux(666),
{ '@_' => [666], '$x' => '666' },
'positional parameter with ||=default supplied'
);
is_deeply(
Example2::quux(undef),
{ '@_' => [undef], '$x' => 42 },
'positional parameter with ||=default supplied undef'
);
is_deeply(
Example2::quux(0),
{ '@_' => [0], '$x' => 42 },
'positional parameter with ||=default supplied false'
);
is_deeply(
Example2::quux(),
{ '@_' => [], '$x' => 42 },
'positional parameter with ||=default omitted'
);
is_deeply(
Example2::xyzzy(42),
{ '@_' => [42], '$x' => 42, '$y' => undef },
'crazy bare =',
);
is_deeply(
Example2::xyzzy(42, 4),
{ '@_' => [42, 4], '$x' => 42, '$y' => undef },
'crazy bare =',
);
is_deeply(
Example2::xyzzy(42, 4, 2),
{ '@_' => [42, 4, 2], '$x' => 42, '$y' => 2 },
'crazy bare =',
);
{
package Example3;
use Kavorka;
fun foo ($x, @) {
return { '@_' => \@_, '$x' => $x, };
}
fun bar ($x, %) {
return { '@_' => \@_, '$x' => $x, };
}
fun baz ($x, ...) {
return { '@_' => \@_, '$x' => $x, };
}
}
is_deeply(
Example3::foo(42, quux => 'xyzzy'),
{ '@_' => [42, quux => 'xyzzy'], '$x' => 42 },
'Signature ($x, @) works',
);
is_deeply(
Example3::bar(42, quux => 'xyzzy'),
{ '@_' => [42, quux => 'xyzzy'], '$x' => 42 },
'Signature ($x, %) works',
);
is_deeply(
Example3::baz(42, quux => 'xyzzy'),
{ '@_' => [42, quux => 'xyzzy'], '$x' => 42 },
'Signature ($x, ...) works',
);
like(
exception { Example3::bar(42, 'xyzzy') },
qr/^Odd number of elements in anonymous hash/,
'Signature ($x, %) can throw',
);
is(
exception { Example3::baz(42, 'xyzzy') },
undef,
"Signature (\$x, ...) won't throw",
);
done_testing;
|