8fa9299aeb69973eebe07f831ea936baa28c251f
[gitmo/MooseX-Getopt.git] / t / 104_override_usage.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 7;
5 use Test::Trap;
6 use Test::NoWarnings 1.04 ':early';
7
8 {
9     package MyScript;
10     use Moose;
11
12     with 'MooseX::Getopt';
13
14     has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo' );
15 }
16
17 # FIXME - it looks like we have a spacing issue in Getopt::Long?
18 my $usage = <<USAGE;
19 usage: 104_override_usage.t [-?h] [long options...]
20 \t-h -? --usage --help  Prints this usage information.
21 \t--foo                A foo
22 USAGE
23
24 {
25     local @ARGV = ('--foo', '1');
26     my $i = trap { MyScript->new_with_options };
27     is($i->foo, 1, 'attr is set');
28     is($trap->stdout, '', 'nothing printed when option is accepted');
29 }
30
31 {
32     local @ARGV = ('--help');
33     trap { MyScript->new_with_options };
34     is($trap->stdout, $usage, 'usage is printed on --help');
35 }
36
37 {
38     local @ARGV = ('-q'); # Does not exist
39     trap { MyScript->new_with_options };
40     is($trap->die, join("\n", 'Unknown option: q', $usage), 'usage is printed on unknown option');
41 }
42
43 {
44     Class::MOP::class_of('MyScript')->add_before_method_modifier(
45         print_usage_text => sub {
46             print "--- DOCUMENTATION ---\n";
47         },
48     );
49
50     local @ARGV = ('--help');
51     trap { MyScript->new_with_options };
52     is(
53         $trap->stdout,
54         join("\n", '--- DOCUMENTATION ---', $usage),
55         'additional text included before normal usage string',
56     );
57 }
58
59 {
60     package MyScript2;
61     use Moose;
62
63     with 'MooseX::Getopt';
64     has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo' );
65 }
66
67 {
68     # some classes (e.g. ether's darkpan and Catalyst::Runtime) overrode
69     # _getopt_full_usage, so we need to keep it in the call stack so we don't
70     # break them.
71     Class::MOP::class_of('MyScript2')->add_before_method_modifier(
72         _getopt_full_usage => sub {
73             print "--- DOCUMENTATION ---\n";
74         },
75     );
76
77     local @ARGV = ('--help');
78     trap { MyScript2->new_with_options };
79     is(
80         $trap->stdout,
81         join("\n", '--- DOCUMENTATION ---', $usage),
82         'additional text included before normal usage string',
83     );
84 }
85