Commit | Line | Data |
182134e8 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
182134e8 |
8 | |
9 | BEGIN { |
46fdd900 |
10 | use_ok('Moose::Util::TypeConstraints'); |
182134e8 |
11 | } |
12 | |
13 | { |
14 | package HTTPHeader; |
182134e8 |
15 | use Moose; |
d03bd989 |
16 | |
182134e8 |
17 | has 'array' => (is => 'ro'); |
d03bd989 |
18 | has 'hash' => (is => 'ro'); |
182134e8 |
19 | } |
20 | |
d03bd989 |
21 | subtype Header => |
22 | => as Object |
182134e8 |
23 | => where { $_->isa('HTTPHeader') }; |
d03bd989 |
24 | |
25 | coerce Header |
26 | => from ArrayRef |
d6e2d9a1 |
27 | => via { HTTPHeader->new(array => $_[0]) } |
d03bd989 |
28 | => from HashRef |
d6e2d9a1 |
29 | => via { HTTPHeader->new(hash => $_[0]) }; |
81dc201f |
30 | |
d03bd989 |
31 | |
32 | Moose::Util::TypeConstraints->export_type_constraints_as_functions(); |
33 | |
182134e8 |
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 | |
470b518a |
41 | my $anon_type = subtype Object => where { $_->isa('HTTPHeader') }; |
182134e8 |
42 | |
b10dde3a |
43 | is( exception { |
470b518a |
44 | coerce $anon_type |
d03bd989 |
45 | => from ArrayRef |
470b518a |
46 | => via { HTTPHeader->new(array => $_[0]) } |
d03bd989 |
47 | => from HashRef |
470b518a |
48 | => via { HTTPHeader->new(hash => $_[0]) }; |
b10dde3a |
49 | }, undef, 'coercion of anonymous subtype succeeds' ); |
e90c03d0 |
50 | |
470b518a |
51 | foreach my $coercion ( |
52 | find_type_constraint('Header')->coercion, |
53 | $anon_type->coercion |
54 | ) { |
e90c03d0 |
55 | |
470b518a |
56 | isa_ok($coercion, 'Moose::Meta::TypeCoercion'); |
d03bd989 |
57 | |
470b518a |
58 | { |
59 | my $coerced = $coercion->coerce([ 1, 2, 3 ]); |
60 | isa_ok($coerced, 'HTTPHeader'); |
d03bd989 |
61 | |
470b518a |
62 | is_deeply( |
63 | $coerced->array(), |
64 | [ 1, 2, 3 ], |
65 | '... got the right array'); |
d03bd989 |
66 | is($coerced->hash(), undef, '... nothing assigned to the hash'); |
470b518a |
67 | } |
d03bd989 |
68 | |
470b518a |
69 | { |
70 | my $coerced = $coercion->coerce({ one => 1, two => 2, three => 3 }); |
71 | isa_ok($coerced, 'HTTPHeader'); |
d03bd989 |
72 | |
470b518a |
73 | is_deeply( |
74 | $coerced->hash(), |
75 | { one => 1, two => 2, three => 3 }, |
76 | '... got the right hash'); |
d03bd989 |
77 | is($coerced->array(), undef, '... nothing assigned to the array'); |
470b518a |
78 | } |
d03bd989 |
79 | |
470b518a |
80 | { |
81 | my $scalar_ref = \(my $var); |
82 | my $coerced = $coercion->coerce($scalar_ref); |
83 | is($coerced, $scalar_ref, '... got back what we put in'); |
84 | } |
d03bd989 |
85 | |
470b518a |
86 | { |
87 | my $coerced = $coercion->coerce("Foo"); |
88 | is($coerced, "Foo", '... got back what we put in'); |
89 | } |
e90c03d0 |
90 | } |
88071df7 |
91 | |
a232d913 |
92 | subtype 'StrWithTrailingX' |
93 | => as 'Str' |
94 | => where { /X$/ }; |
88071df7 |
95 | |
a232d913 |
96 | coerce 'StrWithTrailingX' |
97 | => from 'Str' |
98 | => via { $_ . 'X' }; |
88071df7 |
99 | |
a232d913 |
100 | my $tc = find_type_constraint('StrWithTrailingX'); |
101 | is($tc->coerce("foo"), "fooX", "coerce when needed"); |
102 | is($tc->coerce("fooX"), "fooX", "do not coerce when unneeded"); |
a28e50e4 |
103 | |
104 | done_testing; |