MooseX-TypeLibrary with tests and first pod (phaylon)
[gitmo/MooseX-Types.git] / t / 11_library-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 TestLibrary ':all';
9
10 my @tests = (
11     [ 'NonEmptyStr', 12, "12", [], "foobar", "" ],
12     [ 'IntArrayRef', 12, [12], {}, [17, 23], {} ],
13 );
14
15 plan tests => (@tests * 8) + 1;
16
17 # new array ref so we can safely shift from it
18 for my $data (map { [@$_] } @tests) {
19     my $type = shift @$data;
20
21     # Type name export
22     {
23         ok my $code = __PACKAGE__->can($type), "$type() was exported";
24         is $code->(), "TestLibrary::$type", "$type() returned correct type name";
25     }
26
27     # coercion handler export
28     {   
29         my ($coerce, $coercion_result, $cannot_coerce) = map { shift @$data } 1 .. 3;
30         ok my $code = __PACKAGE__->can("to_$type"), "to_$type() coercion was exported";
31         is_deeply scalar $code->($coerce), $coercion_result, "to_$type() coercion works";
32         ok ! $code->($cannot_coerce), "to_$type() returns false on invalid value";
33     }
34
35     # type test handler
36     {
37         my ($valid, $invalid) = map { shift @$data } 1 .. 2;
38         ok my $code = __PACKAGE__->can("is_$type"), "is_$type() check was exported";
39         ok $code->($valid), "is_$type() check true on valid value";
40         ok ! $code->($invalid), "is_$type() check false on invalid value";
41     }
42 }
43
44 # coercion not available
45 ok ! __PACKAGE__->can('to_TwentyThree'), "type without coercion doesn't have to_* helper";