Commit | Line | Data |
175b83f5 |
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 | has help => ( isa => 'Bool', is => 'ro', default => 0, documentation => 'Help'); |
14 | |
15 | our $usage = 0; |
16 | before _getopt_full_usage => sub { $usage++; }; |
9b7f80a2 |
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()) }; |
175b83f5 |
21 | } |
22 | { |
9b7f80a2 |
23 | local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception; |
175b83f5 |
24 | local @ARGV = ('--foo', '1'); |
25 | my $i = MyScript->new_with_options; |
26 | ok $i; |
27 | is $i->foo, 1; |
9b7f80a2 |
28 | is $MyScript::usage, undef; |
175b83f5 |
29 | } |
30 | { |
9b7f80a2 |
31 | local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception; |
175b83f5 |
32 | local @ARGV = ('--help'); |
33 | throws_ok { MyScript->new_with_options } qr/A foo/; |
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 |
39 | throws_ok { MyScript->new_with_options } qr/A foo/; |
40 | is_deeply \@MyScript::warnings, [ |
41 | 'Unknown option: q |
42 | ' |
43 | ]; |
44 | my $exp = [ |
45 | 'Unknown option: q |
46 | ', |
47 | qq{usage: 104_override_usage.t [long options...] |
48 | \t--help Help |
49 | \t--foo A foo |
50 | } |
51 | ]; |
52 | |
53 | is_deeply \@MyScript::exception, $exp; |
54 | } |
55 | |
175b83f5 |
56 | done_testing; |
57 | |