first guess at structured types, with proof of concept and first shot at the type...
[gitmo/MooseX-Types-Structured.git] / t / positional.t
1 BEGIN {
2         use strict;
3         use warnings;
4         use Test::More tests=>6;
5         use Test::Exception;
6 }
7
8 {
9     package Test::MooseX::Meta::TypeConstraint::Structured::Positional;
10
11     use Moose;
12     use Moose::Util::TypeConstraints;
13     use MooseX::Meta::TypeConstraint::Structured;
14       
15     sub Tuple {
16         my $args = shift @_;
17         return MooseX::Meta::TypeConstraint::Structured->new(
18             name => 'Tuple',
19             parent => find_type_constraint('ArrayRef'),
20             package_defined_in => __PACKAGE__,
21             signature => [map {find_type_constraint($_)} @$args],
22         );
23     }
24      
25     has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str']);
26 }
27
28 ## Instantiate a new test object
29
30 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Positional->new
31  => 'Instantiated new Record test class.';
32  
33 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Positional'
34  => 'Created correct object type.';
35
36 lives_ok sub {
37     $record->tuple([1,'hello']);
38 } => 'Set tuple attribute without error';
39
40 is $record->tuple->[0], 1
41  => 'correct set the tuple attribute index 0';
42
43 is $record->tuple->[1], 'hello'
44  => 'correct set the tuple attribute index 1';
45
46 throws_ok sub {
47     $record->tuple(['asdasd',2]);      
48 }, qr/Validation failed for 'Int'/
49  => 'Got Expected Error for violating constraints';
50