convert all uses of Test::Exception to Test::Fatal.
[gitmo/MooseX-Types-Structured.git] / t / 03-dict.t
CommitLineData
a30fa891 1BEGIN {
8dbdca20 2 use strict;
3 use warnings;
4 use Test::More tests=>17;
25c705c4 5 use Test::Fatal;
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
27ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Dict->new
28 => 'Instantiated new Record test class.';
8dbdca20 29
a30fa891 30isa_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
25c705c4 35is( exception {
a30fa891 36 $record->dict({name=>'frith', age=>23});
25c705c4 37} => undef, 'Set dict attribute without error');
a30fa891 38
39is $record->dict->{name}, 'frith'
40 => 'correct set the dict attribute name';
41
42is $record->dict->{age}, 23
43 => 'correct set the dict attribute age';
8dbdca20 44
25c705c4 45like( exception {
8dbdca20 46 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
a30fa891 47}, qr/Attribute \(dict\) does not pass the type constraint/
25c705c4 48 => 'Got Expected Error for bad value in dict');
8dbdca20 49
a30fa891 50## Test dict_with_maybe
51
25c705c4 52is( exception {
a30fa891 53 $record->dict_with_maybe({name=>'frith', age=>23});
25c705c4 54} => undef, 'Set dict attribute without error');
a30fa891 55
56is $record->dict_with_maybe->{name}, 'frith'
57 => 'correct set the dict attribute name';
58
59is $record->dict_with_maybe->{age}, 23
60 => 'correct set the dict attribute age';
8dbdca20 61
25c705c4 62like( exception {
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/
25c705c4 65 => 'Got Expected Error for bad value in dict');
a30fa891 66
25c705c4 67like( exception {
8dbdca20 68 $record->dict_with_maybe({age=>30});
a30fa891 69}, qr/Attribute \(dict_with_maybe\) does not pass the type constraint/
25c705c4 70 => 'Got Expected Error for missing named parameter');
a30fa891 71
25c705c4 72is( exception {
a30fa891 73 $record->dict_with_maybe({name=>'usal', age=>undef});
25c705c4 74} => undef, 'Set dict attribute without error, skipping maybe');
a30fa891 75
76## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']]
77
25c705c4 78is( exception {
a30fa891 79 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']});
25c705c4 80} => undef, 'Set tuple attribute without error');
a30fa891 81
25c705c4 82like( exception {
a30fa891 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/
25c705c4 85 => 'Threw error on bad constraint');
8dbdca20 86
25c705c4 87is( exception {
a30fa891 88 $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']});
25c705c4 89} => undef, 'Set tuple attribute without error');
a30fa891 90
25c705c4 91is( exception {
a30fa891 92 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]});
25c705c4 93} => undef, 'Set tuple attribute without error');
a30fa891 94
25c705c4 95like( exception {
a30fa891 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/
25c705c4 98 => 'Threw error on bad constraint');