f2d0f772e1282904fab50225ca52d063922cc855
[gitmo/MooseX-Types.git] / t / 12_wrapper-definition.t
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4
5 use Test::More;
6 use FindBin;
7 use lib "$FindBin::Bin/lib";
8 use Moose::Util::TypeConstraints;
9 BEGIN { coerce 'Str', from 'Int', via { "$_" } }
10 use TestWrapper TestLibrary => [qw( NonEmptyStr IntArrayRef )],
11                 Moose       => [qw( Str Int )];
12
13
14 my @tests = (
15     [ 'NonEmptyStr', 'TestLibrary::NonEmptyStr', 12, "12", [], "foobar", "" ],
16     [ 'IntArrayRef', 'TestLibrary::IntArrayRef', 12, [12], {}, [17, 23], {} ],
17     [ 'Str',         'Str',                      12, "12", [], "foo", [777] ],
18 );
19
20 plan tests => (@tests * 9);
21
22 # new array ref so we can safely shift from it
23 for my $data (map { [@$_] } @tests) {
24     my $type = shift @$data;
25     my $full = shift @$data;
26
27     # Type name export
28     {
29         ok my $code = __PACKAGE__->can($type), "$type() was exported";
30         is $code->(), $full, "$type() returned correct type name";
31     }
32
33     # coercion handler export
34     {   
35         my ($coerce, $coercion_result, $cannot_coerce) = map { shift @$data } 1 .. 3;
36         ok my $code = __PACKAGE__->can("to_$type"), "to_$type() coercion was exported";
37         is_deeply scalar $code->($coerce), $coercion_result, "to_$type() coercion works";
38         eval { $code->($cannot_coerce) };
39         is $@, "coercion returned undef\n", "to_$type() died on invalid value";
40     }
41
42     # type test handler
43     {
44         my ($valid, $invalid) = map { shift @$data } 1 .. 2;
45         ok my $code = __PACKAGE__->can("is_$type"), "is_$type() check was exported";
46         ok $code->($valid), "is_$type() check true on valid value";
47         ok ! $code->($invalid), "is_$type() check false on invalid value";
48         is ref($code->()), 'CODE', "is_$type() returns test closure without args";
49     }
50 }
51
52