updated changelog
[gitmo/MooseX-Types-Structured.git] / t / 06-api.t
CommitLineData
a4a88fef 1BEGIN {
2 use strict;
3 use warnings;
820efec9 4 use Test::More tests=>88;
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
820efec9 115ok ( not((Tuple[Tuple[ class_type('Paper'), class_type('Stone') ], Dict[]])->equals( Tuple[Tuple[ Item, Item ], Dict[]] )), "tuple of tuple" );
116ok ( (Tuple[Tuple[ class_type('Paper'), class_type('Stone') ], Dict[]])->equals( Tuple[Tuple[ class_type('Paper'), class_type('Stone') ], Dict[]] ), "tuple of tuple" );
117ok ( (Tuple[Tuple[ class_type('Paper'), class_type('Stone') ], Dict[]])->is_a_type_of( Tuple[Tuple[ Item, Item ], Dict[]] ), "tuple of tuple" );
118ok ( (Tuple[Tuple[ class_type('Paper'), class_type('Stone') ], Dict[]])->is_a_type_of( Tuple[Tuple[ Item, Item ], Dict[]] ), "tuple of tuple" );
119ok ( (Tuple[Tuple[ class_type('Paper'), class_type('Stone') ], Dict[]])->is_subtype_of( Tuple[Tuple[ Item, Item ], Dict[]] ), "tuple of tuple" );
120
179af711 121ok ( MyDict1->is_subtype_of(HashRef), 'MyDict1 is_subtype_of HashRef');
16aea7bf 122ok ( MyDict1->is_subtype_of(Dict), 'MyDict1 is_subtype_of Dict');
179af711 123ok ( MyDict1->is_subtype_of(MyDict4), 'MyDict1 is_subtype_of MyDict4');
16aea7bf 124ok (!MyDict1->is_subtype_of(Tuple), 'MyDict1 NOT is_subtype_of Tuple');
179af711 125ok (!MyDict1->is_subtype_of(MyDict2), 'MyDict1 NOT is_subtype_of MyDict2');
126ok (!MyDict2->is_subtype_of(MyDict1), 'MyDict2 NOT is_subtype_of MyDict1');
16aea7bf 127ok (!MyDict1->is_subtype_of(MyDict3), 'MyDict1 NOT is_subtype_of MyDict3');
128ok (!MyDict2->is_subtype_of(MyDict3), 'MyDict2 NOT is_subtype_of MyDict3');
129ok ( subMyDict1->is_subtype_of(Dict), 'subMyDict1 is_subtype_of Dict');
130ok ( subMyDict1->is_subtype_of(MyDict1), 'subMyDict1 is_subtype_of MyDict1');
179af711 131ok (!subMyDict1->is_subtype_of(subMyDict1), 'subMyDict1 NOT is_subtype_of subMyDict1');
16aea7bf 132ok ( subMyDict1->is_subtype_of(MyDict2), 'subMyDict1 is_subtype_of MyDict2');
133
134ok ( MyTuple1->is_subtype_of(Tuple), 'MyTuple1 is_subtype_of Tuple');
135ok (!MyTuple1->is_subtype_of(Dict), 'MyTuple1 NOT is_subtype_of Dict');
136ok (!MyTuple1->is_subtype_of(MyTuple2), 'MyTuple1 is_subtype_of MyTuple2');
137ok (!MyTuple2->is_subtype_of(MyTuple1), 'MyTuple2 is_subtype_of MyTuple1');
138ok (!MyTuple1->is_subtype_of(MyTuple3), 'MyTuple1 NOT is_subtype_of MyTuple3');
139ok (!MyTuple2->is_subtype_of(MyTuple3), 'MyTuple2 NOT is_subtype_of MyTuple3');
a4a88fef 140
e327145a 141## Test manual parameterizing
142
143PARAMETERIZE: {
144
145 ok (my $int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Int'), 'Got Int');
146 ok (my $str = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Str'), 'Got Str');
147 ok (my $hashref = Moose::Util::TypeConstraints::find_or_parse_type_constraint('HashRef[Int]'), 'Got HashRef');
148
149 ## Test Dict->parameterize
150 ok (my $test_dict = Dict(), 'Created Test Dict');
151 ok (my $person = $test_dict->parameterize(name=>$str, age=>$int), 'Parameterized It');
152 ok ($person->check({name=>'John', age=>21}), 'Passed');
153 ok ($person->check({age=>25, name=>'User'}), 'Passed');
154
155 ## Test Tuple->parameterize
156 ok (my $test_tuple = Tuple(), 'Created Test Tuple');
157 ok (my $int_and_hashref = $test_tuple->parameterize($int, $hashref), 'Parameterized It');
158 ok ($int_and_hashref->check([1, {key=>2, key2=>3}]), "Passed");
159 ok (!$int_and_hashref->check(['a', {key=>2, key2=>3}]), "Not Passed");
160 ok (!$int_and_hashref->check([1, {key=>'a', key2=>3}]), "Not Passed");
161}