File: 107_union_bug.t

package info (click to toggle)
libmoosex-getopt-perl 0.45-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 356 kB
  • sloc: perl: 1,964; makefile: 8
file content (66 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

{
    package example;

    use Moose;
    use Moose::Util::TypeConstraints;
    with qw(
        MooseX::Getopt
    );

    subtype 'ResultSet'
        => as 'DBIx::Class::ResultSet';

    subtype 'ResultList'
        => as 'ArrayRef[Int]';

    MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
            'ResultList'  => '=s',
    );

    coerce 'ResultList'
        => from 'Str'
        => via {
            return [ grep { m/^\d+$/ } split /\D/,$_ ]; # <- split string into arrayref
        };

    has 'results' => (
        is              => 'rw',
        isa             => 'ResultList | ResultSet', # <- union constraint
        coerce          => 1,
    );

    has 'other' => (
        is              => 'rw',
        isa             => 'Str',
    );
}

# Without MooseX::Getopt
{
    my $example = example->new({
        results => '1234,5678,9012',
        other   => 'test',
    });
    isa_ok($example, 'example');
    is_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
}

# With MooseX::Getopt
{
    local @ARGV = ('--results','1234,5678,9012','--other','test');
    my $example = example->new_with_options;
    isa_ok($example, 'example');

    is($example->other,'test');
    is_deeply($example->results, [qw(1234 5678 9012)], 'result as expected');
}

done_testing;