doubt we need the concept file anymore
[gitmo/MooseX-Types-Structured.git] / t / constraints.t
CommitLineData
65748864 1BEGIN {
2 use strict;
3 use warnings;
9a920d27 4 use Test::More tests=>25;
65748864 5 use Test::Exception;
6}
7
8{
bc64165b 9 package Test::MooseX::Meta::TypeConstraint::Structured;
65748864 10
11 use Moose;
12 use Moose::Util::TypeConstraints;
9a491c80 13 use MooseX::Meta::TypeConstraint::Structured::Named;
14 use MooseX::Meta::TypeConstraint::Structured::Positional;
9a920d27 15
c3abf064 16 subtype 'MyString',
17 as 'Str',
18 where { $_=~m/abc/};
9a920d27 19
65748864 20 sub Tuple {
9a491c80 21 my ($args, $optional) = @_;
22 my @args = @$args;
23 my @optional = ref $optional eq 'ARRAY' ? @$optional : ();
24
25 return MooseX::Meta::TypeConstraint::Structured::Positional->new(
65748864 26 name => 'Tuple',
27 parent => find_type_constraint('ArrayRef'),
28 package_defined_in => __PACKAGE__,
8b276dd4 29 signature => [map {
9a920d27 30 _normalize_type_constraint($_);
8b276dd4 31 } @args],
bc64165b 32 );
33 }
9a920d27 34
bc64165b 35 sub Dict {
9a491c80 36 my ($args, $optional) = @_;
37 my %args = @$args;
38 my %optional = ref $optional eq 'HASH' ? @$optional : ();
39
40 return MooseX::Meta::TypeConstraint::Structured::Named->new(
8b276dd4 41 name => 'Dict',
bc64165b 42 parent => find_type_constraint('HashRef'),
43 package_defined_in => __PACKAGE__,
8b276dd4 44 signature => {map {
9a920d27 45 $_ => _normalize_type_constraint($args{$_});
8b276dd4 46 } keys %args},
65748864 47 );
48 }
c3abf064 49
9a920d27 50 sub _normalize_type_constraint {
51 my $tc = shift @_;
52 if($tc && blessed $tc && $tc->isa('Moose::Meta::TypeConstraint')) {
53 return $tc;
54 } elsif($tc) {
55 return Moose::Util::TypeConstraints::find_or_parse_type_constraint($tc);
56 }
57 }
58
c3abf064 59 has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str', 'MyString']);
bc64165b 60 has 'dict' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Int']);
8b276dd4 61 has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Maybe[Int]']);
62 has 'tuple_with_param' => (is=>'rw', isa=>Tuple['Int', 'Str', 'ArrayRef[Int]']);
63 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple['Int', 'Str', 'Maybe[Int]']);
9a920d27 64 has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>'Str', key2=>Tuple['Int','Str']]);
65748864 65}
66
67## Instantiate a new test object
68
bc64165b 69ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
65748864 70 => 'Instantiated new Record test class.';
71
bc64165b 72isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
65748864 73 => 'Created correct object type.';
9a920d27 74
bc64165b 75## Test Tuple type constraint
76
65748864 77lives_ok sub {
c3abf064 78 $record->tuple([1,'hello', 'test.abc.test']);
65748864 79} => 'Set tuple attribute without error';
80
81is $record->tuple->[0], 1
82 => 'correct set the tuple attribute index 0';
83
84is $record->tuple->[1], 'hello'
85 => 'correct set the tuple attribute index 1';
86
c3abf064 87is $record->tuple->[2], 'test.abc.test'
88 => 'correct set the tuple attribute index 2';
89
90throws_ok sub {
91 $record->tuple([1,'hello', 'test.xxx.test']);
92}, qr/Validation failed for 'MyString'/
93 => 'Properly failed for bad value in custom type constraint';
94
65748864 95throws_ok sub {
c3abf064 96 $record->tuple(['asdasd',2, 'test.abc.test']);
65748864 97}, qr/Validation failed for 'Int'/
98 => 'Got Expected Error for violating constraints';
99
bc64165b 100## Test the Dictionary type constraint
101
102lives_ok sub {
103 $record->dict({name=>'frith', age=>23});
104} => 'Set dict attribute without error';
105
106is $record->dict->{name}, 'frith'
107 => 'correct set the dict attribute name';
108
109is $record->dict->{age}, 23
110 => 'correct set the dict attribute age';
111
112throws_ok sub {
113 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
114}, qr/Validation failed for 'Str'/
115 => 'Got Expected Error for bad value in dict';
8b276dd4 116
117## Test tuple_with_maybe
118
119lives_ok sub {
120 $record->tuple_with_maybe([1,'hello', 1]);
121} => 'Set tuple attribute without error';
122
123throws_ok sub {
124 $record->tuple_with_maybe([1,'hello', 'a']);
125}, qr/Validation failed for 'Maybe\[Int\]'/
126 => 'Properly failed for bad value parameterized constraint';
127
128lives_ok sub {
129 $record->tuple_with_maybe([1,'hello']);
130} => 'Set tuple attribute without error skipping optional parameter';
131
132## Test Tuple with parameterized type
133
134lives_ok sub {
135 $record->tuple_with_param([1,'hello', [1,2,3]]);
136} => 'Set tuple attribute without error';
137
138throws_ok sub {
139 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
140}, qr/Validation failed for 'ArrayRef\[Int\]'/
141 => 'Properly failed for bad value parameterized constraint';
142
143## Test dict_with_maybe
144
145lives_ok sub {
146 $record->dict_with_maybe({name=>'frith', age=>23});
147} => 'Set dict attribute without error';
148
149is $record->dict_with_maybe->{name}, 'frith'
150 => 'correct set the dict attribute name';
151
152is $record->dict_with_maybe->{age}, 23
153 => 'correct set the dict attribute age';
154
155throws_ok sub {
156 $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'});
157}, qr/Validation failed for 'Str'/
158 => 'Got Expected Error for bad value in dict';
159
160throws_ok sub {
161 $record->dict_with_maybe({age=>30});
162}, qr/Validation failed for 'Str'/
163 => 'Got Expected Error for missing named parameter';
164
165lives_ok sub {
166 $record->dict_with_maybe({name=>'usal'});
167} => 'Set dict attribute without error, skipping optional';
9a920d27 168
169## Test dict_with_tuple
170
171lives_ok sub {
172 $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
173} => 'Set tuple attribute without error';
174
175throws_ok sub {
176 $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
177}, qr/Validation failed for 'Int'/
178 => 'Threw error on bad constraint';
179
180
181