command_line.t 2.94 KB
Newer Older
1 2
#!/usr/bin/env perl

3
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Brandon Walts's avatar
Brandon Walts committed
4
# Copyright [2016-2020] EMBL-European Bioinformatics Institute
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


use strict;
use warnings;

use Test::More;
use Data::Dumper;

BEGIN {
    use_ok( 'Bio::EnsEMBL::Hive::Utils', 'parse_cmdline_options' );
}
#########################

## block 1 - simple single and double -- options
{
    ## note we do not require the double minus version
    local @ARGV = qw{-foo bar --alpha beta -count 3};
    my ($pairs, $list) = parse_cmdline_options();
    ok($pairs, 'command line parsed to a hash');
    isa_ok($pairs, 'HASH'); ## really a hash
    ok($list, 'list returned');
    isa_ok($list, 'ARRAY'); ## really a list ref
    is(@$list, 0, 'empty list');
    is($pairs->{'foo'}, 'bar', 'foo option set');
    is($pairs->{'alpha'}, 'beta', 'alpha option set');
    is($pairs->{'count'}, 3, 'count option set');
}

## what happens with one too many - ?
{
    local @ARGV = qw{---wrong option};
    my ($pairs, $list) = parse_cmdline_options();
    is(scalar(keys %$pairs), 0, 'ignore the --- option and assign to list instead');
    is(@$list, 2, 'list is populated');
    is($list->[0], '---wrong', 'no pruning of -');
    is($list->[1], 'option', 'fidelity');
}

## mix of options and extra
{
    local @ARGV = qw{-foo bar alpha beta --lorem ipsum};
    my ($pairs, $list) = parse_cmdline_options();
    ok($pairs, 'good');
    ok($list, 'still good');
    is($pairs->{'foo'}, 'bar', 'option foo set to bar');
    is($list->[0], 'alpha', 'centuri');
    is($list->[1], 'beta', 'globulin');
    is($pairs->{'lorem'}, 'ipsum', 'still latin');
}

## complex option array
{
    local @ARGV = (q!--foo=["bar","foobar"]!, 'alpha');
    my ($pairs, $list) = parse_cmdline_options();
    ok($pairs, 'pairs');
    isa_ok($pairs->{'foo'}, 'ARRAY'); ## array value
    is(@$list, 1, 'still set');

}

## complex option hash
{
    local @ARGV = (q!--foo={"bar" => "foobar"}!, 'alpha');
    my ($pairs, $list) = parse_cmdline_options();
    ok($pairs, 'pairs');
    isa_ok($pairs->{'foo'}, 'HASH'); ## hash value
    is(@$list, 1, 'still set');
}

## trailing key
{
    local @ARGV = qw{-foo bar alpha -beta};
    my ($pairs, $list) = parse_cmdline_options();
    ok($pairs, 'pairs');
    ok($list, 'list');
    is(@$list, 1, 'alpha only');
    is($list->[0], 'alpha', 'no beta here');
    is($pairs->{'foo'}, 'bar', 'bar still');
}

done_testing();