Subject: | incorrect argument fingered in validate w/ optional coerced arg and bogus extra arg |
The attached code illustrates this problem:
cfg_path is an optional class_type argument which may be coerced into
a class from a string. If validate() is passed a valid cfg_path plus
a bogus extra parameter, cfg_path's validation fails; the coercion is
not performed, but the class check is still performed. The correct
error would be that the extra parameter is bogus.
Here's the output on a clean install of Perl 5.20.0 with Type::Tiny
0.047_08 (fails on 0.046 as well):
e% perl bug2.t
ok 1 - just the optional argument
ok 2 - 'the coerced optional argument' isa 'Foo'
ok 3 - an unexpected argument
not ok 4 - both the optional and unexpected arguments
# Failed test 'both the optional and unexpected arguments'
# at bug2.t line 51.
# 'Reference {"cfg_path" => ".","shell" => "."} did not pass type constraint "Dict[cfg_path=>Optional[Path]]" (in $_[0]) at /home/dj/.perlbrew/libs/perl-5.20.0@tmp4/lib/perl5/Test/Fatal.pm line 45
# Reference {"cfg_path" => ".","shell" => "."} did not pass type constraint "Dict[cfg_path=>Optional[Path]]" (in $_[0])
# "Dict[cfg_path=>Optional[Path]]" constrains value at key "cfg_path" of hash with "Optional[Path]"
# Value "." did not pass type constraint "Optional[Path]" (in $_[0]->{"cfg_path"})
# $_[0]->{"cfg_path"} exists
# "Optional[Path]" constrains $_[0]->{"cfg_path"} with "Path" if it exists
# Not a blessed reference
# '
# doesn't match '(?^:does not allow key "shell")'
1..4
# Looks like you failed 1 test of 4.
Subject: | bug2.t |
#! perl
use strict;
use warnings;
use Test::More;
use Test::Fatal;
{ package Foo; sub new { bless {}, shift } };
use Types::Standard qw[ Str Dict Optional ];
use Type::Params qw[ validate ];
use Type::Utils -all;
use Type::Library -base, -declare => qw( Path );
class_type Path, { class => "Foo" };
coerce Path, from Str() => q{ Foo->new($_) }, ;
my @spec = ( Dict [ cfg_path => Optional [Path] ] );
my %args = @ARGV;
is(
exception {
validate( [ {cfg_path => '.'} ], @spec );
},
undef,
'just the optional argument',
);
isa_ok( (validate( [ {cfg_path => '.'} ], @spec ))[0]->{cfg_path}, 'Foo',
'the coerced optional argument');
like(
exception {
validate( [ { shell => '.'} ], @spec );
},
qr/does not allow key "shell"/,
'an unexpected argument',
);
like(
exception {
validate( [ { cfg_path => '.', shell => '.'} ], @spec );
},
qr/does not allow key "shell"/,
'both the optional and unexpected arguments',
);
done_testing;