X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F010_misc_type_tests.t;h=7b860469dc5b3ea07c788a549a877d08f35d5548;hb=43b9a91cacfa5e209171acd1141e3ad8e8a2d31a;hp=8e7a1cfffb53a566a22e00a0cf76e3fce4a3345d;hpb=e59a5c292a333cac504b65ebd4bba20b5e98d796;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/010_misc_type_tests.t b/t/040_type_constraints/010_misc_type_tests.t index 8e7a1cf..7b86046 100644 --- a/t/040_type_constraints/010_misc_type_tests.t +++ b/t/040_type_constraints/010_misc_type_tests.t @@ -3,11 +3,12 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More; use Test::Exception; +use Scalar::Util qw(refaddr); BEGIN { - use_ok('Moose::Util::TypeConstraints'); + use_ok('Moose::Util::TypeConstraints'); } # subtype 'aliasing' ... @@ -17,4 +18,72 @@ lives_ok { } '... create bare subtype fine'; my $numb3rs = find_type_constraint('Numb3rs'); -isa_ok($numb3rs, 'Moose::Meta::TypeConstraint'); \ No newline at end of file +isa_ok($numb3rs, 'Moose::Meta::TypeConstraint'); + +# subtype with unions + +{ + package Test::Moose::Meta::TypeConstraint::Union; + + use overload '""' => sub {'Broken|Test'}, fallback => 1; + use Moose; + + extends 'Moose::Meta::TypeConstraint'; +} + +my $dummy_instance = Test::Moose::Meta::TypeConstraint::Union->new; + +ok $dummy_instance => "Created Instance"; + +isa_ok $dummy_instance, + 'Test::Moose::Meta::TypeConstraint::Union' => 'isa correct type'; + +is "$dummy_instance", "Broken|Test" => + 'Got expected stringification result'; + +my $subtype1 = subtype 'New1' => as $dummy_instance; + +ok $subtype1 => 'made a subtype from our type object'; + +my $subtype2 = subtype 'New2' => as $subtype1; + +ok $subtype2 => 'made a subtype of our subtype'; + +# assert_valid + +{ + my $type = find_type_constraint('Num'); + + my $ok_1 = eval { $type->assert_valid(1); }; + ok($ok_1, "we can assert_valid that 1 is of type $type"); + + my $ok_2 = eval { $type->assert_valid('foo'); }; + my $error = $@; + ok(! $ok_2, "'foo' is not of type $type"); + like( + $error, + qr{validation failed for .\Q$type\E.}i, + "correct error thrown" + ); +} + +{ + for my $t (qw(Bar Foo)) { + my $tc = Moose::Meta::TypeConstraint->new({ + name => $t, + }); + + Moose::Util::TypeConstraints::register_type_constraint($tc); + } + + my $foo = Moose::Util::TypeConstraints::find_type_constraint('Foo'); + my $bar = Moose::Util::TypeConstraints::find_type_constraint('Bar'); + + ok(!$foo->equals($bar), "Foo type is not equal to Bar type"); + ok( $foo->equals($foo), "Foo equals Foo"); + ok( 0+$foo == refaddr($foo), "overloading works"); +} + +ok $subtype1, "type constraint boolean overload works"; + +done_testing;