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