5 use Test::More tests => 33;
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);
30 ## Make sure we have a 'create object sanity check'
32 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
33 => 'Created some sort of object';
35 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
36 => "Yes, it's the correct kind of object";
38 ## test arrayrefbase normal and coercion
40 ok $type->arrayrefbase([qw(a b c)])
41 => 'Assigned arrayrefbase qw(a b c)';
43 is_deeply $type->arrayrefbase, [qw(a b c)],
44 => 'Assignment is correct';
46 ok $type->arrayrefbase('d,e,f')
47 => 'Assignment arrayrefbase d,e,f to test coercion';
49 is_deeply $type->arrayrefbase, [qw(d e f)],
50 => 'Assignment and coercion is correct';
52 ## test arrayrefint01 normal and coercion
54 ok $type->arrayrefint01([qw(1 2 3)])
55 => 'Assignment arrayrefint01 qw(1 2 3)';
57 is_deeply $type->arrayrefint01, [qw(1 2 3)],
58 => 'Assignment is correct';
60 ok $type->arrayrefint01('4.5.6')
61 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
63 is_deeply $type->arrayrefint01, [qw(4 5 6)],
64 => 'Assignment and coercion is correct';
66 ok $type->arrayrefint01({a=>7,b=>8})
67 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
69 is_deeply $type->arrayrefint01, [qw(7 8)],
70 => 'Assignment and coercion is correct';
73 $type->arrayrefint01([qw(a b c)])
74 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
76 ## test arrayrefint02 normal and coercion
78 ok $type->arrayrefint02([qw(1 2 3)])
79 => 'Assigned arrayrefint02 qw(1 2 3)';
81 is_deeply $type->arrayrefint02, [qw(1 2 3)],
82 => 'Assignment is correct';
84 ok $type->arrayrefint02('4:5:6')
85 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
87 is_deeply $type->arrayrefint02, [qw(4 5 6)],
88 => 'Assignment and coercion is correct';
90 ok $type->arrayrefint02({a=>7,b=>8})
91 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
93 is_deeply $type->arrayrefint02, [qw(7 8)],
94 => 'Assignment and coercion is correct';
96 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
97 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
99 is_deeply $type->arrayrefint02, [qw(2 3 7)],
100 => 'Assignment and coercion is correct';
102 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
103 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
105 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
106 => 'Assignment and coercion is correct';
110 ok $type->arrayrefint03([qw(11 12 13)])
111 => 'Assigned arrayrefint01 qw(11 12 13)';
113 is_deeply $type->arrayrefint03, [qw(11 12 13)],
114 => 'Assignment is correct';
117 $type->arrayrefint03([qw(a b c)])
118 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
122 ok $type->StrOrArrayRef('string')
123 => 'String part of union is good';
125 ok $type->StrOrArrayRef([1,2,3])
126 => 'arrayref part of union is good';
129 $type->StrOrArrayRef({a=>111});
130 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
134 ok $type->AtLeastOneInt([1,2]),
135 => 'Good assignment';
137 is_deeply $type->AtLeastOneInt, [1,2]
138 => "Got expected values.";
141 $type->AtLeastOneInt([]);
142 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
145 $type->AtLeastOneInt(['a','b']);
146 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';