cleanup synopsis example and finished coercion fix
[gitmo/MooseX-Dependent.git] / t / 05-pod-examples.t
index 061dcec..e5ded22 100644 (file)
@@ -3,19 +3,18 @@ use warnings;
 
 use Test::More;
 
-eval "use Set::Scalar"; if($@) {
-    plan skip_all => 'Set::Scalar not installed';
-}
-
-
 {
     package Test::MooseX::Types::Parameterizable::Synopsis;
 
     use Moose;
     use MooseX::Types::Parameterizable qw(Parameterizable);
-    use MooseX::Types::Moose qw(Str Int);
+    use MooseX::Types::Moose qw(Str Int ArrayRef);
     use MooseX::Types -declare=>[qw(Varchar)];
 
+    ## Create a type constraint that is a string but parameterizes an integer
+    ## that is used as a maximum length constraint on that string, similar to
+    ## an SQL Varchar type.
+
     subtype Varchar,
       as Parameterizable[Str,Int],
       where {
@@ -24,6 +23,13 @@ eval "use Set::Scalar"; if($@) {
       },
       message { "'$_' is too long"  };
 
+    coerce Varchar,
+      from ArrayRef,
+      via { 
+        my ($arrayref, $int) = @_;
+        join('', @$arrayref);
+      };
+
     my $varchar_five = Varchar[5];
 
     Test::More::ok $varchar_five->check('four');
@@ -34,7 +40,7 @@ eval "use Set::Scalar"; if($@) {
     Test::More::ok $varchar_ten->check( 'X' x 9 );
     Test::More::ok ! $varchar_ten->check( 'X' x 12 );
 
-    has varchar_five => (isa=>Varchar[5], is=>'ro');
+    has varchar_five => (isa=>Varchar[5], is=>'ro', coerce=>1);
     has varchar_ten => (isa=>Varchar[10], is=>'ro');
   
     my $object1 = __PACKAGE__->new(
@@ -51,6 +57,14 @@ eval "use Set::Scalar"; if($@) {
 
     Test::More::ok $@, 'There was an error';
     Test::More::like $@, qr('12345678' is too long), 'Correct custom error';
+
+    my $object3 = __PACKAGE__->new(
+        varchar_five => [qw/aa bb/],
+        varchar_ten => '123456789',
+    );
+
+    Test::More::is $object3->varchar_five, 'aabb',
+      'coercion as expected';
 }
 
 done_testing;