Avoid duplication in running the same tests with and without immutable
[gitmo/Moose.git] / t / 040_type_constraints / 001_util_type_constraints.t
index b09ada8..b57a4e5 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 52;
+use Test::More tests => 85;
 use Test::Exception;
 
 use Scalar::Util ();
@@ -134,3 +134,94 @@ throws_ok {$r->add_type_constraint()} qr/not a valid type constraint/, '->add_ty
 throws_ok {$r->add_type_constraint('foo')} qr/not a valid type constraint/, '->add_type_constraint("foo") throws';
 throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws';
 
+# Test some specific things that in the past did not work,
+# specifically weird variations on anon subtypes.
+
+{
+    my $subtype = subtype as 'Str';
+    isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got an anon subtype' );
+    is( $subtype->parent->name, 'Str', 'parent is Str' );
+    # This test sucks but is the best we can do
+    is( $subtype->constraint->(), 1,
+        'subtype has the null constraint' );
+    ok( ! $subtype->has_message, 'subtype has no message' );
+}
+
+{
+    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' );
+}
+
+{
+    my $subtype = subtype 'ArrayRef[Num|Str]' => message { 'foo' };
+    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 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.
+
+{
+    my $type = type( 'Number2', sub { Scalar::Util::looks_like_number($_) } );
+
+    ok( $type->check(5), '... this is a Num' );
+    ok( ! $type->check('Foo'), '... this is not a Num' );
+}
+
+{
+    # anon subtype
+    my $subtype = subtype( 'Number2', sub { $_ > 0 } );
+
+    ok( $subtype->check(5), '... this is a Natural');
+    ok( ! $subtype->check(-5), '... this is not a Natural');
+    ok( ! $subtype->check('Foo'), '... this is not a Natural');
+}
+
+{
+    my $subtype = subtype( 'Natural2', 'Number2', sub { $_ > 0 } );
+
+    ok( $subtype->check(5), '... this is a Natural');
+    ok( ! $subtype->check(-5), '... this is not a Natural');
+    ok( ! $subtype->check('Foo'), '... this is not a Natural');
+}
+
+{
+    my $subtype = subtype( 'Natural3', 'Number2' );
+
+    ok( $subtype->check(5), '... this is a Natural');
+    ok( $subtype->check(-5), '... this is a Natural');
+    ok( ! $subtype->check('Foo'), '... this is not a Natural');
+}
+