Skip to content

Add coderef checks to Test2::Tools::Compare #1053

@FractalBoy

Description

@FractalBoy

It would be nice to have coderef checks that work similar to hash/array/object. e.g:

use Test2::V0;

my $coderef = sub { 
    return 1, 1 if wantarray;
    return 1;
};
is($coderef, code { call_list [1, 1]; call 1 }, 'coderef returns 1 in scalar context and 1, 1 in list context');

Today, you have to do something like this:

use Test2::V0;

my $coderef = sub { 
    return 1, 1 if wantarray;
    return 1;
};
is($coderef, meta {
    prop reftype => 'CODE';
    prop this => check_set(
        validator(sub { my $ret = [$_->()]; ($ret->[0] == 1 and $ret->[1] == 1) ? 1 : 0 }),
        validator(sub { $_->() == 1 ? 1 : 0 }),
    );
}, 'coderef returns 1 in scalar context and 1, 1 in list context');

If there's a simpler way to do this today, please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions