[libtype-tiny-perl] 13/46: patch for RT#98362 (from DAKKAR at cpan.org) - mixture of optional values and non-inlineable coercions within a Dict

Jonas Smedegaard dr at jones.dk
Fri Sep 12 18:48:02 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 1.001_000
in repository libtype-tiny-perl.

commit 06040cbaaa444c0a42c831de9e4504798b28188a
Author: Toby Inkster <mail at tobyinkster.co.uk>
Date:   Thu Aug 28 22:22:18 2014 +0100

    patch for RT#98362 (from DAKKAR at cpan.org) - mixture of optional values and non-inlineable coercions within a Dict
    
    --HG--
    extra : source : 94832fb61ddd0b20a067c90ad023256b2e53cacc
---
 lib/Types/Standard/Dict.pm                |  4 ++--
 t/20-unit/Types-Standard/deep-coercions.t | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/lib/Types/Standard/Dict.pm b/lib/Types/Standard/Dict.pm
index 8657fd3..4bec45f 100644
--- a/lib/Types/Standard/Dict.pm
+++ b/lib/Types/Standard/Dict.pm
@@ -247,8 +247,8 @@ sub __coercion_generator
 	else
 	{
 		my %is_optional = map {
-			; $_ => $dict{$_}->is_strictly_a_type_of($_optional)
-		} keys %dict;
+			; $_ => !!$dict{$_}->is_strictly_a_type_of($_optional)
+		} sort keys %dict;
 		$C->add_type_coercions(
 			$parent => sub {
 				my $value = @_ ? $_[0] : $_;
diff --git a/t/20-unit/Types-Standard/deep-coercions.t b/t/20-unit/Types-Standard/deep-coercions.t
index b28b30e..080abe7 100644
--- a/t/20-unit/Types-Standard/deep-coercions.t
+++ b/t/20-unit/Types-Standard/deep-coercions.t
@@ -365,6 +365,28 @@ DICT_PLUS_SLURPY: {
 	);
 };
 
+DICT_PLUS_OPTIONAL: {
+	my $IntFromStr = declare IntFromStr => as Int;
+	coerce $IntFromStr, from Str, sub { length($_) };
+	$IntFromStr->coercion->freeze;
+
+	my $Dict1 = Dict[ a => $IntFromStr, b => Optional[Int], c => Optional[Int] ];
+	ok(
+		$Dict1->has_coercion && !$Dict1->coercion->can_be_inlined,
+		"$Dict1 has a non-inlinable coercion",
+	);
+	is_deeply(
+		$Dict1->coerce({ a => "Hello", b => 1, c => 2 }),
+		{ a => 5, b => 1, c => 2 },
+		"Coercion (A) to $Dict1",
+	);
+	is_deeply(
+		$Dict1->coerce({ a => "Hello", b => 1 }),
+		{ a => 5, b => 1 },
+		"Coercion (B) to $Dict1",
+	);
+};
+
 TUPLE: {
 	my $IntFromStr = declare IntFromStr => as Int;
 	coerce $IntFromStr, from Str, q{ length($_) };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libtype-tiny-perl.git



More information about the Pkg-perl-cvs-commits mailing list