Commit | Line | Data |
c2463b82 |
1 | #!/usr/bin/env perl |
2 | use warnings; |
3 | use strict; |
4 | |
cf1a8bfa |
5 | use Test::More tests => 29; |
a706b0f2 |
6 | use Test::Exception; |
c2463b82 |
7 | use FindBin; |
8 | use lib "$FindBin::Bin/lib"; |
c2463b82 |
9 | |
20b6a7d1 |
10 | { |
11 | package Test::MooseX::TypeLibrary::TypeDecorator; |
12 | |
13 | use Moose; |
a706b0f2 |
14 | use MooseX::Types::Moose qw( |
15 | Int |
16 | ); |
20b6a7d1 |
17 | use DecoratorLibrary qw( |
cf1a8bfa |
18 | MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef |
20b6a7d1 |
19 | ); |
20 | |
21 | has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1); |
22 | has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1); |
a706b0f2 |
23 | has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1); |
24 | has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]); |
cf1a8bfa |
25 | has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef); |
20b6a7d1 |
26 | } |
c2463b82 |
27 | |
20b6a7d1 |
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)], |
a706b0f2 |
42 | => 'Assignment is correct'; |
20b6a7d1 |
43 | |
44 | ok $type->arrayrefbase('d,e,f') |
a706b0f2 |
45 | => 'Assignment arrayrefbase d,e,f to test coercion'; |
20b6a7d1 |
46 | |
47 | is_deeply $type->arrayrefbase, [qw(d e f)], |
a706b0f2 |
48 | => 'Assignment and coercion is correct'; |
20b6a7d1 |
49 | |
50 | ## test arrayrefint01 normal and coercion |
51 | |
54f5d4e6 |
52 | ok $type->arrayrefint01([qw(1 2 3)]) |
a706b0f2 |
53 | => 'Assignment arrayrefint01 qw(1 2 3)'; |
20b6a7d1 |
54 | |
54f5d4e6 |
55 | is_deeply $type->arrayrefint01, [qw(1 2 3)], |
a706b0f2 |
56 | => 'Assignment is correct'; |
20b6a7d1 |
57 | |
54f5d4e6 |
58 | ok $type->arrayrefint01('4.5.6') |
a706b0f2 |
59 | => 'Assigned arrayrefint01 4.5.6 to test coercion from Str'; |
20b6a7d1 |
60 | |
54f5d4e6 |
61 | is_deeply $type->arrayrefint01, [qw(4 5 6)], |
a706b0f2 |
62 | => 'Assignment and coercion is correct'; |
20b6a7d1 |
63 | |
54f5d4e6 |
64 | ok $type->arrayrefint01({a=>7,b=>8}) |
a706b0f2 |
65 | => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef'; |
54f5d4e6 |
66 | |
67 | is_deeply $type->arrayrefint01, [qw(7 8)], |
a706b0f2 |
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'; |
54f5d4e6 |
113 | |
a706b0f2 |
114 | throws_ok sub { |
115 | $type->arrayrefint03([qw(a b c)]) |
cf1a8bfa |
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'; |