finished parameterized method, tests and converted Moose::Util::TypeConstraints to...
[gitmo/Moose.git] / t / 040_type_constraints / 010_misc_type_tests.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 19;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose::Util::TypeConstraints');           
11 }
12
13 # subtype 'aliasing' ...
14
15 lives_ok {
16     subtype 'Numb3rs' => as 'Num';
17 } '... create bare subtype fine';
18
19 my $numb3rs = find_type_constraint('Numb3rs');
20 isa_ok($numb3rs, 'Moose::Meta::TypeConstraint');
21
22 # subtype with unions
23
24 {
25     package Test::Moose::Meta::TypeConstraint::Union;
26
27     use overload '""' => sub {'Broken|Test'}, fallback => 1;
28     use Moose;
29
30     extends 'Moose::Meta::TypeConstraint';
31 }
32
33 my $dummy_instance = Test::Moose::Meta::TypeConstraint::Union->new;
34
35 ok $dummy_instance => "Created Instance";
36
37 isa_ok $dummy_instance,
38     'Test::Moose::Meta::TypeConstraint::Union' => 'isa correct type';
39
40 is "$dummy_instance", "Broken|Test" =>
41     'Got expected stringification result';
42
43 my $subtype1 = subtype 'New1' => as $dummy_instance;
44
45 ok $subtype1 => 'made a subtype from our type object';
46
47 my $subtype2 = subtype 'New2' => as $subtype1;
48
49 ok $subtype2 => 'made a subtype of our subtype';
50
51 # testing the parameterize method
52
53 {
54         my $parameterizable = subtype 'parameterizable_hashref',
55                 as 'HashRef';
56                 
57         my $parameterized = subtype 'parameterized_hashref',
58                 as 'HashRef[Int]';
59                 
60         my $int = Moose::Util::TypeConstraints::find_type_constraint('Int');
61                 
62         my $from_parameterizable = $parameterizable->parameterize($int);
63         
64         isa_ok $parameterizable, 'Moose::Meta::TypeConstraint::Parameterizable',
65          => 'Got expected type instance';
66
67         package Test::Moose::Meta::TypeConstraint::Parameterizable;
68         use Moose;
69         
70         has parameterizable => (is=>'rw', isa=>$parameterizable);
71         has parameterized => (is=>'rw', isa=>$parameterized);
72         has from_parameterizable => (is=>'rw', isa=>$from_parameterizable);
73 }
74
75 # Create and check a dummy object
76
77 ok my $params = Test::Moose::Meta::TypeConstraint::Parameterizable->new()
78  => 'Create Dummy object for testing';
79  
80 isa_ok $params, 'Test::Moose::Meta::TypeConstraint::Parameterizable'
81  => 'isa correct type';
82
83 # test parameterizable
84
85 lives_ok sub {
86         $params->parameterizable({a=>'Hello', b=>'World'});
87 } => 'No problem setting parameterizable';
88
89 is_deeply $params->parameterizable, {a=>'Hello', b=>'World'}
90  => 'Got expected values';
91
92 # test parameterized
93
94 lives_ok sub {
95         $params->parameterized({a=>1, b=>2});
96 } => 'No problem setting parameterized';
97
98 is_deeply $params->parameterized, {a=>1, b=>2}
99  => 'Got expected values';
100  
101 throws_ok sub {
102         $params->parameterized({a=>'Hello', b=>'World'});
103 }, qr/Attribute \(parameterized\) does not pass the type constraint/
104  => 'parameterized throws expected error';
105
106 # test from_parameterizable
107
108 lives_ok sub {
109         $params->from_parameterizable({a=>1, b=>2});
110 } => 'No problem setting from_parameterizable';
111
112 is_deeply $params->from_parameterizable, {a=>1, b=>2}
113  => 'Got expected values';
114  
115 throws_ok sub {
116         $params->from_parameterizable({a=>'Hello', b=>'World'});
117 }, qr/Attribute \(from_parameterizable\) does not pass the type constraint/
118  => 'from_parameterizable throws expected error';
119  
120  
121  
122  
123  
124  
125