Skip to content

Commit

Permalink
Add tests for RT #118555
Browse files Browse the repository at this point in the history
Optional typed Array and Hash parameters should act like regular
optional Array and Hash paramters (just with a type constraint) and not
die.
  • Loading branch information
MasterDuke17 committed Apr 27, 2017
1 parent 2d9194f commit 785fea0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion S06-signature/optional.t
Expand Up @@ -3,7 +3,7 @@ use Test;

# L<S06/Optional parameters/>

plan 31;
plan 35;

sub opt1($p?) { defined($p) ?? $p !! 'undef'; }

Expand Down Expand Up @@ -82,6 +82,17 @@ is opt_array2(), 0, "optional array not passed is empty (copy)";
is opt_hash1(), 0, "optional hash not passed is empty";
is opt_hash2(), 0, "optional hash not passed is empty (copy)";

# RT #118555
{
sub opt_array(Int @foo?) { @foo.push(42); @foo };
is-deeply opt_array(), Array[Int].new(42), 'can assign to an optional typed array not passed';
is-deeply opt_array(Array[Int].new(1)), Array[Int].new(1, 42), 'can assign to an optional typed array that is passed';

sub opt_hash(Int %foo?) { %foo<bar> = 42; %foo };
is-deeply opt_hash(), (my Int % = :bar(42)), 'can assign to an optional typed hash not passed';
is-deeply opt_hash(my Int % = :baz(1)), (my Int % = :baz(1), :bar(42)), 'can assign to an optional typed hash that is passed';
}

# RT #71110
throws-like 'sub opt($a = 1, $b) { }', X::Parameter::WrongOrder,
'Cannot put required parameter after optional parameters';
Expand Down

0 comments on commit 785fea0

Please sign in to comment.