Skip to content

Commit

Permalink
Merge e59ba0b into 5e95a28
Browse files Browse the repository at this point in the history
  • Loading branch information
ybrliiu committed Mar 25, 2023
2 parents 5e95a28 + e59ba0b commit d3bec30
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Function/Return.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ sub wrap_sub {
push @src => 'return if !@ret;' if @$types == 0;

# check count
push @src => sprintf(q|_croak "Too few return values for fun %s (expected %s, got @{[map { defined $_ ? $_ : 'undef' } @ret]})" if @ret < %d;|,
push @src => sprintf(q|_croak qq!Too few return values for fun %s (expected %s, got @{[map { defined $_ ? $_ : 'undef' } @ret]})! if @ret < %d;|,
$shortname, "@$types", scalar @$types) if @$types > 0;

push @src => sprintf(q|_croak "Too many return values for fun %s (expected %s, got @{[map { defined $_ ? $_ : 'undef' } @ret]})" if @ret > %d;|,
push @src => sprintf(q|_croak qq!Too many return values for fun %s (expected %s, got @{[map { defined $_ ? $_ : 'undef' } @ret]})! if @ret > %d;|,
$shortname, "@$types", scalar @$types);

# type check
for my $i (0 .. $#$types) {
push @src => sprintf(q|_croak "Invalid return in fun %s: return %d: @{[$types->[%d]->get_message($ret[%d])]}" unless $types->[%d]->check($ret[%d]);|, $shortname, $i, $i, $i, $i,$i)
push @src => sprintf(q|_croak qq!Invalid return in fun %s: return %d: @{[$types->[%d]->get_message($ret[%d])]}! unless $types->[%d]->check($ret[%d]);|, $shortname, $i, $i, $i, $i,$i)
}

push @src => 'return @ret;' if @$types > 1;
Expand Down
6 changes: 6 additions & 0 deletions t/01_wrap_sub.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ subtest 'single return' => sub {
like(exception { w(sub { 1.2 }, [Int]) }, qr!^Invalid return in fun hoge: return 0: Value "1\.2" did not pass type constraint "Int" at t/01_wrap_sub\.t line 14\.$!, 'return value is NOT Int');
};

subtest 'type that takes string parameter return' => sub {
my $Color = Enum[qw( red blue green )];
is(w(sub { 'red' }, [$Color]), 'red', 'return value is ' . $Color->display_name);
like(exception { w(sub { 0 }, [$Color]) }, qr!^Invalid return in fun hoge: return 0: Value "0" did not pass type constraint "Enum\["red","blue","green"]" at t/01_wrap_sub\.t line 14\.$!, 'return value is not ' . $Color->display_name);
};

subtest 'multi return' => sub {
my $required_list_context = qr!^Required list context in fun hoge because of multiple return values function at t/01_wrap_sub\.t line 14\.$!;

Expand Down

0 comments on commit d3bec30

Please sign in to comment.