Getopt-Compact-WithCmd

 view release on metacpan or  search on metacpan

t/private/parse_command_struct.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Getopt::Compact::WithCmd;

sub test_parse_command_struct {
    my %specs = @_;

    my ($struct, $expects, $args, $opts, $extra_test, $has_error, $desc) =
        @specs{qw/struct expects args opts extra_test has_error desc/};

    subtest $desc => sub {
        local @ARGV = @$args if $args;
        my $go = bless {
            $opts ? %$opts : (),
            _struct => $struct,
        }, 'Getopt::Compact::WithCmd';

        my $got = $go->_parse_command_struct($struct);

        is +$got->{ret}, $expects->{ret}, 'ret value';
        is +$got->{command}, $expects->{command}, 'command';
        is_deeply +$got->{commands}, $expects->{commands}, 'commands';
        is_deeply +$got->{opt}, $expects->{opt}, 'opt';
        is_deeply +$got->{summary}, $expects->{summary}, 'summary';
        is +$got->{error}, $expects->{error}, 'error message';
        
        $extra_test->($got) if $extra_test;

        done_testing;
    };
}

test_parse_command_struct(
    struct  => {},
    expects => {
        ret      => 1,
        command  => undef,
        commands => undef,
        opt      => undef,
        summary  => undef,
    },
    desc => 'missing',
);

test_parse_command_struct(
    struct  => {
        foo => {
            options => [
                [ [qw/hoge/], 'hoge' ],
            ],
            desc => 'foo',
        },
    },
    expects => {
        ret      => 1,
        command  => undef,
        commands => undef,
        opt      => undef,
        summary  => undef,
    },
    desc => 'impl foo / @ARGV = ()',
);

test_parse_command_struct(
    struct  => {
        foo => {
            options => [
                [ [qw/hoge/], 'hoge' ],
            ],
            desc => 'foo',
        },
    },



( run in 0.667 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )