moving stuff around a bit
[gitmo/Moose.git] / t / 040_type_constraints / 018_custom_parameterized_types.t
CommitLineData
39aba5c9 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 21;
7use Test::Exception;
8
9BEGIN {
10 use_ok("Moose::Util::TypeConstraints");
11 use_ok('Moose::Meta::TypeConstraint::Parameterized');
12}
13
14lives_ok {
15 subtype 'AlphaKeyHash' => as 'HashRef'
16 => where {
17 # no keys match non-alpha
18 (grep { /[^a-zA-Z]/ } keys %$_) == 0
19 };
20} '... created the subtype special okay';
21
22lives_ok {
23 subtype 'Trihash' => as 'AlphaKeyHash'
24 => where {
25 keys(%$_) == 3
26 };
27} '... created the subtype special okay';
28
29lives_ok {
30 subtype 'Noncon' => as 'Item';
31} '... created the subtype special okay';
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');
46}
47
7e4e1ad4 48my $hoi = Moose::Util::TypeConstraints::find_or_create_type_constraint('AlphaKeyHash[Int]');
39aba5c9 49
50ok($hoi->check({ one => 1, two => 2 }), '... validated it correctly');
51ok(!$hoi->check({ one1 => 1, two2 => 2 }), '... validated it correctly');
52ok(!$hoi->check({ one => 'uno', two => 'dos' }), '... validated it correctly');
53ok(!$hoi->check({ one1 => 'un', two2 => 'deux' }), '... validated it correctly');
54
7e4e1ad4 55my $th = Moose::Util::TypeConstraints::find_or_create_type_constraint('Trihash[Bool]');
39aba5c9 56
57ok(!$th->check({ one => 1, two => 1 }), '... validated it correctly');
58ok($th->check({ one => 1, two => 0, three => 1 }), '... validated it correctly');
59ok(!$th->check({ one => 1, two => 2, three => 1 }), '... validated it correctly');
60ok(!$th->check({foo1 => 1, bar2 => 0, baz3 => 1}), '... validated it correctly');
61
62dies_ok {
63 Moose::Meta::TypeConstraint::Parameterized->new(
64 name => 'Str[Int]',
65 parent => find_type_constraint('Str'),
66 type_parameter => find_type_constraint('Int'),
67 );
68} 'non-containers cannot be parameterized';
69
70dies_ok {
71 Moose::Meta::TypeConstraint::Parameterized->new(
72 name => 'Noncon[Int]',
73 parent => find_type_constraint('Noncon'),
74 type_parameter => find_type_constraint('Int'),
75 );
76} 'non-containers cannot be parameterized';
77