summaryrefslogtreecommitdiffstats
path: root/libtests/qtest/arg_parser.test
blob: abaf4d291ad40c2daf8655fd1cba7ef5d617ce8d (plain) (blame)
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

chdir("arg_parser") or die "chdir testdir failed: $!\n";
unshift(@INC, '.');
require completion_helpers;

require TestDriver;

my $td = new TestDriver('arg_parser');

my @completion_tests = (
    ['', 0, 'bad-input-1'],
    ['', 1, 'bad-input-2'],
    ['', 2, 'bad-input-3'],
    ['arg_parser', 2, 'bad-input-4'],
    ['arg_parser ', undef, 'top'],
    ['arg_parser -', undef, 'top-arg'],
    ['arg_parser --po', undef, 'po'],
    ['arg_parser --potato ', undef, 'potato'],
    ['arg_parser --quack ', undef, 'quack'],
    ['arg_parser --quack -', undef, 'quack-'],
    ['arg_parser --quack x ', undef, 'quack-x'],
    ['arg_parser --quack x x ', undef, 'quack-x-x'],
    ['arg_parser --baaa -', undef, 'baaa'],
    ['arg_parser --baaa -- --', undef, 'second'],
    ['arg_parser @quack-xyz ', undef, 'quack-x-y-z'],
    ['arg_parser --quack \'user " password\' ', undef, 'quack-x'],
    ['arg_parser --quack \'user password\' ', undef, 'quack-x'],
    ['arg_parser --quack "user password" ', undef, 'quack-x'],
    ['arg_parser --quack "user pass\'word" ', undef, 'quack-x'],
    ['arg_parser --quack user\ password ', undef, 'quack-x'],
    ['arg_parser --sheep --', undef, 'sheep'],
    );

foreach my $c (@completion_tests)
{
    my ($cmd, $point, $description) = @$c;
    my $out = "completion-$description.out";
    my $zout = "completion-$description-zsh.out";
    if (! -f $zout)
    {
        $zout = $out;
    }
    $td->runtest("bash completion: $description",
                 {$td->COMMAND =>
                      [@{bash_completion("arg_parser", $cmd, $point)}],
                      $td->FILTER => "perl filter-completion.pl $out"},
                 {$td->FILE => "$out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("zsh completion: $description",
                 {$td->COMMAND =>
                      [@{zsh_completion("arg_parser", $cmd, $point)}],
                      $td->FILTER => "perl filter-completion.pl $zout"},
                 {$td->FILE => "$zout", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
}

my @arg_tests = (
    ['--potato', 0],            # 0
    ['--oops', 2],              # 1
    ['--version', 0],           # 2
    ['--version --potato', 2],  # 3
    ['--potato --version', 2],  # 4
    ['--quack', 2],             # 5
    ['--quack --', 0],          # 6
    ['--quack 1 2 3 --', 0],    # 7
    ['--potato --quack 1 2 3 --' . # 8
     ' --potato --quack a b c --' .
     ' --baaa --ram --', 0],
    ['--baaa --potato --', 2],  # 9
    ['--baaa --ewe', 2],        # 10
    ['--oink=baaa', 2],         # 11
    ['--oink=sow', 0],          # 12
    ['-oink=sow', 0],           # 13
    ['@quack-xyz', 2],          # 14
    ['@quack-xyz --', 0],       # 15
    ['--salad', 2],             # 16
    ['--salad=spinach', 0],     # 17
    );

for (my $i = 0; $i < scalar(@arg_tests); ++$i)
{
    my ($args, $status) = @{$arg_tests[$i]};
    $td->runtest("arg_tests $i",
                 {$td->COMMAND => "arg_parser $args"},
                 {$td->FILE => "args-$i.out", $td->EXIT_STATUS => $status},
                 $td->NORMALIZE_NEWLINES);
}

$td->runtest("exceptions",
             {$td->COMMAND => "arg_parser exceptions"},
             {$td->FILE => "exceptions.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

$td->runtest("args from stdin",
             {$td->COMMAND => 'echo --potato | arg_parser @-'},
             {$td->FILE => "stdin.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

my @help_tests = (
    '',
    '=all',
    '=--ewe',
    '=quack',
    );
foreach my $i (@help_tests)
{
    my $out = $i;
    $out =~ s/[=-]//g;
    if ($out ne '')
    {
        $out = "-$out";
    }
    $td->runtest("--help$i",
                 {$td->COMMAND => "arg_parser --help$i"},
                 {$td->FILE => "help$out.out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
}

$td->runtest("bad help option",
             {$td->COMMAND => 'arg_parser --help=--oops'},
             {$td->FILE => "help-bad.out", $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

$td->report(3 + (2 * scalar(@completion_tests)) +
            scalar(@arg_tests) + scalar(@help_tests));