We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / type_constraints / custom_parameterized_types.t
CommitLineData
39aba5c9 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
39aba5c9 8
28fdde7f 9use Moose::Util::TypeConstraints;
10use Moose::Meta::TypeConstraint::Parameterized;
39aba5c9 11
b10dde3a 12is( exception {
39aba5c9 13 subtype 'AlphaKeyHash' => as 'HashRef'
14 => where {
15 # no keys match non-alpha
16 (grep { /[^a-zA-Z]/ } keys %$_) == 0
17 };
b10dde3a 18}, undef, '... created the subtype special okay' );
39aba5c9 19
b10dde3a 20is( exception {
39aba5c9 21 subtype 'Trihash' => as 'AlphaKeyHash'
22 => where {
23 keys(%$_) == 3
24 };
b10dde3a 25}, undef, '... created the subtype special okay' );
39aba5c9 26
b10dde3a 27is( exception {
39aba5c9 28 subtype 'Noncon' => as 'Item';
b10dde3a 29}, undef, '... created the subtype special okay' );
39aba5c9 30
31{
32 my $t = find_type_constraint('AlphaKeyHash');
33 isa_ok($t, 'Moose::Meta::TypeConstraint');
34
35 is($t->name, 'AlphaKeyHash', '... name is correct');
36
37 my $p = $t->parent;
38 isa_ok($p, 'Moose::Meta::TypeConstraint');
39
40 is($p->name, 'HashRef', '... parent name is correct');
41
42 ok($t->check({ one => 1, two => 2 }), '... validated it correctly');
43 ok(!$t->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
dabed765 44
45 ok( $t->equals($t), "equals to self" );
46 ok( !$t->equals($t->parent), "not equal to parent" );
39aba5c9 47}
48
620db045 49my $hoi = Moose::Util::TypeConstraints::find_or_parse_type_constraint('AlphaKeyHash[Int]');
39aba5c9 50
51ok($hoi->check({ one => 1, two => 2 }), '... validated it correctly');
52ok(!$hoi->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
53ok(!$hoi->check({ one => 'uno', two => 'dos' }), '... validated it correctly');
54ok(!$hoi->check({ one1 => 'un', two2 => 'deux' }), '... validated it correctly');
55
dabed765 56ok( $hoi->equals($hoi), "equals to self" );
57ok( !$hoi->equals($hoi->parent), "equals to self" );
58ok( !$hoi->equals(find_type_constraint('AlphaKeyHash')), "not equal to unparametrized self" );
59ok( $hoi->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "Blah", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" );
60ok( !$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" );
61
620db045 62my $th = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Trihash[Bool]');
39aba5c9 63
64ok(!$th->check({ one => 1, two => 1 }), '... validated it correctly');
65ok($th->check({ one => 1, two => 0, three => 1 }), '... validated it correctly');
66ok(!$th->check({ one => 1, two => 2, three => 1 }), '... validated it correctly');
67ok(!$th->check({foo1 => 1, bar2 => 0, baz3 => 1}), '... validated it correctly');
68
b10dde3a 69isnt( exception {
39aba5c9 70 Moose::Meta::TypeConstraint::Parameterized->new(
71 name => 'Str[Int]',
72 parent => find_type_constraint('Str'),
73 type_parameter => find_type_constraint('Int'),
74 );
b10dde3a 75}, undef, 'non-containers cannot be parameterized' );
39aba5c9 76
b10dde3a 77isnt( exception {
39aba5c9 78 Moose::Meta::TypeConstraint::Parameterized->new(
79 name => 'Noncon[Int]',
80 parent => find_type_constraint('Noncon'),
81 type_parameter => find_type_constraint('Int'),
82 );
b10dde3a 83}, undef, 'non-containers cannot be parameterized' );
39aba5c9 84
a28e50e4 85done_testing;