8e7a1ff842d173694ae4a2453a51d1c394940fce
[gitmo/MooseX-Getopt.git] / t / 104_override_usage.t
1 use strict;
2 use warnings;
3 use Test::More 0.88;
4 use Test::Trap;
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     trap { MyScript->new_with_options };
33     like($trap->stdout, qr/A foo/);
34     is $MyScript::usage, 1;
35 }
36 {
37     local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
38     local @ARGV = ('-q'); # Does not exist
39     trap { MyScript->new_with_options };
40     like($trap->die, qr/A foo/);
41     is_deeply \@MyScript::warnings, [
42           'Unknown option: q
43 '
44     ];
45     my $exp = [
46          'Unknown option: q
47 ',
48          qq{usage: 104_override_usage.t [-?] [long options...]
49 \t-? --usage --help  Prints this usage information.
50 \t--foo              A foo
51 }
52      ];
53
54      is_deeply \@MyScript::exception, $exp;
55 }
56
57 done_testing;
58