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