updated makefile since we need a newer Moose to solve some bugs
[gitmo/MooseX-Types.git] / t / 13_typedecorator.t
CommitLineData
c2463b82 1#!/usr/bin/env perl
2use warnings;
3use strict;
4
cbc4d0a2 5use Test::More tests => 36;
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(
cbc4d0a2 15 Int Str
a706b0f2 16 );
20b6a7d1 17 use DecoratorLibrary qw(
cf1a8bfa 18 MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
e088dd03 19 AtLeastOneInt
20b6a7d1 20 );
21
22 has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1);
23 has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1);
a706b0f2 24 has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1);
25 has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]);
cf1a8bfa 26 has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef);
e088dd03 27 has 'AtLeastOneInt' => (is=>'rw', isa=>AtLeastOneInt);
cbc4d0a2 28 has 'pipeoverloading' => (is=>'rw', isa=>Int|Str);
20b6a7d1 29}
c2463b82 30
20b6a7d1 31## Make sure we have a 'create object sanity check'
32
33ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
34 => 'Created some sort of object';
35
36isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
37 => "Yes, it's the correct kind of object";
cbc4d0a2 38
39## Test pipeoverloading
40
41ok $type->pipeoverloading(1)
42 => 'Integer for union test accepted';
43
44ok $type->pipeoverloading('a')
45 => 'String for union test accepted';
46
47throws_ok sub {
48 $type->pipeoverloading({a=>1,b=>2});
49}, qr/Validation failed for 'Int | Str'/ => 'Union test corrected fails a HashRef';
20b6a7d1 50
51## test arrayrefbase normal and coercion
52
53ok $type->arrayrefbase([qw(a b c)])
54 => 'Assigned arrayrefbase qw(a b c)';
55
56is_deeply $type->arrayrefbase, [qw(a b c)],
a706b0f2 57 => 'Assignment is correct';
20b6a7d1 58
59ok $type->arrayrefbase('d,e,f')
a706b0f2 60 => 'Assignment arrayrefbase d,e,f to test coercion';
20b6a7d1 61
62is_deeply $type->arrayrefbase, [qw(d e f)],
a706b0f2 63 => 'Assignment and coercion is correct';
20b6a7d1 64
65## test arrayrefint01 normal and coercion
66
54f5d4e6 67ok $type->arrayrefint01([qw(1 2 3)])
a706b0f2 68 => 'Assignment arrayrefint01 qw(1 2 3)';
20b6a7d1 69
54f5d4e6 70is_deeply $type->arrayrefint01, [qw(1 2 3)],
a706b0f2 71 => 'Assignment is correct';
20b6a7d1 72
54f5d4e6 73ok $type->arrayrefint01('4.5.6')
a706b0f2 74 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
20b6a7d1 75
54f5d4e6 76is_deeply $type->arrayrefint01, [qw(4 5 6)],
a706b0f2 77 => 'Assignment and coercion is correct';
20b6a7d1 78
54f5d4e6 79ok $type->arrayrefint01({a=>7,b=>8})
a706b0f2 80 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
54f5d4e6 81
82is_deeply $type->arrayrefint01, [qw(7 8)],
a706b0f2 83 => 'Assignment and coercion is correct';
84
85throws_ok sub {
86 $type->arrayrefint01([qw(a b c)])
87}, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
88
89## test arrayrefint02 normal and coercion
90
91ok $type->arrayrefint02([qw(1 2 3)])
92 => 'Assigned arrayrefint02 qw(1 2 3)';
93
94is_deeply $type->arrayrefint02, [qw(1 2 3)],
95 => 'Assignment is correct';
96
97ok $type->arrayrefint02('4:5:6')
98 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
99
100is_deeply $type->arrayrefint02, [qw(4 5 6)],
101 => 'Assignment and coercion is correct';
102
103ok $type->arrayrefint02({a=>7,b=>8})
104 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
105
106is_deeply $type->arrayrefint02, [qw(7 8)],
107 => 'Assignment and coercion is correct';
108
109ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
110 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
111
112is_deeply $type->arrayrefint02, [qw(2 3 7)],
113 => 'Assignment and coercion is correct';
114
115ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
116 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
117
118is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
119 => 'Assignment and coercion is correct';
120
121# test arrayrefint03
122
123ok $type->arrayrefint03([qw(11 12 13)])
124 => 'Assigned arrayrefint01 qw(11 12 13)';
125
126is_deeply $type->arrayrefint03, [qw(11 12 13)],
127 => 'Assignment is correct';
54f5d4e6 128
a706b0f2 129throws_ok sub {
130 $type->arrayrefint03([qw(a b c)])
cf1a8bfa 131}, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
132
133# TEST StrOrArrayRef
134
135ok $type->StrOrArrayRef('string')
136 => 'String part of union is good';
137
138ok $type->StrOrArrayRef([1,2,3])
139 => 'arrayref part of union is good';
140
141throws_ok sub {
142 $type->StrOrArrayRef({a=>111});
e088dd03 143}, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
144
145# Test AtLeastOneInt
146
147ok $type->AtLeastOneInt([1,2]),
148 => 'Good assignment';
149
150is_deeply $type->AtLeastOneInt, [1,2]
151 => "Got expected values.";
152
153throws_ok sub {
154 $type->AtLeastOneInt([]);
155}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
156
157throws_ok sub {
158 $type->AtLeastOneInt(['a','b']);
159}, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
160