653f0123195672094f579a102584dfae94340484
[gitmo/MooseX-Getopt.git] / t / 107_union_bug.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More tests => 6;
5 use Test::NoWarnings 1.04 ':early';
6
7 {
8     package example;
9
10     use Moose;
11     use Moose::Util::TypeConstraints;
12     with qw(
13         MooseX::Getopt
14     );
15
16     subtype 'ResultSet'
17         => as 'DBIx::Class::ResultSet';
18
19     subtype 'ResultList'
20         => as 'ArrayRef[Int]';
21
22     MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
23             'ResultList'  => '=s',
24     );
25
26     coerce 'ResultList'
27         => from 'Str'
28         => via {
29             return [ grep { m/^\d+$/ } split /\D/,$_ ]; # <- split string into arrayref
30         };
31
32     has 'results' => (
33         is              => 'rw',
34         isa             => 'ResultList | ResultSet', # <- union constraint
35         coerce          => 1,
36     );
37
38     has 'other' => (
39         is              => 'rw',
40         isa             => 'Str',
41     );
42 }
43
44 # Without MooseX::Getopt
45 {
46     my $example = example->new({
47         results => '1234,5678,9012',
48         other   => 'test',
49     });
50     isa_ok($example, 'example');
51     is_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
52 }
53
54 # With MooseX::Getopt
55 {
56     local @ARGV = ('--results','1234,5678,9012','--other','test');
57     my $example = example->new_with_options;
58     isa_ok($example, 'example');
59
60     is($example->other,'test');
61     is_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
62 }
63