work in progress, tests are failing, and parameterized role is not flexible enough...
[gitmo/MooseX-Getopt.git] / t / 107_union_bug.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
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     is_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
53 }
54
55 # With MooseX::Getopt
56 {
57     local @ARGV = ('--results','1234,5678,9012','--other','test');
58     my $example = example->new_with_options;
59     isa_ok($example, 'example');
60
61     is($example->other,'test');
62     is_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
63 }
64
65 done_testing;
66