5 use Test::More tests => 29;
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
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);
28 ## Make sure we have a 'create object sanity check'
30 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
31 => 'Created some sort of object';
33 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
34 => "Yes, it's the correct kind of object";
36 ## test arrayrefbase normal and coercion
38 ok $type->arrayrefbase([qw(a b c)])
39 => 'Assigned arrayrefbase qw(a b c)';
41 is_deeply $type->arrayrefbase, [qw(a b c)],
42 => 'Assignment is correct';
44 ok $type->arrayrefbase('d,e,f')
45 => 'Assignment arrayrefbase d,e,f to test coercion';
47 is_deeply $type->arrayrefbase, [qw(d e f)],
48 => 'Assignment and coercion is correct';
50 ## test arrayrefint01 normal and coercion
52 ok $type->arrayrefint01([qw(1 2 3)])
53 => 'Assignment arrayrefint01 qw(1 2 3)';
55 is_deeply $type->arrayrefint01, [qw(1 2 3)],
56 => 'Assignment is correct';
58 ok $type->arrayrefint01('4.5.6')
59 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
61 is_deeply $type->arrayrefint01, [qw(4 5 6)],
62 => 'Assignment and coercion is correct';
64 ok $type->arrayrefint01({a=>7,b=>8})
65 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
67 is_deeply $type->arrayrefint01, [qw(7 8)],
68 => 'Assignment and coercion is correct';
71 $type->arrayrefint01([qw(a b c)])
72 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
74 ## test arrayrefint02 normal and coercion
76 ok $type->arrayrefint02([qw(1 2 3)])
77 => 'Assigned arrayrefint02 qw(1 2 3)';
79 is_deeply $type->arrayrefint02, [qw(1 2 3)],
80 => 'Assignment is correct';
82 ok $type->arrayrefint02('4:5:6')
83 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
85 is_deeply $type->arrayrefint02, [qw(4 5 6)],
86 => 'Assignment and coercion is correct';
88 ok $type->arrayrefint02({a=>7,b=>8})
89 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
91 is_deeply $type->arrayrefint02, [qw(7 8)],
92 => 'Assignment and coercion is correct';
94 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
95 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
97 is_deeply $type->arrayrefint02, [qw(2 3 7)],
98 => 'Assignment and coercion is correct';
100 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
101 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
103 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
104 => 'Assignment and coercion is correct';
108 ok $type->arrayrefint03([qw(11 12 13)])
109 => 'Assigned arrayrefint01 qw(11 12 13)';
111 is_deeply $type->arrayrefint03, [qw(11 12 13)],
112 => 'Assignment is correct';
115 $type->arrayrefint03([qw(a b c)])
116 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
120 ok $type->StrOrArrayRef('string')
121 => 'String part of union is good';
123 ok $type->StrOrArrayRef([1,2,3])
124 => 'arrayref part of union is good';
127 $type->StrOrArrayRef({a=>111});
128 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';