Whitespace
[gitmo/MooseX-Getopt.git] / t / 107_union_bug.t
CommitLineData
416dcb2e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::Most tests => 5;
7
8{
9 package example;
86fa042d 10
416dcb2e 11 use Moose;
12 use Moose::Util::TypeConstraints;
13 with qw(
14 MooseX::Getopt
15 );
86fa042d 16
17 subtype 'ResultSet'
416dcb2e 18 => as 'DBIx::Class::ResultSet';
86fa042d 19
20 subtype 'ResultList'
416dcb2e 21 => as 'ArrayRef[Int]';
22
23 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
24 'ResultList' => '=s',
25 );
86fa042d 26
27 coerce 'ResultList'
28 => from 'Str'
416dcb2e 29 => via {
30 return [ grep { m/^\d+$/ } split /\D/,$_ ]; # <- split string into arrayref
31 };
86fa042d 32
416dcb2e 33 has 'results' => (
34 is => 'rw',
35 isa => 'ResultList | ResultSet', # <- union constraint
36 coerce => 1,
37 );
86fa042d 38
416dcb2e 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');
86fa042d 61
416dcb2e 62 explain($example->results);
63 is($example->other,'test');
64 cmp_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
86fa042d 65}