[Moose-commits] r7827 - in MooseX-Types-Structured/trunk: . lib/MooseX/Meta/TypeConstraint t

jnapiorkowski at code2.0beta.co.uk jnapiorkowski at code2.0beta.co.uk
Fri Mar 6 17:02:42 GMT 2009


Author: jnapiorkowski
Date: 2009-03-06 09:02:42 -0800 (Fri, 06 Mar 2009)
New Revision: 7827

Added:
   MooseX-Types-Structured/trunk/t/12-error.t
Modified:
   MooseX-Types-Structured/trunk/Makefile.PL
   MooseX-Types-Structured/trunk/lib/MooseX/Meta/TypeConstraint/Structured.pm
   MooseX-Types-Structured/trunk/t/11-overflow.t
Log:
added some code to improve the error message and added test for that


Modified: MooseX-Types-Structured/trunk/Makefile.PL
===================================================================
--- MooseX-Types-Structured/trunk/Makefile.PL	2009-03-05 22:08:52 UTC (rev 7826)
+++ MooseX-Types-Structured/trunk/Makefile.PL	2009-03-06 17:02:42 UTC (rev 7827)
@@ -10,6 +10,7 @@
 ## Module dependencies
 requires 'Moose' => '0.63';
 requires 'MooseX::Types' => '0.08';
+requires 'Devel::PartialDump' => '0.07';
 
 ## Testing dependencies
 build_requires 'Test::More' => '0.70';

Modified: MooseX-Types-Structured/trunk/lib/MooseX/Meta/TypeConstraint/Structured.pm
===================================================================
--- MooseX-Types-Structured/trunk/lib/MooseX/Meta/TypeConstraint/Structured.pm	2009-03-05 22:08:52 UTC (rev 7826)
+++ MooseX-Types-Structured/trunk/lib/MooseX/Meta/TypeConstraint/Structured.pm	2009-03-06 17:02:42 UTC (rev 7827)
@@ -2,6 +2,7 @@
  MooseX::Meta::TypeConstraint::Structured;
 
 use Moose;
+use Devel::PartialDump;
 use Moose::Util::TypeConstraints ();
 use MooseX::Meta::TypeCoercion::Structured;
 extends 'Moose::Meta::TypeConstraint';
@@ -215,8 +216,20 @@
 
 =head2 get_message
 
-May want to override this to set a more useful error message
+Give you a better peek into what's causing the error.  For now we stringify the
+incoming deep value with L<Devel::PartialDump> and pass that on to either your
+custom error message or the default one.  In the future we'll try to provide a
+more complete stack trace of the actual offending elements
 
+=cut
+
+around 'get_message' => sub {
+    my ($get_message, $self, $value) = @_;
+    my $new_value = Devel::PartialDump::dump($value);
+    return $self->$get_message($new_value);
+    
+};
+
 =head1 SEE ALSO
 
 The following modules or resources may be of interest.

Modified: MooseX-Types-Structured/trunk/t/11-overflow.t
===================================================================
--- MooseX-Types-Structured/trunk/t/11-overflow.t	2009-03-05 22:08:52 UTC (rev 7826)
+++ MooseX-Types-Structured/trunk/t/11-overflow.t	2009-03-06 17:02:42 UTC (rev 7827)
@@ -72,4 +72,3 @@
 ok !$array_tailed_dict->check([]), 'correct fail';
 ok $array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1,2}), 'correct pass with tail';
 ok !$array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1, "hello"}), 'correct fail with tail';
-

Added: MooseX-Types-Structured/trunk/t/12-error.t
===================================================================
--- MooseX-Types-Structured/trunk/t/12-error.t	                        (rev 0)
+++ MooseX-Types-Structured/trunk/t/12-error.t	2009-03-06 17:02:42 UTC (rev 7827)
@@ -0,0 +1,20 @@
+BEGIN {
+	use strict;
+	use warnings;
+	use Test::More tests=>4;
+}
+
+use Moose::Util::TypeConstraints;
+use MooseX::Types::Structured qw(Dict Tuple);
+use MooseX::Types::Moose qw(Int Str ArrayRef HashRef);
+
+# Create some TCs from which errors will be generated
+my $simple_tuple = subtype 'simple_tuple', as Tuple[Int,Str];
+my $simple_dict = subtype 'simple_dict', as Dict[name=>Str,age=>Int];
+
+# We probably need more stuff here...
+ok $simple_tuple->check([1,'hello']), "simple_tuple validates: 1,'hello'";
+ok !$simple_tuple->check(['hello',1]), "simple_tuple fails: 'hello',1";
+like $simple_tuple->validate(['hello',1]), qr/"hello", 1/, 'got expected valiate message';
+like $simple_dict->validate(['hello',1]), qr/"hello", 1/, 'got expected valiate message';
+




More information about the Moose-commits mailing list