use strict;
use warnings;
use Test::More;

{
    package Mock;
    use Sub::Args;

    sub foo {
        my $args = args(
            {
                name => 1,
                age  => 0,
            }
        );
        $args;
    }

    sub bar {
        args('bbb');
    }

    sub baz {
        my $class = shift;
        my $args = args(
            {
                name => 1,
                age  => 0,
            }, @_
        );
        $args;
    }
}

subtest 'success case / no @_' => sub {
    is_deeply +Mock->foo({name => 'nekokak'}), +{name => 'nekokak'};
    is_deeply +Mock->foo({name => 'nekokak', age => 32}), +{name => 'nekokak', age => 32};
    is_deeply +Mock->foo(name => 'nekokak'), +{name => 'nekokak'};
};

subtest 'success case / use @_' => sub {
    is_deeply +Mock->baz({name => 'nekokak'}), +{name => 'nekokak'};
    is_deeply +Mock->baz({name => 'nekokak', age => 32}), +{name => 'nekokak', age => 32};
    is_deeply +Mock->baz(name => 'nekokak'), +{name => 'nekokak'};
};

subtest 'error case' => sub {
    eval {
        Mock->foo({name => 'nekokak', age => 32, nick => 'inukaku'});
    };
    like $@, qr/not listed in the following parameter: nick./;

    eval {
        Mock->foo(name => 'nekokak', age => 32, nick => 'inukaku');
    };
    like $@, qr/not listed in the following parameter: nick./;

    eval {
        Mock->foo({age => 32});
    };
    like $@, qr/Mandatory parameter 'name' missing./;

    eval {
        Mock->foo(age => 32);
    };
    like $@, qr/Mandatory parameter 'name' missing./;

    eval {
        Mock->bar({age => 32});
    };
    like $@, qr/args method require hashref./;

    eval {
        Mock->bar(age => 32);
    };
    like $@, qr/args method require hashref./;

    eval {
        Mock->foo('aaa');
    };
    like $@, qr/not allow excluding hash or hashref/;
};

done_testing;

1;

