created sugar method for merging, with examples
[gitmo/MooseX-Types-Structured.git] / t / 11-overflow.t
CommitLineData
ff801143 1BEGIN {
2 use strict;
3 use warnings;
4 use Test::More tests=>20;
5}
6
7use Moose::Util::TypeConstraints;
8use MooseX::Types::Structured qw(Dict Tuple);
9use MooseX::Types::Moose qw(Int Str ArrayRef HashRef);
10
3b14bb98 11
12sub merge(&$) {
13 my ($code, $tc) = @_;
14 return sub {
15 my @tail_args = @_;
16 $tc->check($code->(@tail_args));
17 }
18}
19
ff801143 20my $array_tailed_tuple =
21 subtype 'array_tailed_tuple',
22 as Tuple[
23 Int,
24 Str,
3b14bb98 25 merge {[@_]} ArrayRef[Int],
ff801143 26 ];
27
28ok !$array_tailed_tuple->check(['ss',1]), 'correct fail';
29ok $array_tailed_tuple->check([1,'ss']), 'correct pass';
30ok !$array_tailed_tuple->check({}), 'correct fail';
31ok $array_tailed_tuple->check([1,'hello',1,2,3,4]), 'correct pass with tail';
32ok !$array_tailed_tuple->check([1,'hello',1,2,'bad',4]), 'correct fail with tail';
33
34my $hash_tailed_tuple =
35 subtype 'hash_tailed_tuple',
36 as Tuple[
37 Int,
38 Str,
3b14bb98 39 merge {+{@_}} HashRef[Int],
ff801143 40 ];
41
42ok !$hash_tailed_tuple->check(['ss',1]), 'correct fail';
43ok $hash_tailed_tuple->check([1,'ss']), 'correct pass';
44ok !$hash_tailed_tuple->check({}), 'correct fail';
45ok $hash_tailed_tuple->check([1,'hello',age=>25,zip=>10533]), 'correct pass with tail';
46ok !$hash_tailed_tuple->check([1,'hello',age=>25,name=>'john']), 'correct fail with tail';
47
48my $hash_tailed_dict =
49 subtype 'hash_tailed_dict',
50 as Dict[
51 name=>Str,
52 age=>Int,
3b14bb98 53 merge {+{@_}} HashRef[Int],
ff801143 54 ];
55
56ok !$hash_tailed_dict->check({name=>'john',age=>'napiorkowski'}), 'correct fail';
57ok $hash_tailed_dict->check({name=>'Vanessa Li', age=>35}), 'correct pass';
58ok !$hash_tailed_dict->check([]), 'correct fail';
59ok $hash_tailed_dict->check({name=>'Vanessa Li', age=>35, more1=>1,more2=>2}), 'correct pass with tail';
60ok !$hash_tailed_dict->check({name=>'Vanessa Li', age=>35, more1=>1,more2=>"aa"}), 'correct fail with tail';
61
62my $array_tailed_dict =
63 subtype 'hash_tailed_dict',
64 as Dict[
65 name=>Str,
66 age=>Int,
3b14bb98 67 merge {[@_]} ArrayRef[Int],
ff801143 68 ];
69
70ok !$array_tailed_dict->check({name=>'john',age=>'napiorkowski'}), 'correct fail';
71ok $array_tailed_dict->check({name=>'Vanessa Li', age=>35}), 'correct pass';
72ok !$array_tailed_dict->check([]), 'correct fail';
73ok $array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1,2}), 'correct pass with tail';
74ok !$array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1, "hello"}), 'correct fail with tail';
75