4 use Test::More tests=>17;
9 package Test::MooseX::Meta::TypeConstraint::Structured::Dict;
12 use MooseX::Types::Structured qw(Dict Tuple);
13 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe);
14 use MooseX::Types -declare => [qw(MyString)];
20 has 'dict' => (is=>'rw', isa=>Dict[name=>Str, age=>Int]);
21 has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>Str, age=>Maybe[Int]]);
22 has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>Str|Object, key2=>Tuple[Int,Str|Object]] );
25 ## Instantiate a new test object
27 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Dict->new
28 => 'Instantiated new Record test class.';
30 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Dict'
31 => 'Created correct object type.';
33 # Test dict Dict[name=>Str, age=>Int]
36 $record->dict({name=>'frith', age=>23});
37 } => 'Set dict attribute without error';
39 is $record->dict->{name}, 'frith'
40 => 'correct set the dict attribute name';
42 is $record->dict->{age}, 23
43 => 'correct set the dict attribute age';
46 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
47 }, qr/Attribute \(dict\) does not pass the type constraint/
48 => 'Got Expected Error for bad value in dict';
50 ## Test dict_with_maybe
53 $record->dict_with_maybe({name=>'frith', age=>23});
54 } => 'Set dict attribute without error';
56 is $record->dict_with_maybe->{name}, 'frith'
57 => 'correct set the dict attribute name';
59 is $record->dict_with_maybe->{age}, 23
60 => 'correct set the dict attribute age';
63 $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'});
64 }, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/
65 => 'Got Expected Error for bad value in dict';
68 $record->dict_with_maybe({age=>30});
69 }, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/
70 => 'Got Expected Error for missing named parameter';
73 $record->dict_with_maybe({name=>'usal', age=>undef});
74 } => 'Set dict attribute without error, skipping maybe';
76 ## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']]
79 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']});
80 } => 'Set tuple attribute without error';
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';
88 $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']});
89 } => 'Set tuple attribute without error';
92 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]});
93 } => 'Set tuple attribute without error';
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/
98 => 'Threw error on bad constraint';