10 use_ok("Moose::Util::TypeConstraints");
11 use_ok('Moose::Meta::TypeConstraint::Parameterized');
15 subtype 'AlphaKeyHash' => as 'HashRef'
17 # no keys match non-alpha
18 (grep { /[^a-zA-Z]/ } keys %$_) == 0
20 }, undef, '... created the subtype special okay' );
23 subtype 'Trihash' => as 'AlphaKeyHash'
27 }, undef, '... created the subtype special okay' );
30 subtype 'Noncon' => as 'Item';
31 }, undef, '... created the subtype special okay' );
34 my $t = find_type_constraint('AlphaKeyHash');
35 isa_ok($t, 'Moose::Meta::TypeConstraint');
37 is($t->name, 'AlphaKeyHash', '... name is correct');
40 isa_ok($p, 'Moose::Meta::TypeConstraint');
42 is($p->name, 'HashRef', '... parent name is correct');
44 ok($t->check({ one => 1, two => 2 }), '... validated it correctly');
45 ok(!$t->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
47 ok( $t->equals($t), "equals to self" );
48 ok( !$t->equals($t->parent), "not equal to parent" );
51 my $hoi = Moose::Util::TypeConstraints::find_or_parse_type_constraint('AlphaKeyHash[Int]');
53 ok($hoi->check({ one => 1, two => 2 }), '... validated it correctly');
54 ok(!$hoi->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
55 ok(!$hoi->check({ one => 'uno', two => 'dos' }), '... validated it correctly');
56 ok(!$hoi->check({ one1 => 'un', two2 => 'deux' }), '... validated it correctly');
58 ok( $hoi->equals($hoi), "equals to self" );
59 ok( !$hoi->equals($hoi->parent), "equals to self" );
60 ok( !$hoi->equals(find_type_constraint('AlphaKeyHash')), "not equal to unparametrized self" );
61 ok( $hoi->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "Blah", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
62 ok( !$hoi->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "Oink", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Str") ) ), "not equal to different parameter" );
64 my $th = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Trihash[Bool]');
66 ok(!$th->check({ one => 1, two => 1 }), '... validated it correctly');
67 ok($th->check({ one => 1, two => 0, three => 1 }), '... validated it correctly');
68 ok(!$th->check({ one => 1, two => 2, three => 1 }), '... validated it correctly');
69 ok(!$th->check({foo1 => 1, bar2 => 0, baz3 => 1}), '... validated it correctly');
72 Moose::Meta::TypeConstraint::Parameterized->new(
74 parent => find_type_constraint('Str'),
75 type_parameter => find_type_constraint('Int'),
77 }, undef, 'non-containers cannot be parameterized' );
80 Moose::Meta::TypeConstraint::Parameterized->new(
81 name => 'Noncon[Int]',
82 parent => find_type_constraint('Noncon'),
83 type_parameter => find_type_constraint('Int'),
85 }, undef, 'non-containers cannot be parameterized' );