Commit | Line | Data |
a30fa891 |
1 | BEGIN { |
8dbdca20 |
2 | use strict; |
3 | use warnings; |
4 | use Test::More tests=>9; |
5 | use Test::Exception; |
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 | |
21 | ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Combined->new |
22 | => 'Instantiated new Record test class.'; |
8dbdca20 |
23 | |
a30fa891 |
24 | isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Combined' |
25 | => 'Created correct object type.'; |
8dbdca20 |
26 | |
a30fa891 |
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'; |
8dbdca20 |
37 | |
a30fa891 |
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'; |
8dbdca20 |
48 | |
a30fa891 |
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'; |