added pipe overloading test for class using a type library
[gitmo/MooseX-Types.git] / t / 13_typedecorator.t
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4
5 use Test::More tests => 36;
6 use Test::Exception;
7 use FindBin;
8 use lib "$FindBin::Bin/lib";
9
10 {
11     package Test::MooseX::TypeLibrary::TypeDecorator;
12     
13     use Moose;
14     use MooseX::Types::Moose qw(
15         Int Str
16     );
17     use DecoratorLibrary qw(
18         MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
19         AtLeastOneInt
20     );
21     
22     has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1);
23     has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1);
24     has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1);
25     has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]);
26     has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef);
27     has 'AtLeastOneInt' => (is=>'rw', isa=>AtLeastOneInt);
28     has 'pipeoverloading' => (is=>'rw', isa=>Int|Str);
29 }
30
31 ## Make sure we have a 'create object sanity check'
32
33 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
34  => 'Created some sort of object';
35  
36 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
37  => "Yes, it's the correct kind of object";
38  
39 ## Test pipeoverloading
40
41 ok $type->pipeoverloading(1)
42  => 'Integer for union test accepted';
43  
44 ok $type->pipeoverloading('a')
45  => 'String for union test accepted';
46
47 throws_ok sub {
48     $type->pipeoverloading({a=>1,b=>2});
49 }, qr/Validation failed for 'Int | Str'/ => 'Union test corrected fails a HashRef';
50
51 ## test arrayrefbase normal and coercion
52
53 ok $type->arrayrefbase([qw(a b c)])
54  => 'Assigned arrayrefbase qw(a b c)';
55  
56 is_deeply $type->arrayrefbase, [qw(a b c)],
57  => 'Assignment is correct';
58
59 ok $type->arrayrefbase('d,e,f')
60  => 'Assignment arrayrefbase d,e,f to test coercion';
61  
62 is_deeply $type->arrayrefbase, [qw(d e f)],
63  => 'Assignment and coercion is correct';
64
65 ## test arrayrefint01 normal and coercion
66
67 ok $type->arrayrefint01([qw(1 2 3)])
68  => 'Assignment arrayrefint01 qw(1 2 3)';
69  
70 is_deeply $type->arrayrefint01, [qw(1 2 3)],
71  => 'Assignment is correct';
72
73 ok $type->arrayrefint01('4.5.6')
74  => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
75  
76 is_deeply $type->arrayrefint01, [qw(4 5 6)],
77  => 'Assignment and coercion is correct';
78
79 ok $type->arrayrefint01({a=>7,b=>8})
80  => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
81  
82 is_deeply $type->arrayrefint01, [qw(7 8)],
83  => 'Assignment and coercion is correct';
84  
85 throws_ok sub {
86     $type->arrayrefint01([qw(a b c)])
87 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
88
89 ## test arrayrefint02 normal and coercion
90
91 ok $type->arrayrefint02([qw(1 2 3)])
92  => 'Assigned arrayrefint02 qw(1 2 3)';
93  
94 is_deeply $type->arrayrefint02, [qw(1 2 3)],
95  => 'Assignment is correct';
96
97 ok $type->arrayrefint02('4:5:6')
98  => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
99  
100 is_deeply $type->arrayrefint02, [qw(4 5 6)],
101  => 'Assignment and coercion is correct';
102
103 ok $type->arrayrefint02({a=>7,b=>8})
104  => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
105  
106 is_deeply $type->arrayrefint02, [qw(7 8)],
107  => 'Assignment and coercion is correct';
108  
109 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
110  => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
111  
112 is_deeply $type->arrayrefint02, [qw(2 3 7)],
113  => 'Assignment and coercion is correct';
114
115 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
116  => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
117  
118 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
119  => 'Assignment and coercion is correct';
120  
121 # test arrayrefint03 
122
123 ok $type->arrayrefint03([qw(11 12 13)])
124  => 'Assigned arrayrefint01 qw(11 12 13)';
125  
126 is_deeply $type->arrayrefint03, [qw(11 12 13)],
127  => 'Assignment is correct';
128  
129 throws_ok sub {
130     $type->arrayrefint03([qw(a b c)])
131 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
132
133 # TEST StrOrArrayRef
134
135 ok $type->StrOrArrayRef('string')
136  => 'String part of union is good';
137
138 ok $type->StrOrArrayRef([1,2,3])
139  => 'arrayref part of union is good';
140  
141 throws_ok sub {
142     $type->StrOrArrayRef({a=>111});
143 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
144
145 # Test AtLeastOneInt
146
147 ok $type->AtLeastOneInt([1,2]),
148  => 'Good assignment';
149
150 is_deeply $type->AtLeastOneInt, [1,2]
151  => "Got expected values.";
152  
153 throws_ok sub {
154     $type->AtLeastOneInt([]);
155 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
156
157 throws_ok sub {
158     $type->AtLeastOneInt(['a','b']);
159 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
160