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