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