4bd5960d6655bb8b29def5b28b58d150841aef7d
[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 => 33;
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
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 }
29
30 ## Make sure we have a 'create object sanity check'
31
32 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
33  => 'Created some sort of object';
34  
35 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
36  => "Yes, it's the correct kind of object";
37
38 ## test arrayrefbase normal and coercion
39
40 ok $type->arrayrefbase([qw(a b c)])
41  => 'Assigned arrayrefbase qw(a b c)';
42  
43 is_deeply $type->arrayrefbase, [qw(a b c)],
44  => 'Assignment is correct';
45
46 ok $type->arrayrefbase('d,e,f')
47  => 'Assignment arrayrefbase d,e,f to test coercion';
48  
49 is_deeply $type->arrayrefbase, [qw(d e f)],
50  => 'Assignment and coercion is correct';
51
52 ## test arrayrefint01 normal and coercion
53
54 ok $type->arrayrefint01([qw(1 2 3)])
55  => 'Assignment arrayrefint01 qw(1 2 3)';
56  
57 is_deeply $type->arrayrefint01, [qw(1 2 3)],
58  => 'Assignment is correct';
59
60 ok $type->arrayrefint01('4.5.6')
61  => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
62  
63 is_deeply $type->arrayrefint01, [qw(4 5 6)],
64  => 'Assignment and coercion is correct';
65
66 ok $type->arrayrefint01({a=>7,b=>8})
67  => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
68  
69 is_deeply $type->arrayrefint01, [qw(7 8)],
70  => 'Assignment and coercion is correct';
71  
72 throws_ok sub {
73     $type->arrayrefint01([qw(a b c)])
74 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
75
76 ## test arrayrefint02 normal and coercion
77
78 ok $type->arrayrefint02([qw(1 2 3)])
79  => 'Assigned arrayrefint02 qw(1 2 3)';
80  
81 is_deeply $type->arrayrefint02, [qw(1 2 3)],
82  => 'Assignment is correct';
83
84 ok $type->arrayrefint02('4:5:6')
85  => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
86  
87 is_deeply $type->arrayrefint02, [qw(4 5 6)],
88  => 'Assignment and coercion is correct';
89
90 ok $type->arrayrefint02({a=>7,b=>8})
91  => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
92  
93 is_deeply $type->arrayrefint02, [qw(7 8)],
94  => 'Assignment and coercion is correct';
95  
96 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
97  => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
98  
99 is_deeply $type->arrayrefint02, [qw(2 3 7)],
100  => 'Assignment and coercion is correct';
101
102 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
103  => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
104  
105 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
106  => 'Assignment and coercion is correct';
107  
108 # test arrayrefint03 
109
110 ok $type->arrayrefint03([qw(11 12 13)])
111  => 'Assigned arrayrefint01 qw(11 12 13)';
112  
113 is_deeply $type->arrayrefint03, [qw(11 12 13)],
114  => 'Assignment is correct';
115  
116 throws_ok sub {
117     $type->arrayrefint03([qw(a b c)])
118 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
119
120 # TEST StrOrArrayRef
121
122 ok $type->StrOrArrayRef('string')
123  => 'String part of union is good';
124
125 ok $type->StrOrArrayRef([1,2,3])
126  => 'arrayref part of union is good';
127  
128 throws_ok sub {
129     $type->StrOrArrayRef({a=>111});
130 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
131
132 # Test AtLeastOneInt
133
134 ok $type->AtLeastOneInt([1,2]),
135  => 'Good assignment';
136
137 is_deeply $type->AtLeastOneInt, [1,2]
138  => "Got expected values.";
139  
140 throws_ok sub {
141     $type->AtLeastOneInt([]);
142 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
143
144 throws_ok sub {
145     $type->AtLeastOneInt(['a','b']);
146 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
147