This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update the version of Darwin we know the by_BE locale to be broken on
[perl5.git] / lib / Getopt / Std.t
CommitLineData
12527e6c 1#!./perl -wT
1a3850a5
GA
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
1a3850a5
GA
6}
7
12527e6c
RGS
8use strict;
9use Test::More tests => 21;
1a3850a5
GA
10use Getopt::Std;
11
12527e6c
RGS
12our ($warning, $opt_f, $opt_i, $opt_o, $opt_x, $opt_y, %opt);
13
1a3850a5
GA
14# First we test the getopt function
15@ARGV = qw(-xo -f foo -y file);
16getopt('f');
17
12527e6c
RGS
18is( "@ARGV", 'file', 'options removed from @ARGV (1)' );
19ok( $opt_x && $opt_o && $opt_y, 'options -x, -o and -y set' );
20is( $opt_f, 'foo', q/option -f is 'foo'/ );
1a3850a5 21
12527e6c
RGS
22@ARGV = qw(-hij k -- -l m -n);
23getopt 'il', \%opt;
1a3850a5 24
12527e6c
RGS
25is( "@ARGV", 'k -- -l m -n', 'options removed from @ARGV (2)' );
26ok( $opt{h} && $opt{i} eq 'j', 'option -h and -i correctly set' );
27ok( !defined $opt{l}, 'option -l not set' );
28ok( !defined $opt_i, '$opt_i still undefined' );
1a3850a5
GA
29
30# Then we try the getopts
31$opt_o = $opt_i = $opt_f = undef;
32@ARGV = qw(-foi -i file);
1a3850a5 33
12527e6c
RGS
34ok( getopts('oif:'), 'getopts succeeded (1)' );
35is( "@ARGV", 'file', 'options removed from @ARGV (3)' );
36ok( $opt_i && $opt_f eq 'oi', 'options -i and -f correctly set' );
37ok( !defined $opt_o, 'option -o not set' );
1a3850a5 38
12527e6c
RGS
39%opt = (); $opt_i = undef;
40@ARGV = qw(-hij -k -- -l m);
1a3850a5 41
12527e6c
RGS
42ok( getopts('hi:kl', \%opt), 'getopts succeeded (2)' );
43is( "@ARGV", '-l m', 'options removed from @ARGV (4)' );
44ok( $opt{h} && $opt{k}, 'options -h and -k set' );
45is( $opt{i}, 'j', q/option -i is 'j'/ );
46ok( !defined $opt_i, '$opt_i still undefined' );
1a3850a5
GA
47
48# Try illegal options, but avoid printing of the error message
12527e6c 49$SIG{__WARN__} = sub { $warning = $_[0] };
1a3850a5
GA
50@ARGV = qw(-h help);
51
12527e6c
RGS
52ok( !getopts("xf:y"), 'getopts fails for an illegal option' );
53ok( $warning eq "Unknown option: h\n", 'user warned' );
1a3850a5
GA
54
55# Then try the Getopt::Long module
56
57use Getopt::Long;
58
59@ARGV = qw(--help --file foo --foo --nobar --num=5 -- file);
60
12527e6c
RGS
61our ($HELP, $FILE, $FOO, $BAR, $NO);
62
63ok( GetOptions(
64 'help' => \$HELP,
65 'file:s' => \$FILE,
66 'foo!' => \$FOO,
67 'bar!' => \$BAR,
68 'num:i' => \$NO,
69 ),
70 'Getopt::Long::GetOptions succeeded'
71);
72is( "@ARGV", 'file', 'options removed from @ARGV (5)' );
73ok( $HELP && $FOO && !$BAR && $FILE eq 'foo' && $NO == 5, 'options set' );