merged
[gitmo/MooseX-Types-Structured.git] / t / 04-combined.t
CommitLineData
a30fa891 1BEGIN {
8dbdca20 2 use strict;
3 use warnings;
4 use Test::More tests=>9;
25c705c4 5 use Test::Fatal;
a30fa891 6}
7
8{
9 package Test::MooseX::Meta::TypeConstraint::Structured::Combined;
10
11 use Moose;
12 use MooseX::Types::Structured qw(Dict Tuple);
8dbdca20 13 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe);
14
15 has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>Str, key2=>Tuple[Int,Str]]);
a30fa891 16 has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>Str|Object, key2=>Tuple[Int,Str|Object]] );
17}
18
19## Instantiate a new test object
20
21ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Combined->new
22 => 'Instantiated new Record test class.';
8dbdca20 23
a30fa891 24isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Combined'
25 => 'Created correct object type.';
8dbdca20 26
a30fa891 27## Test dict_with_tuple
28
25c705c4 29is( exception {
a30fa891 30 $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
25c705c4 31} => undef, 'Set tuple attribute without error');
a30fa891 32
25c705c4 33like( exception {
a30fa891 34 $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
35}, qr/Attribute \(dict_with_tuple\) does not pass the type constraint/
25c705c4 36 => 'Threw error on bad constraint');
8dbdca20 37
a30fa891 38## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']]
39
25c705c4 40is( exception {
a30fa891 41 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']});
25c705c4 42} => undef, 'Set tuple attribute without error');
a30fa891 43
25c705c4 44like( exception {
a30fa891 45 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>['World',2]});
46}, qr/Attribute \(dict_with_tuple_with_union\) does not pass the type constraint/
25c705c4 47 => 'Threw error on bad constraint');
8dbdca20 48
25c705c4 49is( exception {
a30fa891 50 $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']});
25c705c4 51} => undef, 'Set tuple attribute without error');
a30fa891 52
25c705c4 53is( exception {
a30fa891 54 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]});
25c705c4 55} => undef, 'Set tuple attribute without error');
a30fa891 56
25c705c4 57like( exception {
a30fa891 58 $record->dict_with_tuple_with_union({key1=>1, key2=>['World',2]});
59}, qr/Attribute \(dict_with_tuple_with_union\) does not pass the type constraint/
25c705c4 60 => 'Threw error on bad constraint');