Cleanup failing tests
[gitmo/Mouse.git] / Moose-t-failing / 040_type_constraints / 030_class_subtypes.t
diff --git a/Moose-t-failing/040_type_constraints/030_class_subtypes.t b/Moose-t-failing/040_type_constraints/030_class_subtypes.t
new file mode 100644 (file)
index 0000000..594c16e
--- /dev/null
@@ -0,0 +1,147 @@
+#!/usr/bin/perl
+# This is automatically generated by author/import-moose-test.pl.
+# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
+use t::lib::MooseCompat;
+
+use strict;
+use warnings;
+
+use Test::More;
+$TODO = q{Mouse is not yet completed};
+use Test::Exception;
+
+use Mouse::Util::TypeConstraints;
+use Mouse::Meta::TypeConstraint;
+
+
+## Create a subclass with a custom method
+
+{
+    package Test::Mouse::Meta::TypeConstraint;
+    use Mouse;
+    extends 'Mouse::Meta::TypeConstraint';
+
+    sub my_custom_method {
+        return 1;
+    }
+}
+
+my $Int = find_type_constraint('Int');
+ok $Int, 'Got a good type constraint';
+
+my $parent  = Test::Mouse::Meta::TypeConstraint->new({
+        name => "Test::Mouse::Meta::TypeConstraint" ,
+        parent => $Int,
+});
+
+ok $parent, 'Created type constraint';
+ok $parent->check(1), 'Correctly passed';
+ok ! $parent->check('a'), 'correctly failed';
+ok $parent->my_custom_method, 'found the custom method';
+
+my $subtype1 = subtype 'another_subtype' => as $parent;
+
+ok $subtype1, 'Created type constraint';
+ok $subtype1->check(1), 'Correctly passed';
+ok ! $subtype1->check('a'), 'correctly failed';
+ok $subtype1->my_custom_method, 'found the custom method';
+
+
+my $subtype2 = subtype 'another_subtype' => as $subtype1 => where { $_ < 10 };
+
+ok $subtype2, 'Created type constraint';
+ok $subtype2->check(1), 'Correctly passed';
+ok ! $subtype2->check('a'), 'correctly failed';
+ok ! $subtype2->check(100), 'correctly failed';
+
+ok $subtype2->my_custom_method, 'found the custom method';
+
+
+{
+    package Foo;
+
+    use Mouse;
+}
+
+{
+    package Bar;
+
+    use Mouse;
+
+    extends 'Foo';
+}
+
+{
+    package Baz;
+
+    use Mouse;
+}
+
+my $foo = class_type 'Foo';
+my $isa_foo = subtype 'IsaFoo' => as $foo;
+
+ok $isa_foo, 'Created subtype of Foo type';
+ok $isa_foo->check( Foo->new ), 'Foo passes check';
+ok $isa_foo->check( Bar->new ), 'Bar passes check';
+ok ! $isa_foo->check( Baz->new ), 'Baz does not pass check';
+like $foo->get_message( Baz->new ), qr/^Validation failed for 'Foo' with value Baz=HASH\(0x\w+\) \(not isa Foo\)/, 'Better validation message';
+
+# Maybe in the future this *should* inherit?
+like $isa_foo->get_message( Baz->new ), qr/^Validation failed for 'IsaFoo' with value Baz=HASH\(0x\w+\)$/, "Subtypes do not automatically inherit parent type's message";
+
+
+# Implicit types
+{
+    package Quux;
+
+    use Mouse;
+
+    has age => (
+        isa => 'Positive',
+        is => 'bare',
+    );
+}
+
+throws_ok {
+    Quux->new(age => 3)
+} qr/^Attribute \(age\) does not pass the type constraint because: Validation failed for 'Positive' with value 3 \(not isa Positive\)/;
+
+lives_ok {
+    Quux->new(age => (bless {}, 'Positive'));
+};
+
+eval "
+    package Positive;
+    use Mouse;
+";
+
+throws_ok {
+    Quux->new(age => 3)
+} qr/^Attribute \(age\) does not pass the type constraint because: Validation failed for 'Positive' with value 3 \(not isa Positive\)/;
+
+lives_ok {
+    Quux->new(age => Positive->new)
+};
+
+class_type 'Negative' => message { "$_ is not a Negative Nancy" };
+
+{
+    package Quux::Ier;
+
+    use Mouse;
+
+    has age => (
+        isa => 'Negative',
+        is => 'bare',
+    );
+}
+
+throws_ok {
+    Quux::Ier->new(age => 3)
+} qr/^Attribute \(age\) does not pass the type constraint because: 3 is not a Negative Nancy /;
+
+lives_ok {
+    Quux::Ier->new(age => (bless {}, 'Negative'))
+};
+
+done_testing;