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