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