X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F016_subtyping_parameterized_types.t;h=3c6971fa6f86d1801e21572746bb37f2a583296d;hb=a6d3794f353dd356cc916044ae525ce6e23dfc99;hp=221cf6adbd4d9ff4426f3b263a99be2c02bc1344;hpb=36dbd1056e16ddb57ba6e461d3f5548c8c8863cb;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/016_subtyping_parameterized_types.t b/t/040_type_constraints/016_subtyping_parameterized_types.t index 221cf6a..3c6971f 100644 --- a/t/040_type_constraints/016_subtyping_parameterized_types.t +++ b/t/040_type_constraints/016_subtyping_parameterized_types.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 30; +use Test::More; use Test::Exception; BEGIN { @@ -26,16 +26,19 @@ lives_ok { is($p->name, 'HashRef[Int]', '... parent name is correct'); - ok($t->check({ one => 1, two => 2 }), '... validated it correctly'); + ok($t->check({ one => 1, two => 2 }), '... validated {one=>1, two=>2} correctly'); ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly'); ok( $t->equals($t), "equals to self" ); ok( !$t->equals( $t->parent ), "not equal to parent" ); ok( $t->parent->equals( $t->parent ), "parent equals to self" ); + + ok( !$t->is_a_type_of("ThisTypeDoesNotExist"), "not a non existant type" ); + ok( !$t->is_subtype_of("ThisTypeDoesNotExist"), "not a subtype of a non existant type" ); } lives_ok { - subtype 'MySpecialHashExtended' + subtype 'MySpecialHashExtended' => as 'HashRef[Int]' => where { # all values are less then 10 @@ -56,7 +59,7 @@ lives_ok { is($p->name, 'HashRef[Int]', '... parent name is correct'); ok($t->check({ one => 1, two => 2 }), '... validated it correctly'); - ok(!$t->check({ zero => 10, one => 11, two => 12 }), '... validated it correctly'); + ok(!$t->check({ zero => 10, one => 11, two => 12 }), '... validated { zero => 10, one => 11, two => 12 } correctly'); ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly'); } @@ -85,3 +88,44 @@ lives_ok { ok( !$t->check({ one => 1, two => "foo", three => [] }), "failed" ); ok( !$t->check({ one => 1 }), "failed" ); } + +{ + ## Because to throw errors in M:M:Parameterizable needs Moose loaded in + ## order to throw errors. In theory the use Moose belongs to that class + ## but when I put it there causes all sorts or trouble. In theory this is + ## never a real problem since you are likely to use Moose somewhere when you + ## are creating type constraints. + use Moose (); + + my $MyArrayRefInt = subtype 'MyArrayRefInt', + as 'ArrayRef[Int]'; + + my $BiggerInt = subtype 'BiggerInt', + as 'Int', + where {$_>10}; + + my $SubOfMyArrayRef = subtype 'SubOfMyArrayRef', + as 'MyArrayRefInt[BiggerInt]'; + + ok $MyArrayRefInt->check([1,2,3]), '[1,2,3] is okay'; + ok ! $MyArrayRefInt->check(["a","b"]), '["a","b"] is not'; + ok $BiggerInt->check(100), '100 is big enough'; + ok ! $BiggerInt->check(5), '5 is big enough'; + ok $SubOfMyArrayRef->check([15,20,25]), '[15,20,25] is a bunch of big ints'; + ok ! $SubOfMyArrayRef->check([15,5,25]), '[15,5,25] is NOT a bunch of big ints'; + + throws_ok sub { + my $SubOfMyArrayRef = subtype 'SubSubOfMyArrayRef', + as 'SubOfMyArrayRef[Str]'; + }, qr/Str is not a subtype of BiggerInt/, 'Failed to parameterize with a bad type parameter'; +} + +{ + my $RefToInt = subtype as 'ScalarRef[Int]'; + + ok $RefToInt->check(\1), '\1 is okay'; + ok !$RefToInt->check(1), '1 is not'; + ok !$RefToInt->check(\"foo"), '\"foo" is not'; +} + +done_testing;