Move the override logic into Method::Override
[gitmo/Moose.git] / t / 040_type_constraints / 005_util_type_coercion.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 14;
7 use Test::Exception;
8
9 BEGIN {
10         use_ok('Moose::Util::TypeConstraints');
11 }
12
13 {
14     package HTTPHeader;
15     use Moose;
16     
17     has 'array' => (is => 'ro');
18     has 'hash'  => (is => 'ro');    
19 }
20
21 subtype Header => 
22     => as Object 
23     => where { $_->isa('HTTPHeader') };
24     
25 coerce Header 
26     => from ArrayRef 
27         => via { HTTPHeader->new(array => $_[0]) }
28     => from HashRef 
29         => via { HTTPHeader->new(hash => $_[0]) };
30
31         
32 Moose::Util::TypeConstraints->export_type_constraints_as_functions();        
33         
34 my $header = HTTPHeader->new();
35 isa_ok($header, 'HTTPHeader');
36
37 ok(Header($header), '... this passed the type test');
38 ok(!Header([]), '... this did not pass the type test');
39 ok(!Header({}), '... this did not pass the type test');
40
41 my $coercion = find_type_constraint('Header')->coercion;
42 isa_ok($coercion, 'Moose::Meta::TypeCoercion');
43
44 {
45     my $coerced = $coercion->coerce([ 1, 2, 3 ]);
46     isa_ok($coerced, 'HTTPHeader');
47
48     is_deeply(
49         $coerced->array(),
50         [ 1, 2, 3 ],
51         '... got the right array');
52     is($coerced->hash(), undef, '... nothing assigned to the hash');        
53 }
54
55 {
56     my $coerced = $coercion->coerce({ one => 1, two => 2, three => 3 });
57     isa_ok($coerced, 'HTTPHeader');
58     
59     is_deeply(
60         $coerced->hash(),
61         { one => 1, two => 2, three => 3 },
62         '... got the right hash');
63     is($coerced->array(), undef, '... nothing assigned to the array');        
64 }
65
66 {
67     my $scalar_ref = \(my $var);
68     my $coerced = $coercion->coerce($scalar_ref);
69     is($coerced, $scalar_ref, '... got back what we put in');
70 }
71
72 {
73     my $coerced = $coercion->coerce("Foo");
74     is($coerced, "Foo", '... got back what we put in');
75 }
76