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