File: 104_override_usage.t

package info (click to toggle)
libmoosex-getopt-perl 0.78-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: perl: 607; makefile: 2
file content (104 lines) | stat: -rwxr-xr-x 2,573 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use strict;
use warnings;

use Test::More 0.88;
use Test::Trap;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Moose::Util 'find_meta';

$ENV{COLUMNS} = 80;

{
    package MyScript;
    use Moose;

    with 'MooseX::Getopt';

    has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo
with newline and some 123456789 123456789 123456789 characters' );
}

my $usage = qr/^\Qusage: 104_override_usage.t [-?h] [long options...]\E
\s+.*--help.+Prints this usage information\..*
\s+--foo (INT)?\s+A foo.+characters/ms;

{
    local @ARGV = ('--foo', '1');
    my $i = trap { MyScript->new_with_options };
    is($i->foo, 1, 'attr is set');
    is($trap->stdout, '', 'nothing printed when option is accepted');
}

{
    local @ARGV = ('--help');
    trap { MyScript->new_with_options };
    like($trap->stdout, qr/\A$usage\Z/, 'usage is printed on --help');
}

{
    local @ARGV = ('-q'); # Does not exist
    trap { MyScript->new_with_options };
    like($trap->die, qr/\AUnknown option: q\n$usage\Z/, 'usage is printed on unknown option');
}

{
    find_meta('MyScript')->add_before_method_modifier(
        print_usage_text => sub {
            print "--- DOCUMENTATION ---\n";
        },
    );

    local @ARGV = ('--help');
    trap { MyScript->new_with_options };
    like(
        $trap->stdout,
        qr/^--- DOCUMENTATION ---\n$usage\Z/,
        'additional text included before normal usage string',
    );
}

{
    find_meta('MyScript')->add_after_method_modifier(
        print_usage_text => sub {
            print "--- DOCUMENTATION ---\n";
        },
    );

    local @ARGV = ('--help');
    trap { MyScript->new_with_options };
    like(
        $trap->stdout,
        qr/$usage\n--- DOCUMENTATION ---\n/,
        'additional text included before normal usage string',
    );
}

{
    package MyScript2;
    use Moose;

    with 'MooseX::Getopt';
    has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo
with newline and some 123456789 123456789 123456789 characters' );
}

{
    # some classes (e.g. ether's darkpan and Catalyst::Runtime) overrode
    # _getopt_full_usage, so we need to keep it in the call stack so we don't
    # break them.
    find_meta('MyScript2')->add_before_method_modifier(
        _getopt_full_usage => sub {
            print "--- DOCUMENTATION ---\n";
        },
    );

    local @ARGV = ('--help');
    trap { MyScript2->new_with_options };
    like(
        $trap->stdout,
        qr/^--- DOCUMENTATION ---\n$usage/,
        'additional text included before normal usage string',
    );
}

done_testing;