fix for prototype undecl issue when type constraint utils loaded before consumers...
[gitmo/Moose.git] / t / 054_util_type_coercion.t
index 5b9b62e..c483323 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 6;
+use Test::More tests => 14;
 use Test::Exception;
 
 BEGIN {
@@ -12,8 +12,6 @@ BEGIN {
 
 {
     package HTTPHeader;
-    use strict;
-    use warnings;
     use Moose;
     
     has 'array' => (is => 'ro');
@@ -25,12 +23,27 @@ subtype Header =>
     => where { $_->isa('HTTPHeader') };
     
 coerce Header 
-    => as ArrayRef 
-        => to { HTTPHeader->new(array => $_[0]) }
-    => as HashRef 
-        => to { HTTPHeader->new(hash => $_[0]) };
+    => from ArrayRef 
+        => via { HTTPHeader->new(array => $_[0]) }
+    => from HashRef 
+        => via { HTTPHeader->new(hash => $_[0]) };
+
+
+{
+       package Math::BigFloat;
+       sub new { bless { }, shift }; # not a moose class ;-)
+}
+
+subtype "Math::BigFloat"
+       => as "Math::BigFloat"
+       => where { 1 };
+
+coerce "Math::BigFloat"
+       => from Num
+               => via { Math::BigFloat->new( $_ ) };
+
         
-Moose::Util::TypeConstraints::export_type_contstraints_as_functions();        
+Moose::Util::TypeConstraints->export_type_contstraints_as_functions();        
         
 my $header = HTTPHeader->new();
 isa_ok($header, 'HTTPHeader');
@@ -39,27 +52,39 @@ ok(Header($header), '... this passed the type test');
 ok(!Header([]), '... this did not pass the type test');
 ok(!Header({}), '... this did not pass the type test');
 
-my $coercion = Moose::Util::TypeConstraints::find_type_coercion('Header');
-is(ref($coercion), 'CODE', '... got the right type of coercion');
-
-#{
-#    my $coerced = $coercion->([ 1, 2, 3 ]);
-#    isa_ok($coerced, 'HTTPHeader');
-#
-#    is_deeply(
-#        $coerced->array(),
-#        [ 1, 2, 3 ],
-#        '... got the right array');
-#    is($coerced->hash(), undef, '... nothing assigned to the hash');        
-#}
-#
-#{
-#    my $coerced = $coercion->({ one => 1, two => 2, three => 3 });
-#    isa_ok($coerced, 'HTTPHeader');
-#    
-#    is_deeply(
-#        $coerced->hash(),
-#        { one => 1, two => 2, three => 3 },
-#        '... got the right hash');
-#    is($coerced->array(), undef, '... nothing assigned to the array');        
-#}
+my $coercion = find_type_constraint('Header')->coercion;
+isa_ok($coercion, 'Moose::Meta::TypeCoercion');
+
+{
+    my $coerced = $coercion->coerce([ 1, 2, 3 ]);
+    isa_ok($coerced, 'HTTPHeader');
+
+    is_deeply(
+        $coerced->array(),
+        [ 1, 2, 3 ],
+        '... got the right array');
+    is($coerced->hash(), undef, '... nothing assigned to the hash');        
+}
+
+{
+    my $coerced = $coercion->coerce({ one => 1, two => 2, three => 3 });
+    isa_ok($coerced, 'HTTPHeader');
+    
+    is_deeply(
+        $coerced->hash(),
+        { one => 1, two => 2, three => 3 },
+        '... got the right hash');
+    is($coerced->array(), undef, '... nothing assigned to the array');        
+}
+
+{
+    my $scalar_ref = \(my $var);
+    my $coerced = $coercion->coerce($scalar_ref);
+    is($coerced, $scalar_ref, '... got back what we put in');
+}
+
+{
+    my $coerced = $coercion->coerce("Foo");
+    is($coerced, "Foo", '... got back what we put in');
+}
+