45e735105839266c36c1c36b0d0428d433e3e9cc
[gitmo/MooseX-Types-Structured.git] / t / 01-basic.t
1 BEGIN {
2         use strict;
3         use warnings;
4         use Test::More tests=>10;
5         use Test::Exception;
6         
7         use_ok 'Moose::Util::TypeConstraints';
8         use_ok 'MooseX::Meta::TypeConstraint::Structured::Structurable';
9         use_ok 'MooseX::Meta::TypeConstraint::Structured::Positional';
10         use_ok 'MooseX::Meta::TypeConstraint::Structured::Named';
11 }
12
13 my $tuple = MooseX::Meta::TypeConstraint::Structured::Structurable->new(
14                 name => 'Tuple',
15                 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Positional',
16                 package_defined_in => __PACKAGE__,
17                 parent => find_type_constraint('ArrayRef'),
18         );
19
20 Moose::Util::TypeConstraints::register_type_constraint($tuple);
21
22 ## Make sure the new type constraints have been registered
23
24 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple')
25  => 'Found the Tuple Type';
26
27 {
28         package Test::MooseX::Types::Structured::Positionable;
29         
30         use Moose;
31         use Moose::Util::TypeConstraints;
32         
33         has 'tuple' => (is=>'rw', isa=>'Tuple[Int,Str,Int]');
34 }
35
36
37 ok my $positioned_obj = Test::MooseX::Types::Structured::Positionable->new,
38  => 'Got a good object';
39
40 ok $positioned_obj->tuple([1,'hello',3])
41  => "[1,'hello',3] properly suceeds";
42
43 throws_ok sub {
44         $positioned_obj->tuple([1,2,'world']);
45 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
46
47 throws_ok sub {
48         $positioned_obj->tuple(['hello1',2,3]);
49 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
50
51 throws_ok sub {
52         $positioned_obj->tuple(['hello2',2,'world']);
53 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
54
55
56
57
58 #ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')
59 # => 'detected correctly';
60  
61 #is_deeply 
62 #       [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')],
63 #       ["HashRef", "key1", "Int", "key2", "Int", "key3", "ArrayRef[Int]"]
64 # => 'Correctly parsed HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]';