overloading for union constraints, more tests, some code cleanup
[gitmo/MooseX-Types.git] / t / 13_typedecorator.t
CommitLineData
c2463b82 1#!/usr/bin/env perl
2use warnings;
3use strict;
4
cf1a8bfa 5use Test::More tests => 29;
a706b0f2 6use Test::Exception;
c2463b82 7use FindBin;
8use 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
30ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
31 => 'Created some sort of object';
32
33isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
34 => "Yes, it's the correct kind of object";
35
36## test arrayrefbase normal and coercion
37
38ok $type->arrayrefbase([qw(a b c)])
39 => 'Assigned arrayrefbase qw(a b c)';
40
41is_deeply $type->arrayrefbase, [qw(a b c)],
a706b0f2 42 => 'Assignment is correct';
20b6a7d1 43
44ok $type->arrayrefbase('d,e,f')
a706b0f2 45 => 'Assignment arrayrefbase d,e,f to test coercion';
20b6a7d1 46
47is_deeply $type->arrayrefbase, [qw(d e f)],
a706b0f2 48 => 'Assignment and coercion is correct';
20b6a7d1 49
50## test arrayrefint01 normal and coercion
51
54f5d4e6 52ok $type->arrayrefint01([qw(1 2 3)])
a706b0f2 53 => 'Assignment arrayrefint01 qw(1 2 3)';
20b6a7d1 54
54f5d4e6 55is_deeply $type->arrayrefint01, [qw(1 2 3)],
a706b0f2 56 => 'Assignment is correct';
20b6a7d1 57
54f5d4e6 58ok $type->arrayrefint01('4.5.6')
a706b0f2 59 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
20b6a7d1 60
54f5d4e6 61is_deeply $type->arrayrefint01, [qw(4 5 6)],
a706b0f2 62 => 'Assignment and coercion is correct';
20b6a7d1 63
54f5d4e6 64ok $type->arrayrefint01({a=>7,b=>8})
a706b0f2 65 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
54f5d4e6 66
67is_deeply $type->arrayrefint01, [qw(7 8)],
a706b0f2 68 => 'Assignment and coercion is correct';
69
70throws_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
76ok $type->arrayrefint02([qw(1 2 3)])
77 => 'Assigned arrayrefint02 qw(1 2 3)';
78
79is_deeply $type->arrayrefint02, [qw(1 2 3)],
80 => 'Assignment is correct';
81
82ok $type->arrayrefint02('4:5:6')
83 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
84
85is_deeply $type->arrayrefint02, [qw(4 5 6)],
86 => 'Assignment and coercion is correct';
87
88ok $type->arrayrefint02({a=>7,b=>8})
89 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
90
91is_deeply $type->arrayrefint02, [qw(7 8)],
92 => 'Assignment and coercion is correct';
93
94ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
95 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
96
97is_deeply $type->arrayrefint02, [qw(2 3 7)],
98 => 'Assignment and coercion is correct';
99
100ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
101 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
102
103is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
104 => 'Assignment and coercion is correct';
105
106# test arrayrefint03
107
108ok $type->arrayrefint03([qw(11 12 13)])
109 => 'Assigned arrayrefint01 qw(11 12 13)';
110
111is_deeply $type->arrayrefint03, [qw(11 12 13)],
112 => 'Assignment is correct';
54f5d4e6 113
a706b0f2 114throws_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
120ok $type->StrOrArrayRef('string')
121 => 'String part of union is good';
122
123ok $type->StrOrArrayRef([1,2,3])
124 => 'arrayref part of union is good';
125
126throws_ok sub {
127 $type->StrOrArrayRef({a=>111});
128}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';