merged
[gitmo/MooseX-Types-Structured.git] / t / 02-tuple.t
CommitLineData
a30fa891 1BEGIN {
8dbdca20 2 use strict;
3 use warnings;
4 use Test::More tests=>32;
25c705c4 5 use Test::Fatal;
a30fa891 6}
7
8{
9 package Test::MooseX::Meta::TypeConstraint::Structured::Tuple;
10
11 use Moose;
12 use MooseX::Types::Structured qw(Tuple);
8dbdca20 13 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe);
14 use MooseX::Types -declare => [qw(MyString MoreThanFive FiveByFive MyArrayRefMoreThanTwoInt)];
15
a30fa891 16 subtype MyString,
a2efc685 17 as Str,
a30fa891 18 where { $_=~m/abc/};
8dbdca20 19
a2efc685 20 subtype MoreThanFive,
21 as Int,
22 where { $_ > 5};
8dbdca20 23
a2efc685 24 subtype MyArrayRefMoreThanTwoInt,
25 as ArrayRef[MoreThanFive],
26 where { scalar @$_ > 2 };
8dbdca20 27
a2efc685 28 subtype FiveByFive,
29 as Tuple[MoreThanFive, MyArrayRefMoreThanTwoInt];
8dbdca20 30
a30fa891 31 #use Data::Dump qw/dump/; warn dump Tuple;
32
33 has 'tuple' => (is=>'rw', isa=>Tuple[Int, Str, MyString]);
8dbdca20 34 has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]);
35 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int], Object]);
36 has 'tuple_with_maybe2' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]);
37 has 'tuple_with_union' => (is=>'rw', isa=>Tuple[Int,Str,Int|Object,Int]);
38 has 'tuple2' => (is=>'rw', isa=>Tuple[Int,Str,Int]);
39 has 'tuple_with_parameterized' => (is=>'rw', isa=>Tuple[Int,Str,Int,ArrayRef[Int]]);
a2efc685 40 has 'FiveByFiveAttr' => (is=>'rw', isa=>FiveByFive);
a30fa891 41}
42
43## Instantiate a new test object
44
45ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Tuple->new
46 => 'Instantiated new Record test class.';
8dbdca20 47
a30fa891 48isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Tuple'
49 => 'Created correct object type.';
8dbdca20 50
a30fa891 51## Test Tuple type constraint
52
25c705c4 53is( exception {
a30fa891 54 $record->tuple([1,'hello', 'test.abc.test']);
25c705c4 55} => undef, 'Set tuple attribute without error');
a30fa891 56
57is $record->tuple->[0], 1
58 => 'correct set the tuple attribute index 0';
59
60is $record->tuple->[1], 'hello'
61 => 'correct set the tuple attribute index 1';
62
63is $record->tuple->[2], 'test.abc.test'
64 => 'correct set the tuple attribute index 2';
65
25c705c4 66like( exception {
8dbdca20 67 $record->tuple([1,'hello', 'test.xxx.test']);
a30fa891 68}, qr/Attribute \(tuple\) does not pass the type constraint/
25c705c4 69 => 'Properly failed for bad value in custom type constraint');
8dbdca20 70
25c705c4 71like( exception {
8dbdca20 72 $record->tuple(['asdasd',2, 'test.abc.test']);
a30fa891 73}, qr/Attribute \(tuple\) does not pass the type constraint/
25c705c4 74 => 'Got Expected Error for violating constraints');
a30fa891 75
76## Test tuple_with_maybe
77
25c705c4 78is( exception {
a30fa891 79 $record->tuple_with_maybe([1,'hello', 1, $record]);
25c705c4 80} => undef, 'Set tuple attribute without error');
a30fa891 81
25c705c4 82like( exception {
a30fa891 83 $record->tuple_with_maybe([1,'hello', 'a', $record]);
84}, qr/Attribute \(tuple_with_maybe\) does not pass the type constraint/
25c705c4 85 => 'Properly failed for bad value parameterized constraint');
a30fa891 86
25c705c4 87is( exception {
a30fa891 88 $record->tuple_with_maybe([1,'hello',undef, $record]);
25c705c4 89} => undef, 'Set tuple attribute without error skipping optional parameter');
a30fa891 90
91## Test tuple_with_maybe2
92
25c705c4 93is( exception {
a30fa891 94 $record->tuple_with_maybe2([1,'hello', 1]);
25c705c4 95} => undef, 'Set tuple attribute without error');
a30fa891 96
25c705c4 97like( exception {
a30fa891 98 $record->tuple_with_maybe2([1,'hello', 'a']);
99}, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
25c705c4 100 => 'Properly failed for bad value parameterized constraint');
a30fa891 101
25c705c4 102is( exception {
a30fa891 103 $record->tuple_with_maybe2([1,'hello',undef]);
25c705c4 104} => undef, 'Set tuple attribute without error skipping optional parameter');
a30fa891 105
190a34eb 106SKIP: {
107 skip 'Core Maybe incorrectly allows null.', 1, 1;
25c705c4 108 like( exception {
190a34eb 109 $record->tuple_with_maybe2([1,'hello']);
110 }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
25c705c4 111 => 'Properly fails for missing maybe (needs to be at least undef)');
190a34eb 112}
a30fa891 113
114## Test Tuple with parameterized type
115
25c705c4 116is( exception {
a30fa891 117 $record->tuple_with_param([1,'hello', [1,2,3]]);
25c705c4 118} => undef, 'Set tuple attribute without error');
a30fa891 119
25c705c4 120like( exception {
a30fa891 121 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
122}, qr/Attribute \(tuple_with_param\) does not pass the type constraint/
25c705c4 123 => 'Properly failed for bad value parameterized constraint');
a30fa891 124
125## Test tuple2 (Tuple[Int,Str,Int])
126
127ok $record->tuple2([1,'hello',3])
128 => "[1,'hello',3] properly suceeds";
129
25c705c4 130like( exception {
8dbdca20 131 $record->tuple2([1,2,'world']);
25c705c4 132}, qr/Attribute \(tuple2\) does not pass the type constraint/ => "[1,2,'world'] properly fails");
a30fa891 133
25c705c4 134like( exception {
8dbdca20 135 $record->tuple2(['hello1',2,3]);
25c705c4 136}, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,3] properly fails");
a30fa891 137
25c705c4 138like( exception {
8dbdca20 139 $record->tuple2(['hello2',2,'world']);
25c705c4 140}, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,'world'] properly fails");
a30fa891 141
142
143## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
144
145ok $record->tuple_with_parameterized([1,'hello',3,[1,2,3]])
146 => "[1,'hello',3,[1,2,3]] properly suceeds";
147
25c705c4 148like( exception {
8dbdca20 149 $record->tuple_with_parameterized([1,2,'world']);
a30fa891 150}, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
25c705c4 151 => "[1,2,'world'] properly fails");
a30fa891 152
25c705c4 153like( exception {
8dbdca20 154 $record->tuple_with_parameterized(['hello1',2,3]);
a30fa891 155}, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
25c705c4 156 => "['hello',2,3] properly fails");
a30fa891 157
25c705c4 158like( exception {
8dbdca20 159 $record->tuple_with_parameterized(['hello2',2,'world']);
a30fa891 160}, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
25c705c4 161 => "['hello',2,'world'] properly fails");
a30fa891 162
25c705c4 163like( exception {
8dbdca20 164 $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
a30fa891 165}, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
25c705c4 166 => "[1,'hello',3,[1,2,'world']] properly fails");
a2efc685 167
168## Test FiveByFiveAttr
169
25c705c4 170is( exception {
a2efc685 171 $record->FiveByFiveAttr([6,[7,8,9]]);
25c705c4 172} => undef, 'Set FiveByFiveAttr correctly');
a2efc685 173
25c705c4 174like( exception {
8dbdca20 175 $record->FiveByFiveAttr([1,'hello', 'test']);
a2efc685 176}, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
25c705c4 177 => q{Properly failed for bad value in FiveByFiveAttr [1,'hello', 'test']});
a2efc685 178
25c705c4 179like( exception {
8dbdca20 180 $record->FiveByFiveAttr([1,[8,9,10]]);
a2efc685 181}, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
25c705c4 182 => q{Properly failed for bad value in FiveByFiveAttr [1,[8,9,10]]});
8dbdca20 183
25c705c4 184like( exception {
8dbdca20 185 $record->FiveByFiveAttr([10,[11,12,0]]);
a2efc685 186}, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
25c705c4 187 => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12,0]]});
8dbdca20 188
25c705c4 189like( exception {
8dbdca20 190 $record->FiveByFiveAttr([1,[1,1,0]]);
a2efc685 191}, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
25c705c4 192 => q{Properly failed for bad value in FiveByFiveAttr [1,[1,1,0]]});
a2efc685 193
25c705c4 194like( exception {
8dbdca20 195 $record->FiveByFiveAttr([10,[11,12]]);
a2efc685 196}, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
25c705c4 197 => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12]});
8dbdca20 198