print --usage, --help to stdout not stderr
[gitmo/MooseX-Getopt.git] / t / 104_override_usage.t
CommitLineData
175b83f5 1use strict;
2use warnings;
3use Test::More 0.88;
c885acae 4use Test::Trap;
175b83f5 5
6{
7 package MyScript;
8 use Moose;
9
10 with 'MooseX::Getopt';
11
12 has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo' );
175b83f5 13
14 our $usage = 0;
15 before _getopt_full_usage => sub { $usage++; };
9b7f80a2 16 our @warnings;
17 before _getopt_spec_warnings => sub { shift; push(@warnings, @_) };
18 our @exception;
19 before _getopt_spec_exception => sub { shift; push(@exception, @{ shift() }, shift()) };
175b83f5 20}
21{
9b7f80a2 22 local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
175b83f5 23 local @ARGV = ('--foo', '1');
24 my $i = MyScript->new_with_options;
25 ok $i;
26 is $i->foo, 1;
9b7f80a2 27 is $MyScript::usage, undef;
175b83f5 28}
29{
9b7f80a2 30 local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
175b83f5 31 local @ARGV = ('--help');
c885acae 32 trap { MyScript->new_with_options };
33 like($trap->stdout, qr/A foo/);
175b83f5 34 is $MyScript::usage, 1;
35}
9b7f80a2 36{
37 local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
38 local @ARGV = ('-q'); # Does not exist
c885acae 39 trap { MyScript->new_with_options };
40 like($trap->die, qr/A foo/);
9b7f80a2 41 is_deeply \@MyScript::warnings, [
42 'Unknown option: q
43'
44 ];
45 my $exp = [
46 'Unknown option: q
47',
81b19ed8 48 qq{usage: 104_override_usage.t [-?] [long options...]
49\t-? --usage --help Prints this usage information.
50\t--foo A foo
9b7f80a2 51}
52 ];
53
54 is_deeply \@MyScript::exception, $exp;
55}
56
175b83f5 57done_testing;
58