4 use Test::More tests=>12;
9 package Test::MooseX::Meta::TypeConstraint::Structured;
12 use Moose::Util::TypeConstraints;
13 use MooseX::Meta::TypeConstraint::Structured;
20 my @args = @{shift @_};
21 return MooseX::Meta::TypeConstraint::Structured->new(
23 parent => find_type_constraint('ArrayRef'),
24 package_defined_in => __PACKAGE__,
25 signature => [map {find_type_constraint($_)} @args],
30 my %args = @{shift @_};
31 return MooseX::Meta::TypeConstraint::Structured->new(
33 parent => find_type_constraint('HashRef'),
34 package_defined_in => __PACKAGE__,
35 signature => {map { $_ => find_type_constraint($args{$_})} keys %args},
39 has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str', 'MyString']);
40 has 'dict' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Int']);
43 ## Instantiate a new test object
45 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
46 => 'Instantiated new Record test class.';
48 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
49 => 'Created correct object type.';
51 ## Test Tuple type constraint
54 $record->tuple([1,'hello', 'test.abc.test']);
55 } => 'Set tuple attribute without error';
57 is $record->tuple->[0], 1
58 => 'correct set the tuple attribute index 0';
60 is $record->tuple->[1], 'hello'
61 => 'correct set the tuple attribute index 1';
63 is $record->tuple->[2], 'test.abc.test'
64 => 'correct set the tuple attribute index 2';
67 $record->tuple([1,'hello', 'test.xxx.test']);
68 }, qr/Validation failed for 'MyString'/
69 => 'Properly failed for bad value in custom type constraint';
72 $record->tuple(['asdasd',2, 'test.abc.test']);
73 }, qr/Validation failed for 'Int'/
74 => 'Got Expected Error for violating constraints';
76 ## Test the Dictionary type constraint
79 $record->dict({name=>'frith', age=>23});
80 } => 'Set dict attribute without error';
82 is $record->dict->{name}, 'frith'
83 => 'correct set the dict attribute name';
85 is $record->dict->{age}, 23
86 => 'correct set the dict attribute age';
89 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
90 }, qr/Validation failed for 'Str'/
91 => 'Got Expected Error for bad value in dict';