2d2389b8fd277b248b1d1f052e4930e1fe1f1b30
[gitmo/MooseX-Types-Structured.git] / t / 04-combined.t
1 BEGIN {
2     use strict;
3     use warnings;
4     use Test::More tests=>9;
5     use Test::Exception;
6 }
7
8 {
9     package Test::MooseX::Meta::TypeConstraint::Structured::Combined;
10
11     use Moose;
12     use MooseX::Types::Structured qw(Dict Tuple);
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]]);
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
21 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Combined->new
22  => 'Instantiated new Record test class.';
23
24 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Combined'
25  => 'Created correct object type.';
26
27 ## Test dict_with_tuple
28
29 lives_ok sub {
30     $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
31 } => 'Set tuple attribute without error';
32
33 throws_ok sub {
34     $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
35 }, qr/Attribute \(dict_with_tuple\) does not pass the type constraint/
36  => 'Threw error on bad constraint';
37
38 ## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']]
39
40 lives_ok sub {
41     $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']});
42 } => 'Set tuple attribute without error';
43
44 throws_ok sub {
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/
47  => 'Threw error on bad constraint';
48
49 lives_ok sub {
50     $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']});
51 } => 'Set tuple attribute without error';
52
53 lives_ok sub {
54     $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]});
55 } => 'Set tuple attribute without error';
56
57 throws_ok sub {
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/
60  => 'Threw error on bad constraint';