File: 104_override_usage.t

package info (click to toggle)
libmoosex-getopt-perl 0.45-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 356 kB
  • sloc: perl: 1,964; makefile: 8
file content (58 lines) | stat: -rw-r--r-- 1,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use strict;
use warnings;
use Test::More 0.88;
use Test::Trap;

{
    package MyScript;
    use Moose;

    with 'MooseX::Getopt';

    has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo' );

    our $usage = 0;
    before _getopt_full_usage => sub { $usage++; };
    our @warnings;
    before _getopt_spec_warnings => sub { shift; push(@warnings, @_) };
    our @exception;
    before _getopt_spec_exception => sub { shift; push(@exception, @{ shift() }, shift()) };
}
{
    local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
    local @ARGV = ('--foo', '1');
    my $i = MyScript->new_with_options;
    ok $i;
    is $i->foo, 1;
    is $MyScript::usage, undef;
}
{
    local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
    local @ARGV = ('--help');
    trap { MyScript->new_with_options };
    like($trap->stdout, qr/A foo/);
    is $MyScript::usage, 1;
}
{
    local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception;
    local @ARGV = ('-q'); # Does not exist
    trap { MyScript->new_with_options };
    like($trap->die, qr/A foo/);
    is_deeply \@MyScript::warnings, [
          'Unknown option: q
'
    ];
    my $exp = [
         'Unknown option: q
',
         qq{usage: 104_override_usage.t [-?] [long options...]
\t-? --usage --help  Prints this usage information.
\t--foo              A foo
}
     ];

     is_deeply \@MyScript::exception, $exp;
}

done_testing;