Add explicit check for odd number of args to new and give a useful warning
[gitmo/Moose.git] / t / 040_type_constraints / 001_util_type_constraints.t
index 9b463e9..c7c611e 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 73;
+use Test::More;
 use Test::Exception;
 
 use Scalar::Util ();
@@ -12,19 +12,19 @@ use Moose::Util::TypeConstraints;
 
 
 type Number => where { Scalar::Util::looks_like_number($_) };
-type String 
+type String
     => where { !ref($_) && !Number($_) }
     => message { "This is not a string ($_)" };
 
-subtype Natural 
-        => as Number 
+subtype Natural
+        => as Number
         => where { $_ > 0 };
 
-subtype NaturalLessThanTen 
+subtype NaturalLessThanTen
         => as Natural
         => where { $_ < 10 }
         => message { "The number '$_' is not less than 10" };
-        
+
 Moose::Util::TypeConstraints->export_type_constraints_as_functions();
 
 ok(Number(5), '... this is a Num');
@@ -45,9 +45,9 @@ ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
-        
-# anon sub-typing       
-        
+
+# anon sub-typing
+
 my $negative = subtype Number => where  { $_ < 0 };
 ok(defined $negative, '... got a value back from negative');
 isa_ok($negative, 'Moose::Meta::TypeConstraint');
@@ -72,7 +72,7 @@ ok($negative2->is_subtype_of('Number'), '... $negative2 is a subtype of Number')
 ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String');
 
 ok($negative2->has_message, '... it has a message');
-is($negative2->validate(2), 
+is($negative2->validate(2),
    '2 is not a negative number',
    '... validated unsuccessfully (got error)');
 
@@ -89,8 +89,8 @@ ok($natural_less_than_ten->has_message, '... it has a message');
 
 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
 
-is($natural_less_than_ten->validate(15), 
-   "The number '15' is not less than 10", 
+is($natural_less_than_ten->validate(15),
+   "The number '15' is not less than 10",
    '... validated unsuccessfully (got error)');
 
 my $natural = find_type_constraint('Natural');
@@ -103,8 +103,8 @@ ok(!$natural->has_message, '... it does not have a message');
 
 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
 
-is($natural->validate(-5), 
-  "Validation failed for 'Natural' failed with value -5", 
+is($natural->validate(-5),
+  "Validation failed for 'Natural' with value -5",
   '... validated unsuccessfully (got error)');
 
 my $string = find_type_constraint('String');
@@ -114,8 +114,8 @@ ok($string->has_message, '... it does have a message');
 
 ok(!defined($string->validate("Five")), '... validated successfully (no error)');
 
-is($string->validate(5), 
-"This is not a string (5)", 
+is($string->validate(5),
+"This is not a string (5)",
 '... validated unsuccessfully (got error)');
 
 lives_ok { Moose::Meta::Attribute->new('bob', isa => 'Spong') }
@@ -148,7 +148,7 @@ throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type c
 }
 
 {
-    my $subtype = subtype 'ArrayRef[Num|Str]';
+    my $subtype = subtype as 'ArrayRef[Num|Str]';
     isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got an anon subtype' );
     is( $subtype->parent->name, 'ArrayRef[Num|Str]', 'parent is ArrayRef[Num|Str]' );
     ok( ! $subtype->has_message, 'subtype has no message' );
@@ -161,10 +161,44 @@ throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type c
     ok( $subtype->has_message, 'subtype does have a message' );
 }
 
+# alternative sugar-less calling style which is documented as legit:
+{
+    my $subtype = subtype( 'MyStr', { as => 'Str' } );
+    isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got a subtype' );
+    is( $subtype->name, 'MyStr', 'name is MyStr' );
+    is( $subtype->parent->name, 'Str', 'parent is Str' );
+}
+
+{
+    my $subtype = subtype( { as => 'Str' } );
+    isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got a subtype' );
+    is( $subtype->name, '__ANON__', 'name is __ANON__' );
+    is( $subtype->parent->name, 'Str', 'parent is Str' );
+}
+
+{
+    my $subtype = subtype( { as => 'Str', where => sub { /X/ } } );
+    isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got a subtype' );
+    is( $subtype->name, '__ANON__', 'name is __ANON__' );
+    is( $subtype->parent->name, 'Str', 'parent is Str' );
+    ok( $subtype->check('FooX'), 'constraint accepts FooX' );
+    ok( ! $subtype->check('Foo'), 'constraint reject Foo' );
+}
+
+{
+    throws_ok { subtype 'Foo' } qr/cannot consist solely of a name/,
+        'Cannot call subtype with a single string argument';
+}
+
 # Back-compat for being called without sugar. Previously, calling with
 # sugar was indistinguishable from calling directly.
 
 {
+    no warnings 'redefine';
+    *Moose::Deprecated::deprecated = sub { return };
+}
+
+{
     my $type = type( 'Number2', sub { Scalar::Util::looks_like_number($_) } );
 
     ok( $type->check(5), '... this is a Num' );
@@ -196,3 +230,4 @@ throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type c
     ok( ! $subtype->check('Foo'), '... this is not a Natural');
 }
 
+done_testing;