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