fix is subtype of (sortof)
[gitmo/MooseX-Types-Structured.git] / t / 06-api.t
CommitLineData
a4a88fef 1BEGIN {
2 use strict;
3 use warnings;
179af711 4 use Test::More tests=>83;
a4a88fef 5}
6
e327145a 7use Moose::Util::TypeConstraints;
16aea7bf 8use MooseX::Types::Structured qw(Dict Tuple);
179af711 9use MooseX::Types::Moose qw(Int Str Item Object ArrayRef HashRef);
16aea7bf 10use MooseX::Types -declare => [qw(
179af711 11 MyDict1 MyDict2 MyDict3 MyDict4 subMyDict3 subMyDict1
16aea7bf 12 MyTuple1 MyTuple2 MyTuple3 subMyTuple3
13)];
14
15## Create some sample Dicts
16
17subtype MyDict1,
18 as Dict[name=>Str, age=>Int];
19
20subtype subMyDict1,
21 as MyDict1;
22
23subtype MyDict2,
24 as Dict[name=>Str, age=>Int];
25
26subtype MyDict3,
27 as Dict[key=>Int, anotherkey=>Str];
28
29subtype subMyDict3,
30 as MyDict3;
31
179af711 32subtype MyDict4,
33 as Dict[name=>Str, age=>Item];
34
16aea7bf 35## Create some sample Tuples
36
37subtype MyTuple1,
38 as Tuple[Int,Int,Str];
39
40subtype MyTuple2,
41 as Tuple[Int,Int,Str];
42
43subtype MyTuple3,
44 as Tuple[Object, HashRef];
45
46subtype subMyTuple3,
47 as MyTuple3;
a4a88fef 48
49## Test equals
50
16aea7bf 51ok ( MyDict1->equals(MyDict2), 'MyDict1 == MyDict2');
52ok ( MyDict2->equals(MyDict1), 'MyDict2 == MyDict1');
53ok (!MyDict1->equals(MyDict3), 'MyDict1 == MyDict3');
54ok (!MyDict2->equals(MyDict3), 'MyDict2 == MyDict3');
55ok (!MyDict3->equals(MyDict2), 'MyDict3 == MyDict2');
56ok (!MyDict3->equals(MyDict1), 'MyDict3 == MyDict1');
a4a88fef 57
16aea7bf 58ok ( MyTuple1->equals(MyTuple2), 'MyTuple1 == MyTuple2');
59ok ( MyTuple2->equals(MyTuple1), 'MyTuple2 == MyTuple1');
60ok (!MyTuple1->equals(MyTuple3), 'MyTuple1 == MyTuple3');
61ok (!MyTuple2->equals(MyTuple3), 'MyTuple2 == MyTuple3');
62ok (!MyTuple3->equals(MyTuple2), 'MyTuple3 == MyTuple2');
63ok (!MyTuple3->equals(MyTuple1), 'MyTuple3 == MyTuple1');
a4a88fef 64
fe1fd33c 65ok ( MyDict1->equals(MyDict2), 'MyDict1 == MyDict2');
66ok ( MyDict2->equals(MyDict1), 'MyDict2 == MyDict1');
67ok (!MyDict1->equals(MyDict3), 'MyDict1 == MyDict3');
179af711 68ok (!MyDict1->equals(MyDict4), 'MyDict1 == MyDict3');
fe1fd33c 69ok (!MyDict2->equals(MyDict3), 'MyDict2 == MyDict3');
179af711 70ok (!MyDict2->equals(MyDict4), 'MyDict2 == MyDict3');
fe1fd33c 71ok (!MyDict3->equals(MyDict2), 'MyDict3 == MyDict2');
179af711 72ok (!MyDict3->equals(MyDict4), 'MyDict3 == MyDict2');
fe1fd33c 73ok (!MyDict3->equals(MyDict1), 'MyDict3 == MyDict1');
179af711 74ok (!MyDict4->equals(MyDict1), 'MyDict3 == MyDict1');
75ok (!MyDict4->equals(MyDict2), 'MyDict3 == MyDict1');
76ok (!MyDict4->equals(MyDict3), 'MyDict3 == MyDict1');
fe1fd33c 77
78ok ( MyTuple1->equals(MyTuple2), 'MyTuple1 == MyTuple2');
79ok ( MyTuple2->equals(MyTuple1), 'MyTuple2 == MyTuple1');
80ok (!MyTuple1->equals(MyTuple3), 'MyTuple1 == MyTuple3');
81ok (!MyTuple2->equals(MyTuple3), 'MyTuple2 == MyTuple3');
82ok (!MyTuple3->equals(MyTuple2), 'MyTuple3 == MyTuple2');
83ok (!MyTuple3->equals(MyTuple1), 'MyTuple3 == MyTuple1');
af1d00c9 84
a4a88fef 85## Test is_a_type_of
86
179af711 87ok ( MyDict1->is_a_type_of(HashRef), 'MyDict1 is_a_type_of HashRef');
16aea7bf 88ok ( MyDict1->is_a_type_of(Dict), 'MyDict1 is_a_type_of Dict');
89ok (!MyDict1->is_a_type_of(Tuple), 'MyDict1 NOT is_a_type_of Tuple');
90ok ( MyDict1->is_a_type_of(MyDict2), 'MyDict1 is_a_type_of MyDict2');
91ok ( MyDict2->is_a_type_of(MyDict1), 'MyDict2 is_a_type_of MyDict1');
92ok (!MyDict1->is_a_type_of(MyDict3), 'MyDict1 NOT is_a_type_of MyDict3');
93ok (!MyDict2->is_a_type_of(MyDict3), 'MyDict2 NOT is_a_type_of MyDict3');
94ok ( subMyDict1->is_a_type_of(Dict), 'subMyDict1 type of Dict');
95ok ( subMyDict1->is_a_type_of(MyDict1), 'subMyDict1 type of MyDict1');
96ok ( subMyDict1->is_a_type_of(subMyDict1), 'subMyDict1 type of subMyDict1');
97ok ( subMyDict1->is_a_type_of(MyDict2), 'subMyDict1 type of MyDict2');
179af711 98ok ( MyDict4->is_a_type_of(HashRef), 'MyDict4 is_a_type_of HashRef');
99ok ( MyDict4->is_a_type_of(Dict), 'MyDict4 is_a_type_of Dict');
100ok (!MyDict4->is_a_type_of(Tuple), 'MyDict4 NOT is_a_type_of Tuple');
101ok (!MyDict4->is_a_type_of(MyDict2), 'MyDict4 NOT is_a_type_of MyDict2');
102ok ( MyDict2->is_a_type_of(MyDict4), 'MyDict2 is_a_type_of MyDict4');
103ok (!MyDict4->is_a_type_of(MyDict3), 'MyDict4 NOT is_a_type_of MyDict3');
104
16aea7bf 105
106ok ( MyTuple1->is_a_type_of(Tuple), 'MyTuple1 is_a_type_of Tuple');
107ok (!MyTuple1->is_a_type_of(Dict), 'MyTuple1 NOT is_a_type_of Dict');
108ok ( MyTuple1->is_a_type_of(MyTuple2), 'MyTuple1 is_a_type_of MyTuple2');
109ok ( MyTuple2->is_a_type_of(MyTuple1), 'MyTuple2 is_a_type_of MyTuple1');
110ok (!MyTuple1->is_a_type_of(MyTuple3), 'MyTuple1 NOT is_a_type_of MyTuple3');
111ok (!MyTuple2->is_a_type_of(MyTuple3), 'MyTuple2 NOT is_a_type_of MyTuple3');
112
a4a88fef 113## is_subtype_of
114
179af711 115ok ( MyDict1->is_subtype_of(HashRef), 'MyDict1 is_subtype_of HashRef');
16aea7bf 116ok ( MyDict1->is_subtype_of(Dict), 'MyDict1 is_subtype_of Dict');
179af711 117ok ( MyDict1->is_subtype_of(MyDict4), 'MyDict1 is_subtype_of MyDict4');
16aea7bf 118ok (!MyDict1->is_subtype_of(Tuple), 'MyDict1 NOT is_subtype_of Tuple');
179af711 119ok (!MyDict1->is_subtype_of(MyDict2), 'MyDict1 NOT is_subtype_of MyDict2');
120ok (!MyDict2->is_subtype_of(MyDict1), 'MyDict2 NOT is_subtype_of MyDict1');
16aea7bf 121ok (!MyDict1->is_subtype_of(MyDict3), 'MyDict1 NOT is_subtype_of MyDict3');
122ok (!MyDict2->is_subtype_of(MyDict3), 'MyDict2 NOT is_subtype_of MyDict3');
123ok ( subMyDict1->is_subtype_of(Dict), 'subMyDict1 is_subtype_of Dict');
124ok ( subMyDict1->is_subtype_of(MyDict1), 'subMyDict1 is_subtype_of MyDict1');
179af711 125ok (!subMyDict1->is_subtype_of(subMyDict1), 'subMyDict1 NOT is_subtype_of subMyDict1');
16aea7bf 126ok ( subMyDict1->is_subtype_of(MyDict2), 'subMyDict1 is_subtype_of MyDict2');
127
128ok ( MyTuple1->is_subtype_of(Tuple), 'MyTuple1 is_subtype_of Tuple');
129ok (!MyTuple1->is_subtype_of(Dict), 'MyTuple1 NOT is_subtype_of Dict');
130ok (!MyTuple1->is_subtype_of(MyTuple2), 'MyTuple1 is_subtype_of MyTuple2');
131ok (!MyTuple2->is_subtype_of(MyTuple1), 'MyTuple2 is_subtype_of MyTuple1');
132ok (!MyTuple1->is_subtype_of(MyTuple3), 'MyTuple1 NOT is_subtype_of MyTuple3');
133ok (!MyTuple2->is_subtype_of(MyTuple3), 'MyTuple2 NOT is_subtype_of MyTuple3');
a4a88fef 134
e327145a 135## Test manual parameterizing
136
137PARAMETERIZE: {
138
139 ok (my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int'), 'Got Int');
140 ok (my $str = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Str'), 'Got Str');
141 ok (my $hashref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('HashRef[Int]'), 'Got HashRef');
142
143 ## Test Dict->parameterize
144 ok (my $test_dict = Dict(), 'Created Test Dict');
145 ok (my $person = $test_dict->parameterize(name=>$str, age=>$int), 'Parameterized It');
146 ok ($person->check({name=>'John', age=>21}), 'Passed');
147 ok ($person->check({age=>25, name=>'User'}), 'Passed');
148
149 ## Test Tuple->parameterize
150 ok (my $test_tuple = Tuple(), 'Created Test Tuple');
151 ok (my $int_and_hashref = $test_tuple->parameterize($int, $hashref), 'Parameterized It');
152 ok ($int_and_hashref->check([1, {key=>2, key2=>3}]), "Passed");
153 ok (!$int_and_hashref->check(['a', {key=>2, key2=>3}]), "Not Passed");
154 ok (!$int_and_hashref->check([1, {key=>'a', key2=>3}]), "Not Passed");
155}