Subject: | type constraint fails after coercion if too many elements in Dict |
Ok, that subject line is not helpful, I know. I've attached code which shows the problem.
I'm coercing a Str into an ArrayRef in a slurpy Dict. If the Dict has more than two elements in the specification, after a coercion I get a type constraint error. Passing an ArrayRef instead of a Str (bypassing the coercion) or removing an element makes the error go away.
Here's what I get:
$ perl type.pl
ok 1 - StrList ArrayRef
# {"connect" => 1,"encoding" => undef,"hg" => ["a"]} did not pass type constraint "Dict[connect=>Optional[Bool],encoding=>Optional[Str],hg=>Optional[StrList]]" (in $SLURPY) at type.pl line 59.
not ok 2 - StrList scalar
# Failed test 'StrList scalar'
# at type.pl line 62.
# Structures begin differing at:
# $got = undef
# $expected = HASH(0x116c0d0)
ok 3 - StrList ArrayRef
ok 4 - StrList scalar
1..4
# Looks like you failed 1 test of 4.
$
Tests 3 & 4 are duplicates of 1 & 2, but with two elements in the Dict instead of three.
Subject: | type.pl |
use strict;
use warnings;
package Types {
use Type::Library
-base,
-declare => qw[ StrList ];
use Type::Utils;
use Types::Standard qw[ ArrayRef Str ];
declare StrList, as ArrayRef [Str];
coerce StrList, from Str, '[$_]';
}
use Test::More;
use Test::Fatal;
use Type::Params qw[ validate ];
use Types::Standard -all;
sub a {
validate(
\@_,
slurpy Dict [
connect => Optional [Bool],
encoding => Optional [Str],
hg => Optional [Types::StrList],
] );
}
sub b {
validate(
\@_,
slurpy Dict [
connect => Optional [Bool],
hg => Optional [Types::StrList],
] );
}
my $expect = {
connect => 1,
hg => ['a'] };
{
my ( $opts, $e );
$e = exception { ( $opts ) = a( connect => 1, hg => ['a'] ) }
and diag $e;
is_deeply( $opts, $expect, "StrList ArrayRef" );
}
{
my ( $opts, $e );
$e = exception { ( $opts ) = a( connect => 1, hg => 'a' ) }
and diag $e;
is_deeply( $opts, $expect, "StrList scalar" );
}
{
my ( $opts, $e );
$e = exception { ( $opts ) = b( connect => 1, hg => ['a'] ) }
and diag $e;
is_deeply( $opts, $expect, "StrList ArrayRef" );
}
{
my ( $opts, $e );
$e = exception { ( $opts ) = b( connect => 1, hg => 'a' ) }
and diag $e;
is_deeply( $opts, $expect, "StrList scalar" );
}
done_testing;