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