use warnings tester with fewer dependencies, issues
[gitmo/MooseX-Getopt.git] / t / 104_override_usage.t
CommitLineData
175b83f5 1use strict;
aec09248 2use warnings FATAL => 'all';
1f314cdc 3
b1507072 4use Test::More tests => 7;
c885acae 5use Test::Trap;
25eb430d 6use Test::Warnings;
175b83f5 7
8{
9 package MyScript;
10 use Moose;
11
12 with 'MooseX::Getopt';
13
14 has foo => ( isa => 'Int', is => 'ro', documentation => 'A foo' );
175b83f5 15}
8aa34615 16
17# FIXME - it looks like we have a spacing issue in Getopt::Long?
18my $usage = <<USAGE;
19usage: 104_override_usage.t [-?h] [long options...]
20\t-h -? --usage --help Prints this usage information.
21\t--foo A foo
22USAGE
23
175b83f5 24{
25 local @ARGV = ('--foo', '1');
8aa34615 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');
175b83f5 29}
8aa34615 30
175b83f5 31{
32 local @ARGV = ('--help');
c885acae 33 trap { MyScript->new_with_options };
8aa34615 34 is($trap->stdout, $usage, 'usage is printed on --help');
175b83f5 35}
8aa34615 36
9b7f80a2 37{
9b7f80a2 38 local @ARGV = ('-q'); # Does not exist
c885acae 39 trap { MyScript->new_with_options };
8aa34615 40 is($trap->die, join("\n", 'Unknown option: q', $usage), 'usage is printed on unknown option');
9b7f80a2 41}
42
83446b78 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
b1507072 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