Fixed some bugs and added tests
phaylon [Thu, 12 Apr 2007 21:16:34 +0000 (21:16 +0000)]
lib/MooseX/TypeLibrary/Wrapper.pm
t/12_wrapper-definition.t
t/lib/TestWrapper.pm

index dfaa1d9..ca75c16 100644 (file)
@@ -24,10 +24,10 @@ sub import {
         require Class::Inspector->filename($library_class)
             unless Class::Inspector->loaded($library_class);
 
-        $library_class->import( 
-            @{ $libraries{ $l } }, 
-            { -into => scalar(caller) } 
-        );
+        $library_class->import( @{ $libraries{ $l } }, { 
+            -into    => scalar(caller),
+            -wrapper => $class,
+        });
     }
     return 1;
 }
index 2735246..f2d0f77 100644 (file)
@@ -5,24 +5,29 @@ use strict;
 use Test::More;
 use FindBin;
 use lib "$FindBin::Bin/lib";
+use Moose::Util::TypeConstraints;
+BEGIN { coerce 'Str', from 'Int', via { "$_" } }
 use TestWrapper TestLibrary => [qw( NonEmptyStr IntArrayRef )],
                 Moose       => [qw( Str Int )];
 
+
 my @tests = (
-    [ 'NonEmptyStr', 12, "12", [], "foobar", "" ],
-    [ 'IntArrayRef', 12, [12], {}, [17, 23], {} ],
+    [ 'NonEmptyStr', 'TestLibrary::NonEmptyStr', 12, "12", [], "foobar", "" ],
+    [ 'IntArrayRef', 'TestLibrary::IntArrayRef', 12, [12], {}, [17, 23], {} ],
+    [ 'Str',         'Str',                      12, "12", [], "foo", [777] ],
 );
 
-plan tests => (@tests * 8);
+plan tests => (@tests * 9);
 
 # new array ref so we can safely shift from it
 for my $data (map { [@$_] } @tests) {
     my $type = shift @$data;
+    my $full = shift @$data;
 
     # Type name export
     {
         ok my $code = __PACKAGE__->can($type), "$type() was exported";
-        is $code->(), "TestLibrary::$type", "$type() returned correct type name";
+        is $code->(), $full, "$type() returned correct type name";
     }
 
     # coercion handler export
@@ -30,7 +35,8 @@ for my $data (map { [@$_] } @tests) {
         my ($coerce, $coercion_result, $cannot_coerce) = map { shift @$data } 1 .. 3;
         ok my $code = __PACKAGE__->can("to_$type"), "to_$type() coercion was exported";
         is_deeply scalar $code->($coerce), $coercion_result, "to_$type() coercion works";
-        ok ! $code->($cannot_coerce), "to_$type() returns false on invalid value";
+        eval { $code->($cannot_coerce) };
+        is $@, "coercion returned undef\n", "to_$type() died on invalid value";
     }
 
     # type test handler
@@ -39,6 +45,7 @@ for my $data (map { [@$_] } @tests) {
         ok my $code = __PACKAGE__->can("is_$type"), "is_$type() check was exported";
         ok $code->($valid), "is_$type() check true on valid value";
         ok ! $code->($invalid), "is_$type() check false on invalid value";
+        is ref($code->()), 'CODE', "is_$type() returns test closure without args";
     }
 }
 
index 7a142a4..417da25 100644 (file)
@@ -5,7 +5,7 @@ extends 'MooseX::TypeLibrary::Wrapper';
 #use Class::C3;
 #use base 'MooseX::TypeLibrary::Wrapper';
 
-around type_export_generator => sub {
+override type_export_generator => sub {
     my $code = super();
     return sub { $code->(@_) };
 };
@@ -17,7 +17,7 @@ around type_export_generator => sub {
 #    return sub { $code->(@_) };
 #}
 
-around check_export_generator => sub {
+override check_export_generator => sub {
     my $code = super();
     return sub {
         return $code unless @_;
@@ -35,7 +35,7 @@ around check_export_generator => sub {
 #    };
 #}
 
-around coercion_export_generator => sub {
+override coercion_export_generator => sub {
     my $code = super();
     return sub {
         my $value = $code->(@_);