X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F016_subtyping_parameterized_types.t;fp=t%2F040_type_constraints%2F016_subtyping_parameterized_types.t;h=8bd6d9ba24fa1027c4d867bdbe8fd4e09e147849;hb=e15a70e5756560b2d25f0f377b1a255315b6e8d3;hp=0000000000000000000000000000000000000000;hpb=be06d3b055e9040a1d009987c83c601a6b5823fc;p=gitmo%2FMouse.git diff --git a/t/040_type_constraints/016_subtyping_parameterized_types.t b/t/040_type_constraints/016_subtyping_parameterized_types.t new file mode 100644 index 0000000..8bd6d9b --- /dev/null +++ b/t/040_type_constraints/016_subtyping_parameterized_types.t @@ -0,0 +1,136 @@ +#!/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; +use Test::Exception; + +BEGIN { + use_ok("Mouse::Util::TypeConstraints"); +} + +lives_ok { + subtype 'MySpecialHash' => as 'HashRef[Int]'; +} '... created the subtype special okay'; + +{ + my $t = find_type_constraint('MySpecialHash'); + isa_ok($t, 'Mouse::Meta::TypeConstraint'); + + is($t->name, 'MySpecialHash', '... name is correct'); + + my $p = $t->parent; + isa_ok($p, 'Mouse::Meta::TypeConstraint'); + isa_ok($p, 'Mouse::Meta::TypeConstraint'); + + is($p->name, 'HashRef[Int]', '... parent name is correct'); + + 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' + => as 'HashRef[Int]' + => where { + # all values are less then 10 + (scalar grep { $_ < 10 } values %{$_}) ? 1 : undef + }; +} '... created the subtype special okay'; + +{ + my $t = find_type_constraint('MySpecialHashExtended'); + isa_ok($t, 'Mouse::Meta::TypeConstraint'); + + is($t->name, 'MySpecialHashExtended', '... name is correct'); + + my $p = $t->parent; + isa_ok($p, 'Mouse::Meta::TypeConstraint'); + isa_ok($p, 'Mouse::Meta::TypeConstraint'); + + 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 { zero => 10, one => 11, two => 12 } correctly') or diag $t->dump; + ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly'); +} + +lives_ok { + subtype 'MyNonSpecialHash' + => as "HashRef" + => where { keys %$_ == 3 }; +}; + +{ + my $t = find_type_constraint('MyNonSpecialHash'); + + isa_ok($t, 'Mouse::Meta::TypeConstraint'); + isa_ok($t, 'Mouse::Meta::TypeConstraint'); + + ok( $t->check({ one => 1, two => "foo", three => [] }), "validated" ); + ok( !$t->check({ one => 1 }), "failed" ); +} + +{ + my $t = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('MyNonSpecialHash[Int]'); + + isa_ok($t, 'Mouse::Meta::TypeConstraint'); + + ok( $t->check({ one => 1, two => 2, three => 3 }), "validated" ); + ok( !$t->check({ one => 1, two => "foo", three => [] }), "failed" ) + or diag $t->dump; + ok( !$t->check({ one => 1 }), "failed" ); +} + +{ + ## Because to throw errors in M:M:Parameterizable needs Mouse loaded in + ## order to throw errors. In theory the use Mouse 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 Mouse somewhere when you + ## are creating type constraints. + use Mouse (); + + 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'; +} + +{ + last; # ScalarRef[] is not supported + 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;