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