use warnings tester with fewer dependencies, issues
[gitmo/MooseX-Getopt.git] / t / 109_help_flag.t
CommitLineData
81b19ed8 1# The documentation claims:
2# If Getopt::Long::Descriptive is installed and any of the following command
3# line params are passed (--help, --usage, --?), the program will exit with
4# usage information...
5
6# This is not actually true (as of 0.29), as:
7# 1. the consuming class must set up a attributes named 'help', 'usage' and
8# '?' to contain these command line options, which is not clearly
9# documented as a requirement
10# 2. the code is checking whether an option was parsed into an attribute
11# *called* 'help', 'usage' or '?', not whether the option --help, --usage
12# or --? was passed on the command-line (the mapping could be different,
13# if cmd_flag or cmd_aliases is used),
14
15# This inconsistency is the underlying cause of RT#52474, RT#57683, RT#47865.
16
c885acae 17# Update: since 0.41, usage info is printed to stdout, not stderr.
18
25eb430d 19use strict;
20use warnings FATAL => 'all';
9fbb5be9 21use Test::More tests => 23;
25eb430d 22use Test::Warnings;
c885acae 23use Test::Trap;
81b19ed8 24
25{
26 package MyClass;
aec09248 27 use strict; use warnings FATAL => 'all';
81b19ed8 28 use Moose;
29 with 'MooseX::Getopt';
30}
31
32# before fix, prints this on stderr:
33#Unknown option: ?
b94db425 34#usage: test1.t
81b19ed8 35
c885acae 36# after fix, prints this on stdout (formerly stderr):
81b19ed8 37#usage: test1.t [-?] [long options...]
38# -? --usage --help Prints this usage information.
39
3aaa34a1 40my $obj = MyClass->new_with_options;
41ok($obj->meta->has_attribute('usage'), 'class has usage attribute');
42isa_ok($obj->usage, 'Getopt::Long::Descriptive::Usage');
43my $usage_text = $obj->usage->text;
44
8d396d8a 45foreach my $args ( ['--help'], ['--usage'], ['--?'], ['-?'], ['-h'] )
81b19ed8 46{
47 local @ARGV = @$args;
c885acae 48 note "Setting \@ARGV to @$args";
49
50 trap { MyClass->new_with_options() };
81b19ed8 51
c885acae 52 is($trap->leaveby, 'exit', 'bailed with an exit code');
53 is($trap->exit, 0, '...of 0');
54 is(
55 $trap->stdout,
3aaa34a1 56 $usage_text,
c885acae 57 'Usage information printed to STDOUT',
58 );
59 is($trap->stderr, '', 'there was no STDERR output');
81b19ed8 60}
61