1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
=pod
=encoding utf-8
=head1 PURPOSE
Test L<Type::Params> with more complex Dict coercion.
=head1 SEE ALSO
L<https://rt.cpan.org/Ticket/Display.html?id=86004>.
=head1 AUTHOR
Diab Jerius E<lt>djerius@cpan.orgE<gt>.
(Minor changes by Toby Inkster E<lt>tobyink@cpan.orgE<gt>.)
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014 by Diab Jerius.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
use strict;
use warnings;
BEGIN {
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, q { [$_] };
};
use Test::More;
use Test::Fatal;
use Type::Params qw[ validate compile ];
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],
]
);
}
sub c {
validate(
\@_,
slurpy Dict [
connect => Optional [Bool],
encoding => Optional [Str],
hg2 => Optional [Types::StrList->no_coercions->plus_coercions(Types::Standard::Str, sub {[$_]})],
]
);
}
my $expect = {
connect => 1,
hg => ['a'],
};
my $expect2 = {
connect => 1,
hg2 => ['a'],
};
# 1
{
my ( $opts, $e );
$e = exception { ( $opts ) = a( connect => 1, hg => ['a'] ) }
and diag $e;
is_deeply( $opts, $expect, "StrList ArrayRef" );
}
# 2
{
my ( $opts, $e );
$e = exception { ( $opts ) = a( connect => 1, hg => 'a' ) }
and diag $e;
is_deeply( $opts, $expect, "StrList scalar" );
}
# 3
{
my ( $opts, $e );
$e = exception { ( $opts ) = b( connect => 1, hg => ['a'] ) }
and diag $e;
is_deeply( $opts, $expect, "StrList ArrayRef" );
}
# 4
{
my ( $opts, $e );
$e = exception { ( $opts ) = b( connect => 1, hg => 'a' ) }
and diag $e;
is_deeply( $opts, $expect, "StrList scalar" );
}
# 5
{
my ( $opts, $e );
$e = exception { ( $opts ) = c( connect => 1, hg2 => ['a'] ) }
and diag $e;
is_deeply( $opts, $expect2, "StrList ArrayRef - noninline" );
}
# 6
{
my ( $opts, $e );
$e = exception { ( $opts ) = c( connect => 1, hg2 => 'a' ) }
and diag $e;
is_deeply( $opts, $expect2, "StrList scalar - noninline" );
}
#note compile(
# { want_source => 1 },
# slurpy Dict [
# connect => Optional[Bool],
# encoding => Optional[Str],
# hg => Optional[Types::StrList],
# ],
#);
done_testing;
|