33390de61a6bb6069408a608fe267a7d7fd4627f
[gitmo/MooseX-Getopt.git] / t / 109_help_flag.t
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
17 # Update: since 0.41, usage info is printed to stdout, not stderr.
18
19 use strict; use warnings;
20 use Test::More tests => 23;
21 use Test::NoWarnings 1.04 ':early';
22 use Test::Trap;
23
24 {
25     package MyClass;
26     use strict; use warnings;
27     use Moose;
28     with 'MooseX::Getopt';
29 }
30
31 # before fix, prints this on stderr:
32 #Unknown option: ?
33 #usage: test1.t
34
35 # after fix, prints this on stdout (formerly stderr):
36 #usage: test1.t [-?] [long options...]
37 #       -? --usage --help  Prints this usage information.
38
39 my $obj = MyClass->new_with_options;
40 ok($obj->meta->has_attribute('usage'), 'class has usage attribute');
41 isa_ok($obj->usage, 'Getopt::Long::Descriptive::Usage');
42 my $usage_text = $obj->usage->text;
43
44 foreach my $args ( ['--help'], ['--usage'], ['--?'], ['-?'], ['-h'] )
45 {
46     local @ARGV = @$args;
47     note "Setting \@ARGV to @$args";
48
49     trap { MyClass->new_with_options() };
50
51     is($trap->leaveby, 'exit', 'bailed with an exit code');
52     is($trap->exit, 0, '...of 0');
53     is(
54         $trap->stdout,
55         $usage_text,
56         'Usage information printed to STDOUT',
57     );
58     is($trap->stderr, '', 'there was no STDERR output');
59 }
60