root/t/01getopt.t

Revision 223:296a92169936, 2.1 KB (checked in by Anselm Lingnau <anselm@…>, 4 years ago)

Fix some tests to bring them in line with the actual program.

Line 
1#!/usr/bin/perl
2
3use strict;
4
5BEGIN { chdir 't' if -d 't' }
6use lib '../lib';
7
8use Test::More tests => 25;
9
10#use Lancelot::GetOpt qw/GetOptions/;
11use Pod::Usage;
12
13{ no warnings;
14  package Pod::Usage;
15  use subs 'pod2usage';
16  package main;
17  *Pod::Usage::pod2usage = sub {
18      my (%args) = @_;
19      # if ($args{'-message'}
20  };
21}
22
23use_ok('Lancelot::GetOpt');
24
25my (@opts) = qw/opt1|A? opt2|B! opt3|C= opt4|D@ opt5|E:/;
26
27my (@tests) = ([ qw/--opt1/, "opt1=1" ],
28               [ qw/-A/, "opt1=1" ],
29               [ qw/--opt2/, "opt2=1" ],
30               [ qw/-B/, "opt2=1" ],
31               [ qw/--noopt2/, "opt2=0" ],
32               [ qw/--opt3=bla/, "opt3=bla" ],
33               [ qw/-C bla/, "opt3=bla" ],
34               [ qw/-C=bla/, "" ],
35               [ qw/--opt4=bla/, "opt4=[bla]" ],
36               [ qw/--opt4=bla --opt4=bli/, "opt4=[bla,bli]" ],
37               [ qw/-D bla/, "opt4=[bla]" ],
38               [ qw/-D bla -D bli/, "opt4=[bla,bli]" ],
39               [ qw/--opt5/, "opt5=" ],
40               [ qw/--opt5=bla/, "opt5=bla" ],
41               [ qw/-E/, "opt5=" ],
42               [ qw/-E bla/, "opt5= ARGV=bla" ],
43#              [ qw/--help/, "help=1" ],
44#              [ qw/-?/, "help=1" ],
45               [ qw/-v/, "verbose=" ],
46               [ qw/-v bla/, "verbose= ARGV=bla" ],
47               [ qw/--verbose bla/, "verbose= ARGV=bla" ],
48               [ qw/--verbose=bla/, "verbose=bla" ],
49
50               [ qw/--opt1/, [[ qw/-defaults/], "debug=0 opt1=1 opt2=-1 opt4=[]"]],
51               [ qw/-A bla/, [[ qw/-argvmin 1 too-few/ ], "opt1=1 ARGV=bla" ]],
52               [ qw/-A bla blubb/,
53                 [[ qw/-argvmin 1 too-few/ ], "opt1=1 ARGV=bla blubb" ]],
54               [ qw/-A bla blubb/,
55                 [[ qw/-argvmin 3 too-few/ ], "opt1=1 ARGV=bla blubb" ]],
56               );
57
58foreach my $t (@tests) {
59    my (@tt) = @$t;
60    my ($result) = pop @tt;
61    my (%options);
62    my ($control) = [];
63
64    ($control, $result) = @$result if ref($result) eq 'ARRAY';
65
66    @ARGV = @tt;
67    Lancelot::GetOpt::GetOptions(\%options, @$control, @opts);
68
69    foreach my $o (keys %options) {
70        if (ref($options{$o}) eq "ARRAY") {
71            $options{$o} = "[" . join(",", @{$options{$o}}) . "]";
72        }
73    }
74
75    my $output = join " ", map { "$_=$options{$_}" } sort keys %options;
76    $output .= " ARGV=@ARGV" if @ARGV;
77
78    is($output, $result, "Test @tt");
79}
Note: See TracBrowser for help on using the browser.