Skip to content

Commit

Permalink
improve: types not only list
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Sep 1, 2021
1 parent 84a9adb commit 1e1eb42
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 12 additions & 2 deletions lib/Function/Return.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ sub _register_submeta {
my ($class, $pkg, $sub, $types) = @_;

my $meta = Sub::Meta->new(sub => $sub, stashname => $pkg);
$meta->set_returns(list => $types);
$meta->set_returns(_normalize_types($types));

if (my $materials = Sub::Meta::Finder::FunctionParameters::find_materials($sub)) {
$meta->set_is_method($materials->{is_method});
Expand All @@ -156,7 +156,7 @@ sub _register_submeta_and_install {
my $wrapped = $class->wrap_sub($sub, $types);

my $meta = Sub::Meta->new(sub => $wrapped, stashname => $pkg);
$meta->set_returns(list => $types);
$meta->set_returns(_normalize_types($types));

if (my $materials = Sub::Meta::Finder::FunctionParameters::find_materials($sub)) {
$meta->set_is_method($materials->{is_method});
Expand All @@ -174,6 +174,16 @@ sub _register_submeta_and_install {
return;
}

sub _normalize_types {
my $types = shift;
if (@$types == 1) {
return $types->[0];
}
else {
return $types;
}
}

1;
__END__
Expand Down
16 changes: 13 additions & 3 deletions t/03_meta.t
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ subtest 'single' => sub {
subname($code);
my $meta = Function::Return::meta $code;
isa_ok $meta, 'Sub::Meta';
is_deeply $meta->returns->list, [Str];
is $meta->returns->list, Str;
is $meta->returns->scalar, Str;
is $meta->returns->void, Str;
}
};

Expand All @@ -45,6 +47,8 @@ subtest 'multi' => sub {
my $meta = Function::Return::meta $code;
isa_ok $meta, 'Sub::Meta';
is_deeply $meta->returns->list, [Str, Int];
is_deeply $meta->returns->scalar, [Str, Int];
is_deeply $meta->returns->void, [Str, Int];
}
};

Expand All @@ -54,6 +58,8 @@ subtest 'empty' => sub {
my $meta = Function::Return::meta $code;
isa_ok $meta, 'Sub::Meta';
is_deeply $meta->returns->list, [];
is_deeply $meta->returns->scalar, [];
is_deeply $meta->returns->void, [];
}
};

Expand All @@ -70,7 +76,9 @@ subtest 'with_fp_fun' => sub {
note subname($code);
my $meta = Function::Return::meta $code;
isa_ok $meta, 'Sub::Meta';
is_deeply $meta->returns->list, [Num];
is $meta->returns->list, Num;
is $meta->returns->scalar, Num;
is $meta->returns->void, Num;

my $pinfo = Function::Parameters::info \&with_fp_fun;
is $pinfo, undef;
Expand All @@ -88,7 +96,9 @@ subtest 'with_fp_method' => sub {
note subname($code);
my $meta = Function::Return::meta $code;
isa_ok $meta, 'Sub::Meta';
is_deeply $meta->returns->list, [Num];
is $meta->returns->list, Num;
is $meta->returns->scalar, Num;
is $meta->returns->void, Num;

my $pinfo = Function::Parameters::info \&with_fp_method;
is $pinfo, undef;
Expand Down

0 comments on commit 1e1eb42

Please sign in to comment.