Commit | Line | Data |
a30fa891 |
1 | BEGIN { |
8dbdca20 |
2 | use strict; |
3 | use warnings; |
4 | use Test::More tests=>17; |
5 | use Test::Exception; |
a30fa891 |
6 | } |
7 | |
8 | { |
9 | package Test::MooseX::Meta::TypeConstraint::Structured::Dict; |
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 | use MooseX::Types -declare => [qw(MyString)]; |
15 | |
a30fa891 |
16 | subtype MyString, |
189dc572 |
17 | as Str, |
a30fa891 |
18 | where { $_=~m/abc/}; |
8dbdca20 |
19 | |
a30fa891 |
20 | has 'dict' => (is=>'rw', isa=>Dict[name=>Str, age=>Int]); |
8dbdca20 |
21 | has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>Str, age=>Maybe[Int]]); |
a30fa891 |
22 | has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>Str|Object, key2=>Tuple[Int,Str|Object]] ); |
23 | } |
24 | |
25 | ## Instantiate a new test object |
26 | |
27 | ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Dict->new |
28 | => 'Instantiated new Record test class.'; |
8dbdca20 |
29 | |
a30fa891 |
30 | isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Dict' |
31 | => 'Created correct object type.'; |
8dbdca20 |
32 | |
a30fa891 |
33 | # Test dict Dict[name=>Str, age=>Int] |
8dbdca20 |
34 | |
a30fa891 |
35 | lives_ok sub { |
36 | $record->dict({name=>'frith', age=>23}); |
37 | } => 'Set dict attribute without error'; |
38 | |
39 | is $record->dict->{name}, 'frith' |
40 | => 'correct set the dict attribute name'; |
41 | |
42 | is $record->dict->{age}, 23 |
43 | => 'correct set the dict attribute age'; |
8dbdca20 |
44 | |
a30fa891 |
45 | throws_ok sub { |
8dbdca20 |
46 | $record->dict({name=>[1,2,3], age=>'sdfsdfsd'}); |
a30fa891 |
47 | }, qr/Attribute \(dict\) does not pass the type constraint/ |
48 | => 'Got Expected Error for bad value in dict'; |
8dbdca20 |
49 | |
a30fa891 |
50 | ## Test dict_with_maybe |
51 | |
52 | lives_ok sub { |
53 | $record->dict_with_maybe({name=>'frith', age=>23}); |
54 | } => 'Set dict attribute without error'; |
55 | |
56 | is $record->dict_with_maybe->{name}, 'frith' |
57 | => 'correct set the dict attribute name'; |
58 | |
59 | is $record->dict_with_maybe->{age}, 23 |
60 | => 'correct set the dict attribute age'; |
8dbdca20 |
61 | |
a30fa891 |
62 | throws_ok sub { |
8dbdca20 |
63 | $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'}); |
a30fa891 |
64 | }, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/ |
65 | => 'Got Expected Error for bad value in dict'; |
66 | |
67 | throws_ok sub { |
8dbdca20 |
68 | $record->dict_with_maybe({age=>30}); |
a30fa891 |
69 | }, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/ |
70 | => 'Got Expected Error for missing named parameter'; |
71 | |
72 | lives_ok sub { |
73 | $record->dict_with_maybe({name=>'usal', age=>undef}); |
74 | } => 'Set dict attribute without error, skipping maybe'; |
75 | |
76 | ## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']] |
77 | |
78 | lives_ok sub { |
79 | $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']}); |
80 | } => 'Set tuple attribute without error'; |
81 | |
82 | throws_ok sub { |
83 | $record->dict_with_tuple_with_union({key1=>'Hello', key2=>['World',2]}); |
84 | }, qr/Attribute \(dict_with_tuple_with_union\) does not pass the type constraint/ |
85 | => 'Threw error on bad constraint'; |
8dbdca20 |
86 | |
a30fa891 |
87 | lives_ok sub { |
88 | $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']}); |
89 | } => 'Set tuple attribute without error'; |
90 | |
91 | lives_ok sub { |
92 | $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]}); |
93 | } => 'Set tuple attribute without error'; |
94 | |
95 | throws_ok sub { |
96 | $record->dict_with_tuple_with_union({key1=>1, key2=>['World',2]}); |
97 | }, qr/Attribute \(dict_with_tuple_with_union\) does not pass the type constraint/ |
189dc572 |
98 | => 'Threw error on bad constraint'; |