Commit | Line | Data |
ff801143 |
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 | my $array_tailed_tuple = |
12 | subtype 'array_tailed_tuple', |
13 | as Tuple[ |
14 | Int, |
15 | Str, |
16 | sub { |
17 | (ArrayRef[Int])->check([@_]); |
18 | }, |
19 | ]; |
20 | |
21 | ok !$array_tailed_tuple->check(['ss',1]), 'correct fail'; |
22 | ok $array_tailed_tuple->check([1,'ss']), 'correct pass'; |
23 | ok !$array_tailed_tuple->check({}), 'correct fail'; |
24 | ok $array_tailed_tuple->check([1,'hello',1,2,3,4]), 'correct pass with tail'; |
25 | ok !$array_tailed_tuple->check([1,'hello',1,2,'bad',4]), 'correct fail with tail'; |
26 | |
27 | my $hash_tailed_tuple = |
28 | subtype 'hash_tailed_tuple', |
29 | as Tuple[ |
30 | Int, |
31 | Str, |
32 | sub { |
33 | (HashRef[Int])->check({@_}); |
34 | }, |
35 | ]; |
36 | |
37 | ok !$hash_tailed_tuple->check(['ss',1]), 'correct fail'; |
38 | ok $hash_tailed_tuple->check([1,'ss']), 'correct pass'; |
39 | ok !$hash_tailed_tuple->check({}), 'correct fail'; |
40 | ok $hash_tailed_tuple->check([1,'hello',age=>25,zip=>10533]), 'correct pass with tail'; |
41 | ok !$hash_tailed_tuple->check([1,'hello',age=>25,name=>'john']), 'correct fail with tail'; |
42 | |
43 | my $hash_tailed_dict = |
44 | subtype 'hash_tailed_dict', |
45 | as Dict[ |
46 | name=>Str, |
47 | age=>Int, |
48 | sub { |
49 | (HashRef[Int])->check({@_}); |
50 | } |
51 | ]; |
52 | |
53 | ok !$hash_tailed_dict->check({name=>'john',age=>'napiorkowski'}), 'correct fail'; |
54 | ok $hash_tailed_dict->check({name=>'Vanessa Li', age=>35}), 'correct pass'; |
55 | ok !$hash_tailed_dict->check([]), 'correct fail'; |
56 | ok $hash_tailed_dict->check({name=>'Vanessa Li', age=>35, more1=>1,more2=>2}), 'correct pass with tail'; |
57 | ok !$hash_tailed_dict->check({name=>'Vanessa Li', age=>35, more1=>1,more2=>"aa"}), 'correct fail with tail'; |
58 | |
59 | my $array_tailed_dict = |
60 | subtype 'hash_tailed_dict', |
61 | as Dict[ |
62 | name=>Str, |
63 | age=>Int, |
64 | sub { |
65 | (ArrayRef[Int])->check([@_]); |
66 | } |
67 | ]; |
68 | |
69 | ok !$array_tailed_dict->check({name=>'john',age=>'napiorkowski'}), 'correct fail'; |
70 | ok $array_tailed_dict->check({name=>'Vanessa Li', age=>35}), 'correct pass'; |
71 | ok !$array_tailed_dict->check([]), 'correct fail'; |
72 | ok $array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1,2}), 'correct pass with tail'; |
73 | ok !$array_tailed_dict->check({name=>'Vanessa Li', age=>35, 1, "hello"}), 'correct fail with tail'; |
74 | |