5 use Test::More tests => 36;
8 use lib "$FindBin::Bin/lib";
11 package Test::MooseX::TypeLibrary::TypeDecorator;
14 use MooseX::Types::Moose qw(
17 use DecoratorLibrary qw(
18 MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
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);
31 ## Make sure we have a 'create object sanity check'
33 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
34 => 'Created some sort of object';
36 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
37 => "Yes, it's the correct kind of object";
39 ## Test pipeoverloading
41 ok $type->pipeoverloading(1)
42 => 'Integer for union test accepted';
44 ok $type->pipeoverloading('a')
45 => 'String for union test accepted';
48 $type->pipeoverloading({a=>1,b=>2});
49 }, qr/Validation failed for 'Int | Str'/ => 'Union test corrected fails a HashRef';
51 ## test arrayrefbase normal and coercion
53 ok $type->arrayrefbase([qw(a b c)])
54 => 'Assigned arrayrefbase qw(a b c)';
56 is_deeply $type->arrayrefbase, [qw(a b c)],
57 => 'Assignment is correct';
59 ok $type->arrayrefbase('d,e,f')
60 => 'Assignment arrayrefbase d,e,f to test coercion';
62 is_deeply $type->arrayrefbase, [qw(d e f)],
63 => 'Assignment and coercion is correct';
65 ## test arrayrefint01 normal and coercion
67 ok $type->arrayrefint01([qw(1 2 3)])
68 => 'Assignment arrayrefint01 qw(1 2 3)';
70 is_deeply $type->arrayrefint01, [qw(1 2 3)],
71 => 'Assignment is correct';
73 ok $type->arrayrefint01('4.5.6')
74 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
76 is_deeply $type->arrayrefint01, [qw(4 5 6)],
77 => 'Assignment and coercion is correct';
79 ok $type->arrayrefint01({a=>7,b=>8})
80 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
82 is_deeply $type->arrayrefint01, [qw(7 8)],
83 => 'Assignment and coercion is correct';
86 $type->arrayrefint01([qw(a b c)])
87 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
89 ## test arrayrefint02 normal and coercion
91 ok $type->arrayrefint02([qw(1 2 3)])
92 => 'Assigned arrayrefint02 qw(1 2 3)';
94 is_deeply $type->arrayrefint02, [qw(1 2 3)],
95 => 'Assignment is correct';
97 ok $type->arrayrefint02('4:5:6')
98 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
100 is_deeply $type->arrayrefint02, [qw(4 5 6)],
101 => 'Assignment and coercion is correct';
103 ok $type->arrayrefint02({a=>7,b=>8})
104 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
106 is_deeply $type->arrayrefint02, [qw(7 8)],
107 => 'Assignment and coercion is correct';
109 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
110 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
112 is_deeply $type->arrayrefint02, [qw(2 3 7)],
113 => 'Assignment and coercion is correct';
115 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
116 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
118 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
119 => 'Assignment and coercion is correct';
123 ok $type->arrayrefint03([qw(11 12 13)])
124 => 'Assigned arrayrefint01 qw(11 12 13)';
126 is_deeply $type->arrayrefint03, [qw(11 12 13)],
127 => 'Assignment is correct';
130 $type->arrayrefint03([qw(a b c)])
131 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
135 ok $type->StrOrArrayRef('string')
136 => 'String part of union is good';
138 ok $type->StrOrArrayRef([1,2,3])
139 => 'arrayref part of union is good';
142 $type->StrOrArrayRef({a=>111});
143 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
147 ok $type->AtLeastOneInt([1,2]),
148 => 'Good assignment';
150 is_deeply $type->AtLeastOneInt, [1,2]
151 => "Got expected values.";
154 $type->AtLeastOneInt([]);
155 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
158 $type->AtLeastOneInt(['a','b']);
159 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';