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