6 use Test::More tests => 32;
10 use_ok("Moose::Util::TypeConstraints");
14 subtype 'MySpecialHash' => as 'HashRef[Int]';
15 } '... created the subtype special okay';
18 my $t = find_type_constraint('MySpecialHash');
19 isa_ok($t, 'Moose::Meta::TypeConstraint');
21 is($t->name, 'MySpecialHash', '... name is correct');
24 isa_ok($p, 'Moose::Meta::TypeConstraint::Parameterized');
25 isa_ok($p, 'Moose::Meta::TypeConstraint');
27 is($p->name, 'HashRef[Int]', '... parent name is correct');
29 ok($t->check({ one => 1, two => 2 }), '... validated {one=>1, two=>2} correctly');
30 ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly');
32 ok( $t->equals($t), "equals to self" );
33 ok( !$t->equals( $t->parent ), "not equal to parent" );
34 ok( $t->parent->equals( $t->parent ), "parent equals to self" );
36 ok( !$t->is_a_type_of("ThisTypeDoesNotExist"), "not a non existant type" );
37 ok( !$t->is_subtype_of("ThisTypeDoesNotExist"), "not a subtype of a non existant type" );
41 subtype 'MySpecialHashExtended'
44 # all values are less then 10
45 (scalar grep { $_ < 10 } values %{$_}) ? 1 : undef
47 } '... created the subtype special okay';
50 my $t = find_type_constraint('MySpecialHashExtended');
51 isa_ok($t, 'Moose::Meta::TypeConstraint');
53 is($t->name, 'MySpecialHashExtended', '... name is correct');
56 isa_ok($p, 'Moose::Meta::TypeConstraint::Parameterized');
57 isa_ok($p, 'Moose::Meta::TypeConstraint');
59 is($p->name, 'HashRef[Int]', '... parent name is correct');
61 ok($t->check({ one => 1, two => 2 }), '... validated it correctly');
62 ok(!$t->check({ zero => 10, one => 11, two => 12 }), '... validated { zero => 10, one => 11, two => 12 } correctly');
63 ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly');
67 subtype 'MyNonSpecialHash'
69 => where { keys %$_ == 3 };
73 my $t = find_type_constraint('MyNonSpecialHash');
75 isa_ok($t, 'Moose::Meta::TypeConstraint');
76 isa_ok($t, 'Moose::Meta::TypeConstraint::Parameterizable');
78 ok( $t->check({ one => 1, two => "foo", three => [] }), "validated" );
79 ok( !$t->check({ one => 1 }), "failed" );
83 my $t = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MyNonSpecialHash[Int]');
85 isa_ok($t, 'Moose::Meta::TypeConstraint');
87 ok( $t->check({ one => 1, two => 2, three => 3 }), "validated" );
88 ok( !$t->check({ one => 1, two => "foo", three => [] }), "failed" );
89 ok( !$t->check({ one => 1 }), "failed" );