Implements feature suggestion RT#58715 by storing the Usage object, fixes
[gitmo/MooseX-Getopt.git] / t / 104_override_usage.t
1 use strict;
2 use warnings;
3 use Test::More 0.88;
4 use Test::Exception;
5
6 {
7     package MyScript;
8     use Moose;
9
10     with 'MooseX::Getopt';
11
12     has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo' );
13
14     our $usage = 0;
15     before _getopt_full_usage => sub { $usage++; };
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()) };
20 }
21 {
22     local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
23     local @ARGV = ('--foo', '1');
24     my $i = MyScript->new_with_options;
25     ok $i;
26     is $i->foo, 1;
27     is $MyScript::usage, undef;
28 }
29 {
30     local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
31     local @ARGV = ('--help');
32     throws_ok { MyScript->new_with_options } qr/A foo/;
33     is $MyScript::usage, 1;
34 }
35 {
36     local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
37     local @ARGV = ('-q'); # Does not exist
38     throws_ok { MyScript->new_with_options } qr/A foo/;
39     is_deeply \@MyScript::warnings, [
40           'Unknown option: q
41 '
42     ];
43     my $exp = [
44          'Unknown option: q
45 ',
46          qq{usage: 104_override_usage.t [-?] [long options...]
47 \t-? --usage --help  Prints this usage information.
48 \t--foo              A foo
49 }
50      ];
51
52      is_deeply \@MyScript::exception, $exp;
53 }
54
55 done_testing;
56