From: Shawn M Moore Date: Mon, 9 Jun 2008 23:41:46 +0000 (+0000) Subject: Tests and implementation for Undef/Defined types X-Git-Tag: 0.04~90 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=f5fbe3cced480252b924d64511d69b325d4ddebd Tests and implementation for Undef/Defined types --- diff --git a/lib/Mouse/TypeRegistry.pm b/lib/Mouse/TypeRegistry.pm index 3e63cec..3a38eb3 100644 --- a/lib/Mouse/TypeRegistry.pm +++ b/lib/Mouse/TypeRegistry.pm @@ -10,8 +10,8 @@ sub optimized_constraints { Bool => sub { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' }, - Undef => sub { 1 }, - Defined => sub { 1 }, + Undef => sub { !defined($_) }, + Defined => sub { defined($_) }, Value => sub { 1 }, Num => sub { 1 }, Int => sub { 1 }, diff --git a/t/024-isa.t b/t/024-isa.t index d594814..c589614 100644 --- a/t/024-isa.t +++ b/t/024-isa.t @@ -9,7 +9,7 @@ my %values_for_type = ( valid => [ undef, \undef, - 1.0, + 1.3, "foo", \"foo", sub { die }, @@ -29,17 +29,45 @@ my %values_for_type = ( Bool => { valid => [undef, "", 1, 0, "1", "0"], - invalid => [1.5, "true", "false", "t", "f", ], + invalid => [ + \undef, + 1.5, + "true", + "false", + "t", + "f", + \"foo", + sub { die }, + qr/^1?$|^(11+?)\1+$/, + [], + {}, + \do { my $v = 1 }, + Test::Builder->new, + ], }, Undef => { - valid => [], - invalid => [], + valid => [undef], + invalid => [ + \undef, + 0, + '', + 1.5, + "undef", + \"undef", + sub { die }, + qr/^1?$|^(11+?)\1+$/, + [], + {}, + \do { my $v = undef }, + Test::Builder->new, + ], }, Defined => { - valid => [], - invalid => [], + # populated later with the values from Undef + #valid => [], + #invalid => [], }, Value => { @@ -114,6 +142,8 @@ my %values_for_type = ( ); $values_for_type{Item}{valid} = $values_for_type{Any}{valid}; +$values_for_type{Defined}{valid} = $values_for_type{Undef}{invalid}; +$values_for_type{Defined}{invalid} = $values_for_type{Undef}{valid}; my $plan = 0; $plan += 5 * @{ $values_for_type{$_}{valid} } for keys %values_for_type; @@ -152,16 +182,17 @@ for my $type (keys %values_for_type) { } for my $value (@{ $values_for_type{$type}{invalid} }) { + my $display = defined($value) ? $value : 'undef'; my $via_new; throws_ok { $via_new = Class->new($type => $value); - } qr/Attribute \($type\) does not pass the type constraint because: Validation failed for '$type' failed with value \Q$value\E/; + } qr/Attribute \($type\) does not pass the type constraint because: Validation failed for '$type' failed with value \Q$display\E/; is($via_new, undef, "no object created"); my $via_set = Class->new; throws_ok { $via_set->$type($value); - } qr/Attribute \($type\) does not pass the type constraint because: Validation failed for '$type' failed with value \Q$value\E/; + } qr/Attribute \($type\) does not pass the type constraint because: Validation failed for '$type' failed with value \Q$display\E/; is($via_set->$type, undef, "value for $type not set"); }