Moose does not like receiving an undefined message param when declaring a type constraint
Brian Phillips [Mon, 25 Jul 2011 16:35:38 +0000 (11:35 -0500)]
lib/MooseX/Meta/TypeConstraint/Parameterizable.pm
t/07-no-message.t [new file with mode: 0644]

index 5163922..44f2abc 100644 (file)
@@ -192,7 +192,7 @@ sub parameterize {
                     constraining_value => $args,
                     parent_type_constraint=>$self->parent_type_constraint,
                     constraining_value_type_constraint => $self->constraining_value_type_constraint,
-                    message => $self->message,
+                    ($self->has_message ? (message => $self->message) : ()),
                 );
             }
         }
diff --git a/t/07-no-message.t b/t/07-no-message.t
new file mode 100644 (file)
index 0000000..4cb4219
--- /dev/null
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+{
+    package Test::MyMooseClass;
+
+    use Moose;
+    use MooseX::Types::Parameterizable qw(Parameterizable);
+    use MooseX::Types::Moose qw(Str Int);
+    use MooseX::Types -declare=>[qw(Varchar)];
+
+    ## Minor change from docs to avoid additional test dependencies
+    subtype Varchar,
+      as Parameterizable[Str,Int],
+      where {
+        my($string, $int) = @_;
+        $int >= length($string) ? 1:0;
+      };
+
+       has short_string => ( is => 'rw', isa => Varchar[5] );
+}
+
+my $obj = Test::MyMooseClass->new(short_string => 'four');
+
+is $obj->short_string, 'four', 'attribute stored correctly';
+
+# this should die
+eval { $obj->short_string('longer') };
+
+like $@, qr/Attribute \(short_string\) does not pass the type constraint/, 'fails on longer string with correct error message';
+
+done_testing;